16.5.1 Single Conversion

In Single Conversion Example, a software trigger is used to start a single conversion.

Single Conversion Example

#include <xc.h>

// The channel output
long result = 0;

int main()
{
    // Set up clock for 40Msps operation
    clock_ADC_for_40Msps_from_PLL2();

    // In this example channel 1 will be used.
    // Select single conversion mode.
    AD5CH0CON1bits.MODE = 0;
    // Software trigger will start a conversion.
    AD5CH0CON1bits.TRG1SRC = 1;
    // Use a single ended input.
    AD5CH0CON1bits.DIFF = 0;
    // Select the AN0 analog positive input/pin for the signal.
    AD5CH0CON1bits.PINSEL = 0;
    // Select the ANN0 (Vss) analog negative input/pin for the signal.
    AD5CH0CON1bits.NINSEL = 0;
    // Select signal sampling time (6.5 TADs = 81nS).
    AD5CH0CON1bits.SAMC = 3;
    // Set ADC to RUN mode.
    AD5CONbits.MODE = 2;
    // Enable ADC.
    AD5CONbits.ON = 1;
    // Wait when ADC will be ready/calibrated.
    while (AD5CONbits.ADRDY == 0);
    // Trigger channel #1 in software and wait for the result.
    while (1) {
        // Trigger channel # 1.
        AD5SWTRGbits.CH0TRG = 1;
        // Wait for a conversion ready flag.
        while (AD5STATbits.CH0RDY == 0);
        // Read result. It will clear the conversion ready flag.
        result = AD5CH0DATA;
    }
    return 1;
}