43.6.2.2 Channel Sequencer Configuration Examples

The following code examples illustrate the configuration of the channel sequencer. The first example uses software to set the GO bit and perform one complete sequence, while the second uses an auto-conversion trigger to perform sequences at a timed interval. Both examples scan Contexts 1, 2, and 4, while skipping Context 3.

Software Sets GO

void scanOnce(void)
{
    configScan124();             // Configure sequencer
    ADCON0bits.ON = 1;           // Enable the ADC
    ADCON0bits.GO = 1;           // Software starts the sequencer
}
void configScan124(void)
{
    configADCContext();          // Configure channel contexts
    ADCSEL1.CHEN = 1;            // Enable Context 1
    ADCSEL1.SSI = 0;             // Don't stop on interrupt
    ADCSEL2.CHEN = 1;            // Enable Context 2
    ADCSEL2.SSI = 0;             // Don't stop on interrupt
    ADCSEL3.CHEN = 0;            // Context 3 not included in scan
    ADCSEL3.SSI = 0;             // Don't stop on interrupt
    ADCSEL4.CHEN = 0;            // Enable Context 4
    ADCSEL4.SSI = 1;             // Stop scan on last context
            
    ADCTX = 0x03;                // Select Context 4
    ADCON3bits.TMD = 0b111;      // Interrupt regardless of test results
    ADCON0bits.CSEN = 1;         // Enable the sequencer
}

Auto-Conversion Trigger Sets GO

void main(void)
{
    TMR1_Initialize();           // Configure TMR1 
    configScan124();             // Configure sequencer
    ADACT = TMR1_overflow;       // TMR1 is Auto-Conversion trigger
    ADCON0bits.ON = 1;           // Enable the ADC
    T1CONbits.ON = 1;            // Start TMR1
    while(1)
    {
        // wait for ADCH4IF = 1 and service ISR
    }
}