RS BIT P3.1 ; LCD
RW BIT P3.2 ; Constants
E BIT P3.0 ;
BZ BIT P1.7 ; busy flag
org 0h ; reset vector
sjmp Start
org 20h ; jump over vectors
;
; Start of code
;---------------------------------
Start:
acall LcdInit ; called once only
While:
acall LcdLine1 ; Goto Line1
mov DPTR,#MSG1
acall LcdPrint ; Display message
acall LcdLine2 ; goto Line2
mov DPTR,#MSG2 ;
acall LcdPrint ; Display message
sjmp While ; forever loop
MSG1:
db "First Line ",0
MSG2:
db "Second Line ",0
;
; Initialize LCD
;---------------------------------
LcdInit:
clr RW
acall delay40Ms
mov P1,#33h ; Init once
setb E
clr E
acall delayMs ; Init twice
setb E
clr E
acall delayMs ; Now busy can be read.
mov a,#38h ; Function set: 8 bits: 2 Line : font 5x7.
acall LcdCom
mov a,#0Ch ; Display On: Cursor Off : Blink Off.
acall LcdCom
mov a,#06h ; Shift forward.
acall LcdCom
mov a,#01h ; Clear Goto position 1.
acall LcdCom
ret
;
; Goto first line
;---------------------------------
LcdLine1:
mov A,#80h ; position 1 on line 1
acall LcdCom
ret
;
; Goto second line
;---------------------------------
LcdLine2:
mov A,#0C0h ; position 1 on line 2
acall LcdCom
ret
;
; Print a null terminated string
;---------------------------------
LcdPrint:
mov A,#0h
movc A,@A+DPTR ; Through string until 0
cjne A,#0,Char ;
ret
Char:
acall LcdData ; Print data character
inc dptr
sjmp LcdPrint
;
; Waiting for LCD module
;---------------------------------
LcdBusy:
mov P1,#0FFh ; tristate the port
clr RS ; command mode
setb RW ; Read flags
busy: clr E
setb E
nop
jb BZ, busy ; wait until ready
clr E
clr RW ; Back to write mode
ret
;
; Send command to LCD module
;---------------------------------
LcdCom: ; sends a command
acall LcdBusy
clr RW
clr RS
mov P1,A ; Acc has command
setb E
clr E
ret
;
; Send data to LCD module
;---------------------------------
LcdData: ; Sends data
acall LcdBusy
clr RW
setb RS
mov P1,A ; Acc has data
setb E
clr E
ret
;
; Delays
;---------------------------------
delay40Ms:
mov R1,#25h ; Simple delay @ 40 milli seconds
dlyM: acall delayMs
djnz R1,dlyM
ret
delayMs:
mov R0,#0h ; Simple delay @ 1.1 milli second
dlyU: nop
nop
djnz R0,dlyU
ret
end
[code]
We'll do some interrupts later..