list P=16F877A
#include <P16F877A.inc>
errorlevel -302
__config _CP_OFF & _PWRTE_ON & _XT_OSC & _WDT_OFF & _BODEN_OFF & _LVP_OFF
cblock 20h
Digit1,Digit2,Digit3,ADL,lsd,msd,bigmsd,Del
endc
org 0000h
clrf STATUS
Init bsf STATUS,RP0
clrf TRISB
clrf TRISD
movlw b'00000001' ;make RA0 input (sensor)
movwf TRISA
movlw b'10001110' ;result right justified,Vref+=Vdd,Vref-=Vss
movwf ADCON1
bcf STATUS,RP0
movlw b'01000001' ;ADON,RA0 Channel,FOSC/8
movwf ADCON0
clrf ADL
clrf Digit1
clrf Digit2
clrf Digit3
clrf PORTB
clrf PORTD
clrf PORTA
goto Main
;************
;Main program
;************
Main call Multiplex ;show the display
call Check_AD ;get the AD value
bcf STATUS,0
[COLOR="Red"][B]rrf ADL,F ;devide the AD value by by 2[/B][/COLOR]
call BCD ;call BCD & make into decimals
call BCD_Split ;move BCD results to segment digits
goto Main
;**********************************
;move BCD results to segment digits
;**********************************
BCD_Split movf lsd,W
movwf Digit1
movf msd,W
movwf Digit2
movf bigmsd,W
movwf Digit3
return
;**********************
;Read AD value routine
;**********************
Check_AD clrf ADL
bsf ADCON0,GO
btfsc ADCON0,GO
goto $-1
bsf STATUS,RP0
movf ADRESL,W
bcf STATUS,RP0
movwf ADL
return
;****************************
;8 bit to 3 digit BCD version
;****************************
BCD movf ADL,W
clrf msd ;result register1
clrf lsd ;result register2
clrf bigmsd ;result register3
call convert_bcd
movf msd,W
call convert_big
return
convert_bcd clrf msd ; clear the msd
movwf lsd ; move the number to convert into lsd
ten movlw .10 ; load the constant 10
subwf lsd,w ; subtract 10 from lsd
btfss STATUS,0 ; check if answer negative
goto done ; conversion complete, jump to done
movwf lsd ; save the answer back in lsd and
incf msd,F ; increment the msd
goto ten ; do the routine again
done return ; return to caller
convert_big clrf bigmsd ; clear hundreds
movwf msd ; move the hundred number to msd
hundred movlw .10 ; load the constant 10
subwf msd,w ; subtract 10 from msd
btfss STATUS,0 ; check if answer negative
goto hundreddone ; conversion complete, jump to hundreddone
movwf msd ; save the answer back in msd and
incf bigmsd,F ; increment the bigmsd
goto hundred ; do the routine again
hundreddone return ; return to caller
;*******************************************************
;Multiplex routine 3 digits (last digit ignoring-Digit3)
;*******************************************************
Multiplex movlw 10h
movwf PORTD
movf Digit3,W
call Delay
movf Digit2,W
call Delay
movf Digit1,W
goto Delay
Delay call Table
movwf PORTB
decfsz Del,F
goto $-1
clrf PORTB
bcf STATUS,C
rlf PORTD,F
return
;***************************************
;Data look up table for segment patterns
;***************************************
Table addwf PCL,F
retlw b'01111110' ;0
retlw b'01100000' ;1 ;dp cbdeafg
retlw b'00111101' ;2
retlw b'01110101' ;3
retlw b'01100011' ;4
retlw b'01010111' ;5
retlw b'01011111' ;6
retlw b'01100100' ;7
retlw b'01111111' ;8
retlw b'01110111' ;9
end