15.5.2 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()
{
// In this example channel 1 will be used.
// Select single conversion mode.
AD1CH1CONbits.MODE = 0;
// Software trigger will start a conversion.
AD1CH1CONbits.TRG1SRC = 1;
// Use a differential input.
AD1CH1CONbits.DIFF = 1;
// Select the AN9 analog positive input/pin for the signal.
AD1CH1CONbits.PINSEL = 9;
// Select the ANN1 analog negative input/pin for the signal.
AD1CH1CONbits.NINSEL = 1;
// Select signal sampling time (6.5 TADs = 81nS).
AD1CH1CONbits.SAMC = 3;
// Set ADC to RUN mode.
AD1CONbits.MODE = 2;
// Enable ADC.
AD1CONbits.ON = 1;
// Wait when ADC will be ready/calibrated.
while(AD1CONbits.ADRDY == 0);
// Trigger channel #1 in software and wait for the result.
while(1){
// Trigger channel # 1.
AD1SWTRGbits.CH1TRG = 1;
// Wait for a conversion ready flag.
while(AD1STATbits.CH1RDY == 0);
// Read result. It will clear the conversion ready flag.
result = AD1CH1DATA;
}
return 1;
}