Analog to digital converter using PIC


Most of the sensors and transducers are analog such as temperature, humidity, pressure. To interface these sensors to micro-controllers we need to convert the analog output of these sensors to digital.

  • The PIC Microcontrollers have built-in ADC modules.
  • PIC Microcontrollers have 8 channel,10bit ADC.
  • The positive and negative reference voltage (+Vref and -Vref) of PIC ADC is software selectable.
  • The A/D converter has a unique feature of being able to operate while the device is in Sleep mode.

ADC Registers

  • The A/D module has four registers.
    1. A/D Result High Register (ADRESH)
    2. A/D Result Low Register (ADRESL)
    3. A/D Control Register 0 (ADCON0)
    4. A/D Control Register 1 (ADCON1).

ADCON0 Register

  • ADCON0 register controls the operation of the A/D module.
  • ADCS1 and ADCS2 are used to select A/D Conversion Clock. It should be selected in accordance with the device clock. (Bit – 7 to 6).
  • CHS2, CHS1, and CHS0 are used to select one of the analog input channels out of eight channels. (Bit – 5 to 3).
  • GO/DONE is the A/D Conversion Status bit. Setting this bit initializes A/D Conversion and will be automatically cleared when the conversion is complete. (Bit-2).
  • ADON is used to switch on/off the ADC Module. When it is 1, the ADC Module turns ON and when it is 0, the ADC Module will be OFF. (Bit-0).

ADCON1 Register

  • ADCON1 REGISTER is used to configure the function of the port pins.
  • The port pins can be configured as analog inputs (RA3 can also be the voltage reference) or as digital I/O.
  • ADFM is the ADC Result Format select bit. Two 8 bit register (ADRESH and ADRESL) are provided to store the 10-bit result of A/D Conversion, thus we need to use 6 bits. When ADFM is 1, the result will be right-justified, ie Most Significant Bits of ADRESH will be read as 0. When ADFM is 0, the result will be left-justified, ie Least Significant Bits of ADRESL will be read as zero. (Bit 7).
  • ADCS2 is used to select A/D Conversion Clock in association with ADCS1 and ADC2 of the ADCON0 register. (Bit 6).
  • PCFG3 – PCFG0 is the A/D Port Configuration Control bits. Each pin amoung AN0 – AN7 is configured as analog, digital or reference voltage inputs according to the status of these configuration bits as given below.
  • Bit 3-0 PCFG3:PCFG0: A/D Port Configuration Control bits.

A/D Conversion,Follow These Steps:

  • -Configure the A/D module:
    Configure analog pins/voltage reference and digital I/O (ADCON1)
    Select A/D input channel (ADCON0)
    Select A/D conversion clock (ADCON0)
    Turn on the A/D module (ADCON0).
  • Configure A/D interrupt (if desired):
    Clear ADIF bit
    Set ADIE bit
    Set PEIE bit
    Set GIE bit.
  • Wait the required acquisition time.
  • Start conversion:
    Set GO/DONE bit (ADCON0)
  • Wait for A/D conversion to complete by either:
    Polling for the GO/DONE bit to be cleared (interrupts disabled;
    Waiting for the A/D interrupt.
  • Read A/D Result register pair(ADRESH: ADRESL), clear bit ADIF if required.
  • For the next conversion, go to step 1 or step 2 as required. The A/D conversion time per bit is defined as TAD.

Selecting the A/D Conversion Clock

  • The time for A/D Conversion per bit is defined as TAD and it requires a minimum of 12TAD to generate the 10-bit result.
  • The time TAD is determined by the A/D Conversion Clock which is software selectable to the following options.
    1.2 TOSC
    2.4 TOSC
    3.8 TOSC
    4.16 TOSC
    5.32 TOSC
    6.64 TOSC
    7.Internal A/D module RC oscillator (2-6 us).
  • TOSC is the time period of the device clock usually provided by the crystal oscillator.
  • Care should be taken while selecting the A/D Conversion Clock, such that the clock should provide the minimum TAD(1.6uS) required for the correct A/D Conversion.


 unsigned int adc;
 void main()
 ADCON1 = 0x80;
 TRISA = 0xFF; // PORTA is input
 TRISB = 0; // PORTB is output
 // Get 10-bit results of AD conversion
 adc = ADC_Read(1);
 // Send lower 8 bits to PORTB
 PORTB = adc;