27.2.5 ADC Conversion Procedure

This is an example procedure for using the ADC to perform an analog-to-digital conversion:

  1. Configure port:
    1. Disable the pin output driver (refer to the TRISx register)
    2. Configure the pin as analog (refer to the ANSELx register)
  2. Configure the ADC module:
    1. Select the ADC conversion clock
    2. Configure the voltage reference
    3. Select the ADC input channel
    4. Configure result format
    5. Turn on the ADC module
  3. Configure ADC interrupt (optional):
    1. Clear the ADC interrupt flag
    2. Enable the ADC interrupt
    3. Enable the peripheral interrupt (PEIE bit)
    4. Enable the global interrupt (GIE bit)(1)
  4. 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 ADC Acquisition Requirements 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 TRISA
    BSF     TRISA,0     ; Set RA0 to input
    BANKSEL ANSEL
    BSF     ANSEL,0     ; Set RA0 to analog
    BANKSEL ADCON0
    CLRF    ADCON0
    CLRF    ADCON1
    CLRF    ADACT       ; Auto-conversion disabled
    BSF     ADCON0,0    ; CHS = RA0, ADC ON
    MOVLW   B’11110000’ ; FM = Right-justified, CS = ADCRC, PREF = VDD
    MOVWF   ADCON1
    CALL    SampleTime  ; Acquisition delay
    BANKSEL ADCON0
    BSF     ADCON0,GO   ; Start conversion
    BTFSC   ADCON0,GO   ; Is conversion done?
    GOTO    $-1         ; 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();

        // Configure Port
        TRISAbits.TRISA0 = 1;      // Set RA0 to input
        ANSELAbits.ANSELA0 = 1;    // Set RA0 to analog

        // Configure ADC
        ADCON1bits.CS = 1;         // ADCRC Clock
        ADCON1bits.PREF = ‘b11;    // VDD
        ADCON0bits.CHS = ‘b000000; // RA0
        ADCON1bits.FM = 1;         // Right justify
        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
    }
 }