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.
- A sample and hold circuit to acquire the input voltage, Vin.
- A voltage comparator that compares Vin to the DAC output. It outputs the comparison result to the successive approximation register (SAR).
- A successive approximation register designed to supply the approximated digital code of Vin to the internal DAC.
- An internal reference DAC that supplies the comparator with an analog voltage equivalent of the digital code supplied by the SAR.
- A register ADRES to store the ADC result upon-end of-conversion(EOC).
REGISTERS FOR ADC USING PIC16F877A
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:
- A/D Control Register 0 (ADCON0)
- A/D Control Register 1 (ADCON1)
- A/D Result High Register (ADRESH)
- A/D Result Low Register (ADRESL)
A/D Control Register 0 (ADCON0)
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.
A/D Control Register 1 (ADCON1)
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
A/D Result High Register (ADRESH) & A/D Result Low Register (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.
STEPS TO PERFORM ADC CONVERSION IN PIC16F877A
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
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
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.
CIRCUIT DIAGRAM FOR ADC USING PIC16F877A
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.
COMPLETE FIRMWARE EXAMPLE FOR ADC USING PIC16F877A