16.5.1 16-bit Conversion Example

In 16-bit Conversion, the conversions are started by a software trigger and continued by back-to-back triggers. The number of accumulated conversion results is set to 256 to get the 16-bit result. The oversampling process cannot be interrupted by other channel conversions because the ACCBRST bit is set.

16-bit Conversion

#include <xc.h>
// The channel output.
long result16bit = 0;
int main(){
 
clock_ADC_for_40Msps_from_PLL2();
 
// In this example channel 0 will be used.
// Oversampling conversion mode.
AD3CH0CON1bits.MODE = 3;
// Set number of conversions accumulated to 256/16bit result.
AD3CH0CON1bits.ACCNUM = 3;
// The oversampling if started cannot be interrupted
// by a high priority channels conversion request.
AD3CH0CON2bits.ACCBRST = 1;
// Software trigger will start conversions.
AD3CH0CON1bits.TRG1SRC = 1;
// Re-trigger back to back.
AD3CH0CON1bits.TRG2SRC = 2;
// Select the AN0 analog positive input/pin.
AD3CH0CON1bits.PINSEL = 0;
// Interrupt when oversampling is done.
AD3CH0CON1bits.IRQSEL = 1;
// Select signal sampling time to 6.5 TADs.
AD3CH0CON1bits.SAMC = 3;
// Enable ADC.
AD3CONbits.ON = 1;
// Wait when ADC will be ready/calibrated.
while(AD3CONbits.ADRDY == 0);
// Trigger channel #0 in software and wait for the 256 samples
// 16 bit oversampling result.
while(1){
// Trigger channel # 0.
AD3SWTRGbits.CH0TRG = 1;
// Wait for a conversion ready flag.
while(AD3STATbits.CH0RDY == 0);
// Read oversampling result. It will clear the conversion ready flag.
result16bit = AD3CH0DATA;
}
return 1;
}