43.2.6 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 ADC conversion clock
    2. Configure voltage reference
    3. Select ADC input channel
    4. Configure precharge (ADPRE) and acquisition (ADACQ) time period
    5. Turn on ADC module
  3. Configure ADC interrupt (optional):
    1. Clear the ADC interrupt flag
    2. Enable the ADC interrupt
    3. Enable global interrupt (GIE bit)(1)
  4. If ADACQ = 0, software must wait the required acquisition time(2).
  5. Start conversion by setting the GO bit.
  6. Wait for ADC conversion to complete by one of the following:
    • Polling the GO bit
    • Waiting for the ADC interrupt (if interrupt is enabled)
  7. Read ADC Result.
  8. Clear the ADC interrupt flag (if interrupt is enabled).
Note:
  1. With global interrupts disabled (GIE = 0), the device will wake from Sleep, but will not enter an Interrupt Service Routine.
  2. Refer to the ADC Acquisition Requirements section for more details.

ADC Conversion (assembly)

; This code block configures the ADC for polling, Vdd and Vss references,
; ADCRC oscillator, and AN0 input.
; Conversion start & polling for completion are included.

    BANKSEL ADCON1      ;
    clrf    ADCON1      ;
    clrf    ADCON2      ; Legacy mode, no filtering, ADRES->ADPREV
    clrf    ADCON3      ; no math functions
    clrf    ADREF       ; Vref = Vdd & Vss
    clrf    ADPCH       ; select RA0/AN0
    clrf    ADACQ       ; software controlled acquisition time
    clrf    ADCAP       ; default S&H capacitance
    clrf    ADRPT       ; no repeat measurements
    clrf    ADACT       ; auto-conversion disabled
    movlw   B'10010100' ; ADC On, right-justified, ADCRC clock
    movwf   ADCON0
    BANKSEL TRISA       ;
    bsf     TRISA,0     ; Set RA0 to input
    BANKSEL ANSEL       ;
    bsf     ANSEL,0     ; Set RA0 to analog
    call    SampleTime  ; Acquisiton delay
    BANKSEL ADCON0 
    bsf     ADCON0,GO   ; Start conversion
    btfsc   ADCON0,GO   ; Is conversion done?
    goto    $-2         ; No, test again
    BANKSEL ADRESH      ;
    movf    ADRESH,W    ; Read upper byte
    movwf   RESULTHI    ; store in GPR space
    movf    ADRESL,W    ; Read lower byte
    movwf   RESULTLO    ; Store in GPR space

ADC Conversion (C)

/*This code block configures the ADC
for polling, VDD and VSS references,
ADCRC 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;      //ADCRC 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
    }
 }