15.5.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 set in the AD1CNT15 register.

Integration of the Multiple Samples Example

#include <xc.h>

// The channel output.
long result = 0; 

int main() { 
     // In this example channel 15 will be used.
     // Integration conversion mode.
     AD1CH15CONbits.MODE = 2;
     // Set number of conversions accumulated to 123.
     AD1CH15CNT = 123;
     // Software trigger will start a conversions.
     AD1CH15CONbits.TRG1SRC = 1; 
     // Re-trigger back to back.
     AD1CH15CONbits.TRG2SRC = 2; 
     // Select the AN5 analog positive input/pin.
     AD1CH15CONbits.PINSEL = 5;
     // Select signal sampling time (6.5 TADs = 81nS).
     AD1CH15CONbits.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 #15 in software and wait for the 123 samples
     // accumulated result.
     while(1){
        // Trigger channel # 15.
        AD1SWTRGbits.CH15TRG = 1;
        // Wait for a conversion ready flag.
        while(AD1STATbits.CH15RDY == 0);
        // Read oversampling result. It will clear the conversion ready flag.
        result = AD1CH15DATA;
     }
     return 1;
}