Professional Documents
Culture Documents
The analog to digital converter (ADC) on the 16F877 allows you to measure analog input voltages to
a resolution of 10 bits. An input of Vss will read as 0, while an input of VDD corresponding to the full-
scale reading of 1023. Eight analog input pins are available: AN0:AN7. And there are eight ADC
modules.
Example 1:
Read Analog Voltage of AN0 pin of pic16F877 MCU with Fosc/16 Conversion Clock Speed and display
result on PortB and PortC.
To do an A/D Conversion, follow these steps:
1. Configure the A/D module:
• Configure analog pins/voltage reference and digital I/O (ADCON1)
;select AN0 pin as analog input and other pins as Digital I/O
bcf 0x9F,0
bsf 0x9F,1
bsf 0x9F,2
bsf 0x9F,3
bsf 0x1F,0
4. Start conversion:
• Set GO/DONE bit (ADCON0)
bsf 0x1F,2
6. Read A/D Result register pair (ADRESH:ADRESL), clear bit ADIF if required.
7. For the next conversion, go to step 1 or step 2 as required. The A/D conversion time per bit is
defined as TAD.
Complete code
#include <p16f877.inc>
__CONFIG _CP_OFF & _WDT_OFF & _XT_OSC & _PWRTE_OFF
;*****Variables
T1 equ 0x22 ;delay 1
ADL equ 0x23 ;ADRESL value
ADH equ 0x24 ;ADRESH value
;select AN0 pin as analog input and other pins as Digital I/O
bcf ADCON1,0
bsf ADCON1,1
bsf ADCON1,2
bsf ADCON1,3
movlw b’11001110’
;Select Right Justification Result Format movwf ADCON1
bsf ADCON1,7
;Start AD conversion
Start_ADC bsf ADCON0,2
bsf STATUS,5
movf ADRESL,0 ; copy ADRESL result to W register
bcf STATUS,5
movwf ADL ;copy W register value to ADL variable
Dinusha Gunathilaka
--------------------------------------------------------------------------------------------------------------------------------------