You are on page 1of 6

Analog to Digital Conversion

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.

The A/D module has four registers. These registers are:


• A/D Result High Register (ADRESH)
• A/D Result Low Register (ADRESL)
• A/D Control Register 0 (ADCON0)
• A/D Control Register 1 (ADCON1)

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

• Select A/D Result Format (ADCON1)

;Select Right Justification Result Format


bsf 0x9F,7
 Select A/D conversion clock (ADCON0 & ADCON1)

;select ADC clock Speed Fosc/16 ADCON0 bits


bsf 0x1F,6
bcf 0x1F,7

;select ADC clock Speed Fosc/16 ADCON1 bits


bsf 0x9F,6

• Select A/D input channel (ADCON0)

;select ADC channel 0


bcf 0x1F,3
bcf 0x1F,4
bcf 0x1F,5
• Turn on A/D module (ADCON0)

bsf 0x1F,0

2. Configure A/D interrupts (if desired):


• Clear ADIF bit
• Set ADIE bit
• Set PEIE bit
• Set GIE bit
3. Wait the required acquisition time (19.72 us).

;set 20us time delay ( 4MHz Clock speed )


movlw D’20’ ;copy 20 to w register
movwf 0x22 ;copy w register to 20h register ( variable register)
Delay1 decfsz 0x22,1 ; decrease 20h register until 0 and write new value 20h reg it self
goto Delay1

4. Start conversion:
• Set GO/DONE bit (ADCON0)

bsf 0x1F,2

5. Wait for A/D conversion to complete by either:


• Polling for the GO/DONE bit to be cleared (interrupts disabled); OR
• Waiting for the A/D interrupt

; wait until complete AD conversion


ADcon btfsc 0x1F,2
Goto ADcon

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

;**** Registers Define***********


ADCON0 equ 0x1F
ADCON1 equ 0x9F
STATUS equ 0x03
ADRESL equ 0x9E
ADRESH equ 0x1E
TRISB equ 0x86
TRISC equ 0x87
PORTB equ 0x06
PORTC equ 0x07

;*****Variables
T1 equ 0x22 ;delay 1
ADL equ 0x23 ;ADRESL value
ADH equ 0x24 ;ADRESH value

bsf STATUS,5 ; jump to bank1

;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

;select ADC clock Speed Fosc/16 ADCON1 bits


bsf ADCON1,6

clrf TRISB ;set port B as output


clrf TRISC ;set port C as output

bcf STATUS,5 ; jump to bank1

;select ADC clock Speed Fosc/16 ADCON0 bits


bsf ADCON0,6
bcf ADCON0,7

;select ADC channel 0 movlw b’10000001’


bcf ADCON0,3 movwf ADCON0
bcf ADCON0,4
bcf ADCON0,5
; Turn on ADC module
bsf ADCON0,0
;set 20us time delay ( 4MHz Clock speed )
movlw D'20' ;copy 20 to w register
movwf T1 ;copy w register to T1 variable register
Delay1 decfsz T1,1 ; decrease T1 until 0 and write new value T1 it self
goto Delay1

;Start AD conversion
Start_ADC bsf ADCON0,2

; wait until complete AD conversion


ADcon btfsc ADCON0,2
Goto ADcon

;Read ADC Result registers


movf ADRESH,0 ; copy ADRESL result to W register
movwf ADH ;copy W register value to ADL variable

bsf STATUS,5
movf ADRESL,0 ; copy ADRESL result to W register
bcf STATUS,5
movwf ADL ;copy W register value to ADL variable

;Display result on Port B and port C


movf ADL,0
movwf PORTB
movf ADH,0
movwf PORTC

goto Start_ADC ;go back to next convention


end

Dinusha Gunathilaka
--------------------------------------------------------------------------------------------------------------------------------------

You might also like