Microchip PIC16F877A Analogue to Digital Conversion

16f877a Adc Banner
As mentioned in the previous post, the PIC16F877A has a built-in Analogue to Digital converter. What ADC does is basically convert an analogue voltage ranging from -Vref to +Vref (usually 0V to 5V) and converts it to a binary value. The ADC on the PIC16F877A has 10-bit resolution and this provides 1024 steps (2^10=1024) which is more than enough for normal applications.

This is how I connected the circuit on a breadboard :

What I had in mind to display the results of the ADC is to have 5 LEDs and the higher the input voltage, the more LEDs will light up. I have a 10k potentiometer to vary the input voltage going into PORTA,0 which is also Analogue Channel 0.

For eagled eyed viewers, you might notice that I have another resistor (550 ohms) before the 10k pot and I’ve done that to prevent excessive current from going into PORTA,0 when the pot has 0 resistance. This will indeed affect the max voltage going into PORTA,0 and it’s no longer 5V so I should set a different +Vref value. But I couldn’t be bothered because the voltage drop on the 550 ohms resistor is just 0.26V. I don’t need accuracy here anyway.

So let’s get to the initialization.

Bank 1

TRISA,0 is set to allow input for Analogue Channel 0
TRISD is cleared to allow output to LEDs

ADCON1 is cleared. Result is right justified because I’ll just take the higher 8 bits for simpler calculation. This will not affect the accuracy much. I wanted clock conversion to be Fosc/8 to get > 1.6us of minimum conversion time (I’m using a 4MHz crystal here). AN0 to AN7 is set to Analogue mode. +Vref is 5V and -Vref is 0V.

Bank 0

ADCON0 is set to ‘0100 0000’ to select Fosc/8 and Analogue Channel 0.

That’s about it. And the rest of the code can be seen below.


The video below will show how the code works.

Print Friendly
  1. shane05-01-2013

    hello..can u plz sent me the full programming code in microbasic language?
    thanks 🙂

Leave a Reply