list p=18F1320
include <p18F1320.inc>
CONFIG OSC=INTIO2,WDT=OFF,MCLRE=ON,LVP=OFF
cblock 0x00
d1,d2,d3,dataout,temp,dec1,dec2,vr1,vr2
endc
org 0x0000
init bsf OSCCON,IRCF2 ;set to 8MHz clock
bsf OSCCON,IRCF1
bsf OSCCON,IRCF0
movlw b'00001010'
movwf TRISA
clrf TRISB
clrf LATA
clrf LATB
call dl_40ms ;allow lcd to settle before init
call lcdinit ;init lcd
movlw b'01110101' ;init a/d
movwf ADCON1
movlw b'00011101'
movwf ADCON2
main movlw HIGH table1 ;display table1
movwf TBLPTRH
movlw LOW table1
movwf TBLPTRL
oneloop tblrd *+
movf TABLAT,W
xorlw 0x00 ;zero?
btfsc STATUS,Z
goto print1 ;yes, done
movwf dataout ;no, go again
call lcdchar
goto oneloop
print1 call analog
movf vr1,W
call fixhex
movff dec1,dataout
call lcdchar
movff dec2,dataout
call lcdchar
call lcd_line2
linetwo movlw HIGH table2 ;display table2
movwf TBLPTRH
movlw LOW table2
movwf TBLPTRL
twoloop tblrd *+
movf TABLAT,W
xorlw 0x00 ;zero?
btfsc STATUS,Z
goto print2 ;yes, done
movwf dataout ;no, go again
call lcdchar
goto twoloop
print2 movf vr2,W
call fixhex
movff dec1,dataout
call lcdchar
movff dec2,dataout
call lcdchar
call delay
call lcd_line1
goto main
table1 db "VR1 - $",0x00
table2 db "VR2 - $",0x00
analog movlw b'00000101' ;enable a/d - an1
movwf ADCON0
bsf ADCON0,GO ;go do a/d conversion
adloop btfsc ADCON0,DONE
goto adloop
movff ADRESH,vr1 ;store in vr1
movlw b'00001101' ;enable a/d - an3
movwf ADCON0
bsf ADCON0,GO ;go do a/d conversion
adloop2 btfsc ADCON0,DONE
goto adloop2
movff ADRESH,vr2 ;store in vr2
return
;*****************************************************************
; fixhex - subroutine takes byte passed in W - splits it into two
; ascii bytes representing hex digits and stores them in dec1/dec2
fhex db "0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"
fixhex movwf temp ;store it for later
movwf dec1 ;and put it in dec1 too
movlw 0xf0 ;mask out low nybble
andwf dec1
swapf dec1,F ;swap nybbles
movlw HIGH fhex ;set up table pointer
movwf TBLPTRH
movlw LOW fhex
movwf TBLPTRL
movf dec1,W
addwf TBLPTRL ;add offset
btfsc STATUS,C ;was there a carry?
incf TBLPTRH,F ;yes, increment table pointer MSB
tblrd * ;read the byte
movff TABLAT,dec1 ;store it in dec1
fixhx2 movlw 0x0f ;mask out high nybble
andwf temp,F
movlw HIGH fhex ;set up table pointer
movwf TBLPTRH
movlw LOW fhex
movwf TBLPTRL
movf temp,W
addwf TBLPTRL ;add offset
btfsc STATUS,C ;was there a carry?
incf TBLPTRH,F ;yes, increment table pointer MSB
tblrd * ;read the byte
movff TABLAT,dec2
return
lcd_line1
movlw 0x80
movwf dataout
call lcdcmd
return
lcd_line2
movlw 0xc0
movwf dataout
call lcdcmd
return
lcdcmd swapf dataout,W ;swap nybbles and transfer to w
andlw 0x0f ;mask out high nybble
movwf temp ;rotate left 2 bits for B2-B5
rlncf temp,F
rlncf temp,W
movwf LATB ;move to PORTB
call e_togg ;latch the data
movf dataout,W ;get byte in w again
andlw 0x0f ;and mask out high nybble
movwf temp ;rotate data into position
rlncf temp,F
rlncf temp,W
movwf LATB ;move to PORTB
call e_togg ;latch the data
call dl_50us
return
lcdchar swapf dataout,W ;swap nybbles and transfer to w
andlw 0x0f ;mask out high nybble
movwf temp ;rotate data into position
rlncf temp,F
rlncf temp,W
movwf LATB ;move to PORTB
bsf LATB,0 ;set RS high
call e_togg ;latch the data
movf dataout,w ;get byte in w again
andlw 0x0f ;and mask out high nybble
movwf temp ;rotate data into position
rlncf temp,F
rlncf temp,W
movwf LATB ;move to PORTB
bsf LATB,0 ;RS high
call e_togg ;latch the data
call dl_50us
return
e_togg bsf LATB,1 ;toggle E to latch data
nop ;delay 450ns minimum
nop
nop
bcf LATB,1
return
lcdinit movlw b'00001100' ;send 03 nybble
movwf LATB
call e_togg
call dl_05ms ;wait 5ms
movlw b'00001100' ;send 3 second time
movwf LATB
call e_togg
call dl_160us ;wait 160us
movlw b'00001100' ;send 3 third time
movwf LATB
call e_togg
call dl_160us ;wait 160us
movlw b'00001000' ;enable 4-bit mode ($02 nybble)
movwf LATB
call e_togg
call dl_50us ;wait 50us
movlw 0x28 ;set 4-bit mode and 2-lines
movwf dataout
call lcdcmd
call dl_50us ;wait 50us
movlw 0x10 ;cursor move & shift left
movwf dataout
call lcdcmd
call dl_50us ;wait 50us
movlw 0x06 ;entry mode = increment
movwf dataout
call lcdcmd
call dl_50us ;wait 50us
movlw 0x0d ;display on, cursor on, blink on
movwf dataout
call lcdcmd
call dl_50us ;wait 50us
movlw 0x01 ;clear display
movwf dataout
call lcdcmd
call dl_05ms ;wait 5ms
return
dl_40ms movlw 0x7e ;40ms delay
movwf d1
movlw 0x3f
movwf d2
dl40_0 decfsz d1,F
goto $+6
decfsz d2,F
goto dl40_0
nop
return
dl_05ms movlw 0xce ;5ms delay
movwf d1
movlw 0x08
movwf d2
dl05_0 decfsz d1,F
goto $+6
decfsz d2,F
goto dl05_0
return
dl_160us ;160us delay
movlw 0x69
movwf d1
dl160_0 decfsz d1,F
goto dl160_0
return
dl_50us movlw 0x1f ;50us delay
movwf d1
dl50_0 decfsz d1,F
goto dl50_0
return
delay movlw 0x5e ;.15 second delay
movwf d1
movlw 0xeb
movwf d2
delay_0 decfsz d1,F
goto $+6
decfsz d2,F
goto delay_0
return
end