;================================================= ===========
; File name:analog to digital conversion on LCD
; Date:
; Author:kgavionics
; Processor:pic16f877a
; Reference circuit:
;================================================= ===========
; Copyright notice:
;================================================= ===========
; Program Description:
;
;===========================
; configuration switches
;===========================
list p=16f877A ; list directive to define processor
#include <p16f877A.inc> ; processor specific variable definitions
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF
;================================================= ====
; constant definitions
;================================================= ====
;================================================= ====
; PIC register equates
;================================================= ====
;================================================= ====
; variables in PIC RAM
;================================================= ====
cblock 0x20
BIN : 1
count : 1
huns : 1
tens : 1
ones : 1
counter1 : 1
counter2 : 1
number : 1
timer : 1
endc
;================================================= ===========
; program
;================================================= ===========
org 0X00 ; start at address
goto main
; Space for interrupt handlers
org 0x08
BIN2BCD
;in: BIN
;out huns. tens, ones
;uses ADD-3 algoerthm
movlw 8
movwf count
clrf huns
clrf tens
clrf ones
BCDADD3
movlw 5
subwf huns, 0
btfsc STATUS, C
CALL ADD3HUNS
movlw 5
subwf tens, 0
btfsc STATUS, C
CALL ADD3TENS
movlw 5
subwf ones, 0
btfsc STATUS, C
CALL ADD3ONES
decf count, 1
bcf STATUS, C
rlf BIN, 1
rlf ones, 1
btfsc ones,4 ;
CALL CARRYONES
rlf tens, 1
btfsc tens,4 ;
CALL CARRYTENS
rlf huns,1
bcf STATUS, C
movf count, 0
btfss STATUS, Z
GOTO BCDADD3
movf huns, 0 ; add ASCII Offset
addlw h'30'
movwf huns
movf tens, 0 ; add ASCII Offset
addlw h'30'
movwf tens
movf ones, 0 ; add ASCII Offset
addlw h'30'
movwf ones
RETURN
ADD3HUNS
movlw 3
addwf huns,1
RETURN
ADD3TENS
movlw 3
addwf tens,1
RETURN
ADD3ONES
movlw 3
addwf ones,1
RETURN
CARRYONES
bcf ones, 4
bsf STATUS, C
RETURN
CARRYTENS
bcf tens, 4
bsf STATUS, C
RETURN
delay
movlw 0x0b
movwf counter2
loop1
movlw 0xff
movwf counter1
loop2
nop
decfsz counter1
goto loop2
decfsz counter2
goto loop1
return
instw
movwf PORTB
call delay
bcf PORTD,4
call delay
bsf PORTD,5
call delay
bcf PORTD,5
call delay
return
dataw
movwf PORTB
call delay
bsf PORTD, 4
call delay
bsf PORTD, 5
call delay
bcf PORTD, 5 ;Transitional E signal
call delay
return
sampledelay
movlw 100
movwf timer
loop12
decfsz timer
goto loop12
return
convert
call sampledelay ; provide necessary sampling time
bsf ADCON0,GO ; start conversion
loop14
btfsc ADCON0,GO ; is the conversion over?
goto loop14 ;loop
movf ADRESH,W ; STORE conversion value to W
movwf BIN
return
main:
bsf STATUS,RP0
movlw 0x00
movwf TRISB
movwf TRISD
movlw B'00001000'; ADCON1 VALUE
movwf ADCON1 ;ADCON1 SET
bcf STATUS,RP0
clrf PORTD
clrf PORTB
InitializeAD
movlw B'10001001' ; value to be set to ADCON0
movwf ADCON0 ; ADCON0 SET
;call delay
;call delay
start
clrf PORTD ;Here RW is pulled down to ground
;LCD routine starts
call delay
;give LCD module to reset automatically
;Fundtion for 8-bit, 2-line display, and 5x8 dot matrix
call BIN2BCD
movlw 0x38
call instw
call delay
movlw 0x0E
call instw
call delay
movlw 0x01
call instw
call delay
movlw 0x41
call dataw
movlw 0x44
call dataw
movlw 0x43
call dataw
movlw 0x20
call dataw
movlw 0x43
call dataw
movlw 0x4f
call dataw
movlw 0x4e
call dataw
movlw 0x56
call dataw
movlw 0x45
call dataw
movlw 0x52
call dataw
movlw 0x53
call dataw
movlw 0x49
call dataw
movlw 0x4f
call dataw
movlw 0x4e
call dataw
kais
movlw 0xc0
call instw ; change the cursor to 2nd line
call delay
call convert
call BIN2BCD
movf huns,w
call dataw
movf tens,w
call dataw
movf ones,w
call dataw
goto kais
;================================================= ===========
end ; END OF PROGRAM
;================================================= ===========