;4 bit code tested & working
;
;LCD is connected to MCU in following way:
;D4 - P3.0
;D5 - P3.1
;D6 - P3.2
;D7 - P3.3
;EN - P3.7
;RS - P3.5
;RW connected to GND (only write operations)
;
$MOD51
;
lcd_port equ P3 ;LCD connected to Port3
EN equ P3.7 ;Enable connected to P3.7
RS equ P3.5 ;Register select to P3.5
;
;-----------------------------------------------------
dcount equ 08h ;
bytecount equ 09h ;
temp equ 0Ah ;
;
ORG 0 ;
Reset:
sjmp Start ;
Start:
MOV SP,#06FH ;
MOV A,#0FFH ;
MOV P0,A ;
MOV P1,A ;
MOV P2,A ;
MOV P3,A ;
call lcd_reset
call lcd_init ;
Main:
mov dptr,#message1 ;Point to message
lcall WrLCD ;Transmit message
mov a,#0C0H ;move cursor to 2nd line
call lcd_cmd ;
mov dptr,#message2 ;Point to message
lcall WrLCD ;Transmit message
stop:
sjmp stop
;----------------------------------------------------------
message1:
db 'Hello There! '
message2:
db 'How Are You? '
;--------------Writes Text to LCD display --------------------
WrLCD: ;
mov bytecount,#16
WrLCD1LP:
CLR A
MOVC A,@A+DPTR ;get data to send to LCD
INC DPTR
call lcd_dat
djnz bytecount,WrLCD1LP
RET ;
;------------------------------------------------------------------
lcd_reset: ;LCD reset sequence
mov lcd_port, #0FFH
mov dcount,#20 ;20mS delay
acall delay
mov lcd_port, #83H ;Data = 30H, EN = 1, First Init
mov lcd_port, #03H ;Data = 30H, EN = 0
mov dcount,#15 ;Delay 15mS
acall delay
mov lcd_port, #83H ;Second Init, Data = 30H, EN = 1
mov lcd_port, #03H ;Data = 30H, EN = 0
mov dcount,#5 ;Delay 5mS
acall delay
mov lcd_port, #83H ;Third Init
mov lcd_port, #03H
mov dcount,#5 ;Delay 5mS
acall delay
mov lcd_port, #82H ;Select Data width (20H for 4bit)
mov lcd_port, #02H ;Data = 20H, EN = 0
mov dcount,#5 ;Delay 5mS
acall delay
ret
;------------------------------------------------------------------
lcd_init:
acall lcd_reset ;Call LCD Reset sequence
mov a,#28H ;4-bit, 2 line, 5x7 dots
acall lcd_cmd ;Call LCD command
mov a,#0CH ;Display ON cursor OFF
acall lcd_cmd ;Call LCD command
mov a,#06H ;Set entry mode (Auto increment)
acall lcd_cmd ;Call LCD command
mov a,#80H ;Bring cursor to line 1
acall lcd_cmd ;Call LCD command
ret
;------------------------------------------------------------------
lcd_cmd: ;LCD command Routine
mov temp,a ;Save a copy of command to temp
swap a ;Swap to use higher nibble
anl a,#0FH ;Mask the first four bits
add a,#80H ;Enable = 1, RS = 0
mov lcd_port,a ;Move it to lcd port
anl a,#0FH ;Enable = 0, RS = 0
mov lcd_port,a ;Move to lcd port
mov a,temp ;Reload the command from temp
anl a,#0FH ;Mask first four bits
add a,#80H ;Enable = 1
mov lcd_port,a ;Move to port
anl a,#0FH ;Enable = 0
mov lcd_port,a ;Move to lcd port
mov dcount,#1 ;delay 1 ms
acall delay
ret
;------------------------------------------------------------------
lcd_dat: ;LCD data Routine
mov temp,a ;Keep copy of data in temp
swap a ;We need higher nibble
anl a,#0FH ;Mask first four bits
add a,#0A0H ;Enable = 1, RS = 1
mov lcd_port,a ;Move to lcd port
nop
clr en ;Enable = 0
mov a,temp ;Reload the data from temp
anl a,#0FH ;we need lower nibble now
add a,#0A0H ;Enable = 1, RS = 1
mov lcd_port,a ;Move to lcd port
nop
clr en ;Enable = 0
mov dcount,#1 ;Delay 1mS
acall delay
ret
;------------------------------------------------------------------
;1mS delay @11.0592 MHz
Time1mS:
MOV R2,#2
MOV R1,#221
tim1mS:
DJNZ R1,$
DJNZ R2,tim1mS
RET
;------------------------------------------------------------------
Delay:
call Time1mS
djnz dcount,Delay
ret
;------------------------------------------------------------------
END