PIC16F877 ADC code and Proteus simulation
This post provides the ADC code using PIC16F877 microcontroller. This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the ‘Downloads‘ section at the bottom of this page.
It is assumed that you know how to interface LCD with PIC16F877 microcontroller. If you don’t then please read this page first, before proceeding with this article.
The result of simulating the code in Proteus is shown below.
In the above figure, pin10(AN7) of PIC16F877 is being used as ADC input pin. The result of ADC conversion is shown on the LCD screen. If you attach pin10 of PIC16F877 to ground, then a value of 0000 will be displayed on the LCD screen and if you attach pin10 with 5v power supply, then a value of 1023 will be displayed on the LCD screen. In other words, ADC resolution is 10bits and value of ADC result varies from 0(0v) to 1023(5v). Because there is a variable resistor of 50% value attached on pin10 of PIC16F877 in Figure1, that is why it has 2.5v on it. As a result, a value of 511(Half of 1023) is being displayed on the LCD.
There are 8 ADC input channels from AN0 (pin2) to AN7 (pin10). You can use any ADC channel as an input channel for ADC conversion. You can select AN0 or AN1 or AN2 or AN3 or AN4 or AN5 or AN6 or AN7 channel. In the code I have given a simple channel selection method (Explained below in the code section). I have selected VREF+ equal to VDD and VREF- equal to GND (Of-course you can change this if you want).
The main function code is shown below.
In the main function, ADC is initialized for all ADC input pins (from AN0 to AN7) using InitADC() function. After ADC, LCD is initialized using InitLCD() function. After that, in the while loop code reads ADC value from AN7 channel after every half second and converts this integer value into 4 digits. And those digits are displayed on the LCD screen. In this way, after every half second new ADC value is updated on the LCD screen.
The code used to initialize ADC input channels is shown below.
InitADC() function should be called in the start of main function to initialize ADC input channels. This function declares PORTA and PORTE pins as analog pins, meaning that all of them can be used as ADC input pins and they are not digital pins anymore. You can change this setting if you need more digital pins.
The function used to read ADC value from input pin is shown below.
To read ADC value from AN0 channel, you can write GetADCValue(AN0); in your code. Similarly, you can read ADC value from all other channels as well. You can read ADC value only from one channel at a time. GetADCValue() function returns the 10bit ADC value.
You can use this ADC code library in your projects. You can leave your comments in the comment section below.
Notes and References
 Besides pin10(AN7), you can select pin9(AN6) or pin8(AN5) or pin7(AN4) or pin5(AN3) or pin4(AN2) or pin3(AN1) or pin2(AN0) as ADC input pin.
ADC code using PIC16F877 was compiled in MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10. To download code and Proteus simulation click here.
Current Project / Post can also be found using:
- temperature sensor with pic16f877a with hi tech c code pdf
- pic16f877a lcd mikroc code pdf
- simulation pic 16F88 protéus
- adc interfacing with pic16f877a using ptoteus