32.2.7 ADC Conversion Procedure (Basic Mode)

This is an example procedure for using the ADC to perform an Analog-to-Digital Conversion:

  1. Configure Port:
    1. Disable pin output driver (Refer to the TRISx register)
    2. Configure pin as analog (Refer to the ANSELx register)
  2. Configure the ADC module:
    1. Select the ADC conversion clock
    2. Configure voltage reference
    3. Select the ADC input channel (precharge + acquisition)
    4. Turn on the ADC module
  3. Configure the ADC interrupt (optional):
    1. Clear the ADC interrupt flag
    2. Enable the ADC interrupt
    3. Enable the peripheral interrupt (PIE bit)
    4. Enable the global interrupt (GIE bit) (see the Note 1 below)
  4. If ADACQ = 0, software must wait the required acquisition time (see the Note 2 below).
  5. Start conversion by setting the GO bit.
  6. Wait for the ADC conversion to complete by one of the following:
    1. Polling the GO bit
    2. Polling the ADIF bit
    3. Waiting for the ADC interrupt (interrupts enabled)
  7. Read the ADC result.
  8. Clear the ADC interrupt flag (required if interrupt is enabled).
Important:
  1. The global interrupt can be disabled if the user is attempting to wake up from Sleep and resume in-line code execution.
  2. Refer to the “ADC Acquisition Requirements” section.

ADC Conversion (assembly)

; This code block configures the ADC for polling, VDD and VSS references,
; FRC oscillator, and AN0 input.
; Conversion start & polling for completion are included.
;
    BANKSEL ADCON1      ;
    movlw   B’11110000’ ;Right justify, FRC oscillator
    movwf   ADCON1      ;Vdd and Vss Vref
    BANKSEL TRISA       ;
    bsf     TRISA,0     ;Set RA0 to input
    BANKSEL ANSEL       ;
    bsf     ANSEL,0     ;Set RA0 to analog
    BANKSEL ADCON0      ;
    movlw   B’00000001’ ;Select channel AN0
    movwf   ADCON0      ;Turn ADC On
    call    SampleTime  ;Acquisiton delay
    bsf     ADCON0,ADGO ;Start conversion
    btfsc   ADCON0,ADGO ;Is conversion done?
    goto    $-1         ;No, test again
    BANKSEL ADRESH      ;
    movf    ADRESH,W    ;Read upper 2 bits
    movwf   RESULTHI    ;store in GPR space
    movf    ADRESL,W    ;Read lower 8 bits
    movwf   RESULTLO    ;Store in GPR space	

ADC Conversion (C)


/*This code block configures the ADC for polling, VDD and VSS references, FRC
oscillator and AN0 input.
Conversion start & polling for completion are included.
*/
void main() {
//System Initialize
initializeSystem();

//Setup ADC
ADCON0bits.FM = 1;      //right justify
ADCON0bits.CS = 1;      //FRC Clock
ADPCH = 0x00;           //RA0 is Analog channel
TRISAbits.TRISA0 = 1;   //Set RA0 to input
ANSELAbits.ANSELA0 = 1; //Set RA0 to analog
ADCON0bits.ON = 1;      //Turn ADC On

while (1) {
ADCON0bits.GO = 1;     //Start conversion
while (ADCON0bits.GO); //Wait for conversion done
resultHigh = ADRESH;   //Read result
resultLow = ADRESL;    //Read result
}
}