15.5.5 Oversampling
In Oversampling Example, the conversions are started by a software trigger and continued by back-to-back triggers. The number of accumulated conversion results is set to 16. The oversampling process cannot be interrupted by other channel conversions because the ACCBRST bit is set.
Oversampling Example
#include <xc.h>
// The channel output.
long result = 0;
int main(){
// In this example channel 4 will be used.
// Oversampling conversion mode.
AD1CH4CONbits.MODE = 3;
// Set number of conversions accumulated to 16.
AD1CH4CONbits.ACCNUM = 1;
// The oversampling if started cannot be interrupted
// by a high priority channels conversion requests.
AD1CH4CONbits.ACCBRST = 1;
// Software trigger will start a conversions.
AD1CH4CONbits.TRG1SRC = 1;
// Re-trigger back to back.
AD1CH4CONbits.TRG2SRC = 2;
// Select the AN5 analog positive input/pin.
AD1CH4CONbits.PINSEL = 5;
// Select signal sampling time (6.5 TADs = 81nS).
AD1CH4CONbits.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 #4 in software and wait for the 16 samples
// oversampling result.
while(1){
// Trigger channel # 4.
AD1SWTRGbits.CH4TRG = 1;
// Wait for a conversion ready flag.
while(AD1STATbits.CH4RDY == 0);
// Read oversampling result. It will clear the conversion ready flag.
result = AD1CH4DATA;
}
return 1;
}