As an Amazon Associate we earn from qualifying purchases.
The PIC16F877A has a built-in 10-bit analog-to-digital converter (ADC) that can read up to eight analog inputs, AN0 to AN7. This article shows how to set it up in HI-TECH C, explains what each register bit does, and walks through a working example that reads AN0 and shows the 10-bit result on PORTB and PORTC.
If you prefer another compiler, there is also a MikroC version of this ADC example on the site.
How the PIC16F877A ADC works
The ADC compares the voltage on a selected pin against a reference and returns a number from 0 to 1023. With the default reference (VDD and VSS), the result is proportional to the input voltage between ground and the supply.
Two control registers set up the module:
- ADCON0 holds the clock select bits ADCS1:ADCS0, the channel select bits CHS2:CHS0, the GO/DONE bit that starts a conversion, and the ADON bit that turns the module on.
- ADCON1 holds ADFM (result justification), ADCS2 (the third clock select bit) and PCFG3:PCFG0, which decide which pins are analog and which are digital.
The 10-bit result is stored in two registers, ADRESH and ADRESL. Which bits land where depends on ADFM.
The original example
This program selects AN0 as the input, runs the conversion repeatedly, and shows the result on PORTC (high byte) and PORTB (low byte).
//in this i have set AN0 as input ADC port
// value after analog to digital conversion is stored in a variable and
// also shown at portc and portb in combined form(10 bit)
#include<pic.h>
#define _XTAL_FREQ 16000000
__CONFIG(0x3f7a);
int a;
void main()
{
TRISB=0;//setting port B as output
PORTB=0;
TRISC=0;//setting port C as output
PORTC=0;
TRISA=255;//setting the PORTA as input
ADON=1;//enables ADC
ADCS0=0;//setting the clock for ADC
ADCS1=0;//setting the clock for ADC
ADCS2=0;//setting the clock for ADC
ADFM=1;//sets the higher bits of conversion to right justified
PCFG3=0;//Configuring 8 pin as adc input channels
PCFG2=0;//Configuring 8 pin as adc input channels
PCFG1=0;//Configuring 8 pin as adc input channels
PCFG0=0;//Configuring 8 pin as adc input channels
CHS2=0;
CHS1=0;
CHS0=0;
while(1)
{
__delay_ms(100);
a=(ADRESH<<8)+ADRESL; //this only for further data processing
PORTC=ADRESH; //higher data byte
PORTB=ADRESL; //lower data byte
GO=1; // to restart conversion
}
}
What each part does
Port setup
PORTB and PORTC are set as outputs so you can see the result on LEDs. TRISA is set to 255, which makes all PORTA pins inputs. AN0 is on RA0, so that is where you connect the signal, for example the wiper of a potentiometer. If you want to read and write regular pins as well, see the post on digital output and input.
Analog pin configuration (PCFG)
Setting PCFG3:PCFG0 to 0000 makes all eight channels analog and uses VDD and VSS as the reference voltages. Other combinations turn some pins into digital I/O or use external reference pins, so check the table in the datasheet if you need that.
Channel select (CHS)
CHS2:CHS0 = 000 selects channel 0 (AN0). To read AN1, set CHS0 to 1; for AN2, set CHS1 to 1, and so on.
Result justification (ADFM)
With ADFM = 1 the result is right-justified. ADRESH holds only the top two bits (bits 8 and 9) and ADRESL holds the lower eight bits. That is why the code combines them with (ADRESH<<8)+ADRESL. With ADFM = 0 the result is left-justified, which is handy if you only need 8-bit resolution: just read ADRESH.
Two things to fix in the original code
1. Choose a valid ADC clock
The original sets ADCS2:ADCS0 to 000, which is Fosc/2. At 16 MHz that gives a conversion clock period (TAD) far shorter than the minimum the datasheet allows, which is around 1.6 µs. Results can be inaccurate or wrong. At 16 MHz, Fosc/32 (ADCS2 = 0, ADCS1 = 1, ADCS0 = 0) gives a 2 µs TAD and is a safe choice.
2. Start a conversion before reading the result
In the original loop the result is read before any conversion has been started, so the first value is meaningless. The conversion also takes time, so it is better to start it, wait for the GO bit to clear, and then read ADRESH and ADRESL.
Improved example
#include<pic.h>
#define _XTAL_FREQ 16000000
__CONFIG(0x3f7a);
unsigned int a;
void main()
{
TRISB=0;
PORTB=0;
TRISC=0;
PORTC=0;
TRISA=255;
ADCON1=0x80; // ADFM=1 (right justified), ADCS2=0, all 8 channels analog
ADCON0=0x81; // ADCS1:ADCS0=10 (Fosc/32), channel 0, ADON=1
while(1)
{
__delay_us(20); // acquisition time
GO=1; // start conversion
while(GO); // wait until it finishes
a=((unsigned int)ADRESH<<8)|ADRESL;
PORTC=ADRESH; // upper 2 bits
PORTB=ADRESL; // lower 8 bits
__delay_ms(100);
}
}
Here the registers are written as whole bytes. ADCON1 = 0x80 sets ADFM and leaves PCFG at 0000. ADCON0 = 0x81 selects Fosc/32 with ADCS1:ADCS0 = 10, channel 0, and turns the module on.
Converting the result to a voltage
With VDD as the reference, the input voltage is:
Vin = ADC value × Vref / 1024
On a 5 V supply, each step is about 4.88 mV. A reading of 512 is therefore about 2.5 V. For floating-point maths on a PIC16, you can use integer maths instead, for example multiplying by 5000 and dividing by 1024 to get millivolts.
Tips for stable readings
- Keep the signal source impedance low, ideally 10 kΩ or less, so the sampling capacitor charges fully.
- Add a small capacitor close to the input pin and decoupling capacitors on the supply to reduce noise.
- Average several samples if the value jumps around.
- Do not feed more than the supply voltage into an analog pin.
- If you want to send the result over UART, read the value into a variable as shown above and convert it to text before transmitting. For regular sampling, use timer-based sampling instead of long delays.
FAQ
How many ADC channels does the PIC16F877A have?
It has eight analog channels, AN0 to AN7, spread across PORTA and PORTE. They share one converter, so only one channel is read at a time.
What is the resolution of the PIC16F877A ADC?
10 bits, which gives values from 0 to 1023.
Why do I only see two bits on PORTC?
With right-justified results, ADRESH holds only bits 8 and 9 of the 10-bit value. The other eight bits are in ADRESL, shown on PORTB in this example.
Can I use an external reference voltage?
Yes. Certain PCFG settings let you use AN2 and AN3 as VREF- and VREF+. Check the PCFG table in the datasheet for the exact combinations.