In this tutorial, we will learn how to use the Analog to Digital converter peripheral of the PIC16F877A controller. Basically we use this kind of peripherals in the modem. It is a 10 bit ADC module. ADC module of the PIC16F877A controller has a 10-bit resolution output. That means an analog input converted into a corresponding 10-bit digital output and 7 channel ADC.
This is the 10-bit Successive Approximation block diagram.
Most of the configurations to set up the ADC has to be done in the ADCON register. The ADCON register is the Control Register of the ADC. The ADC conversion is given in the user manual of PIC16F877A. Under the register description, you will notice the various bits associated with the ADCON register. The following are the necessary configurations of the bits in the ADCON register which we will be using for the ADC setup.
A/D has 4 main registers:
This register uses to select input channels and control conversion process. It also controls the operation of A/D Converter.
ADON bit: ADCON0 needs to be configured for enabling the ADC peripheral ().
GO bit: The register starts conversion of the input analog signal when set. Bit is also used as A/D conversion status bit.
CHS2:CHS0: bits are the channel select bits from 7(0-7) variable channels which are multiplexed with digital I/O pins.
ADCS1:ADCS0: Configures the clock option for ADC peripheral. The system clock is divided to configure the clock for AD conversion configurations for different clocks that can be obtained from the datasheet.
This register is used to set the port A/E pins to read analog inputs/reference.
PCFG0:PCFG3: Bits in ADCON1 determines which pins of the controller should be configured as analog inputs. We have seen that the ADC module has 7 channels which are multiplexed with the I/O pins. Configuration of pins as digital or analog can be achieved through configuring these bits in ADCON1.
ADFM: This bit is used to select the format by which result should be stored int the ADRESH and ADRESL
ADRESH(High byte) and ADRESL(Low byte)Registers are used to store the converted data.i.e,Digital data.But the data is 10bit wide, the remaining six-bit are not used.
Configure the ADC module. Configuring analog pin, voltage reference, and digital I/O. Select A/D acquisition time and conversion clock, output result format. Right Justified
ADCON0=0x00; ADCON1=(1<<SBIT_ADFM);
Turn on the ADC by setting ADON bit
ADCON0 = (1<<SBIT_ADON) | (adcChannel<SBIT_CHS0);
Wait for the required acquisition time. Start the conversion by setting ADON/GO bit. Wait for A/D conversion to complete. As long as the GO_DONE bit HIGH, the conversion is not complete.
GO=1; // Start ADC conversion while(GO_DONE==1); return((ADRESH<<8) + ADRESL);
Read the result from the ADRES register.
return((ADRESH<<8) + ADRESL); // return right justified 10-bit result
Repeat the start conversion section if it needs further conversion.
Here, the circuit diagram for Analog to Digital converter using PIC16F877A Microcontroller. A potentiometer is directly connected to RA0 pin, which is used as a voltage divider to provide varies voltages to the ADC pin.