16.6.4 Integration of the Multiple Samples

In Integration of the Multiple Samples Example, the conversions are started by a software trigger and continued by back-to-back triggers until the number of conversions is less than the value set in the AD3CH0CNT register.

Integration of the Multiple Samples Example

#include <xc.h>

int main(){ 
    
    //Set up clock for 40Msps operation
    clock_ADC_for_40Msps_from_PLL2();
    
    _TRISC8 = 0;
    
    // In this example channel 15 will be used.
    // Set number of conversions accumulated to 123.
    AD3CH0CNT = 123;
    // Software trigger will start a conversions.
    AD3CH0CON1bits.TRG1SRC = 1; 
    // Re-trigger back to back.
    AD3CH0CON1bits.TRG2SRC = 2; 
    // Select the AN0 analog positive input/pin.
    AD3CH0CON1bits.PINSEL = 0;
    // Select signal sampling time (6.5 TADs = 81nS).
    AD3CH0CON1bits.SAMC = 3;
    // Enable ADC.
    AD3CONbits.ON = 1;
    // Wait when ADC will be ready/calibrated.
    while(AD3CONbits.ADRDY == 0);
    // Trigger channel #15 in software and wait for the 123 samples
    // accumulated 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.
       result = AD3CH0DATA;
       
       _LATC8 ^= 1;
    }
    return 1;
}