ADC Conversion Procedure (Basic Mode)

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

  1. 1.Configure Port:
    1. a.Disable pin output driver (refer to the TRISx register)
    2. b.Configure pin as analog (refer to the ANSELx register)
  2. 2.Configure the ADC module:
    1. a.Select ADC conversion clock
    2. b.Configure voltage reference
    3. c.Select ADC input channel
    4. d.Configure precharge (ADPRE) and acquisition (ADACQ) time period
    5. e.Turn on ADC module
  3. 3.Configure ADC interrupt (optional):
    1. a.Clear ADC interrupt flag
    2. b.Enable ADC interrupt
    3. c.Enable global interrupt (GIE bit)(1)
  4. 4.If ADACQ != 0, software must wait the required acquisition time(2).
  5. 5.Start conversion by setting the GO bit.
  6. 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. 7.Read ADC Result.
  8. 8.Clear the ADC interrupt flag (if interrupt is enabled).
Notes:
  1. 1.With global interrupts disabled (GIE = 0), the device will wake from Sleep, but will not enter an Interrupt Service Routine.
  2. 2.Refer to the ADC Acquisition Requirements section for more details.

ADC Conversion (Differential Inputs)

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

    //Setup ADC
    ADCON0bits.FM = 10;     //Right justify, sign/magnitude
    ADCON0bits.CS = 1;      //ADCRC Clock
    ADCON0bits.IC = 1;      //Differential mode
    ADPCH = 0x00;           //RA0 is positive input
    ADNCH = 0x01;           //RA1 is negative input
    TRISAbits.TRISA0 = 1;   //Set RA0 to input
    TRISAbits.TRISA1 = 1;   //Set RA1 to input
    ANSELAbits.ANSELA0 = 1; //Set RA0 to analog
    ANSELAbits.ANSELA1 = 1; //Set RA1 to analog
    ADACQ = 32;             //Set acquitisition time
    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
    }
 }