📄 4bit.asm
字号:
;------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
;Author: Ashwin.V
;Country:India
;Code:LCD interface in 4bit mode
;CPU:At89c51@11.0592Mhz
;Tips:All you need to do is call the line where you want to display the message, mov the charecter to lcd_data and call datw.
;If you want to display a string, move the address of the hardcodded string into dptr and call datw.
;-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
RS EQU p3.7
RW EQU p3.6
En EQU p3.5
LCD_DATA DATA 30h
org 0000h
ljmp lcd4_main
org 0030h
;___________________________________________________________________________
;---------------------delay subroutines start here--------------------------
;___________________________________________________________________________
;-------------------------------8us delay-----------------------------------
delay_8us: nop
nop
nop
nop
nop
nop
ret
;------------------------------100us delay----------------------------------
delay_100us: push 00
mov r0,#46
delay_100us_loop: djnz r1,delay_100us_loop
pop 00
ret
;------------------------------16ms delay-----------------------------------
delay_16ms: push 00
push 01
mov r0,#30
delay_16ms_loop1: mov r1,#255
delay_16ms_loop2: djnz r1,delay_16ms_loop2
djnz r0,delay_16ms_loop1
pop 01
pop 00
ret
;--------------------------half second delay-------------------------------
delay_half_second: push 00
push 01
push 02
mov r2,#0ah
delay_half_second1: mov r1,#64h
delay_half_second2: mov r0,#0ffh
back: djnz r0,back
djnz r1, delay_half_second2
djnz r2,delay_half_second1
pop 02
pop 01
pop 00
ret
;___________________________________________________________________________
;---------------------delay subroutines end here----------------------------
;___________________________________________________________________________
;___________________________________________________________________________
;----------------------LCD subroutines start here---------------------------
;___________________________________________________________________________
;-----------------------------------------------------------------
;----------------message definition and display-------------------
;-----------------------------------------------------------------
disp_LCD_message: push acc
push 00h
mov r0,#17
disp_LCD_message1: mov a, #00h ; reset accumulator
movc a,@a+dptr
mov lcd_data,a
acall datw
inc dptr
djnz r0,disp_LCD_message1
pop 00h
pop acc
ret
disp_testing: DB "Testing 4bit LCD"
disp_initialising: DB "Initialising...."
disp_ready: DB " 4bit LCD ready "
disp_clear: DB " "
;-----------------------------------------------------------------
;----------------lcd initialisation starts here-------------------
;-----------------------------------------------------------------
entry_set_mode:
mov lcd_data,#00h
acall cmdw4
acall delay_100us
ret
;-----------------------------------------------------
;-----------------------------------------------------
lcd4_initialise:
acall delay_16ms ; wait for more than 15ms after vdd reaches 4.5v
mov lcd_data,#020h ; 4bit interface
acall cmdw4
acall delay_100us
;-----------------------------------------------------
mov lcd_data,#20h ; entry mode set
acall cmdw4
acall delay_100us
mov lcd_data,#0c0h ; 2line 5x10 matrix
acall cmdw4
acall delay_100us
;------------------------------------------------------
acall entry_set_mode
mov lcd_data,#80h ; display off
acall cmdw4
acall delay_100us
;-------------------------------------------------------
acall entry_set_mode
mov lcd_data,#10h ; clear display
acall cmdw4
acall delay_100us
;--------------------------------------------------------
acall entry_set_mode
mov lcd_data,#60h ; cursor moves right, display doesn't shift
acall cmdw4
acall delay_100us
;--------------------------------------------------------
acall entry_set_mode
mov lcd_data,#0f0h ; cursor on and blink
acall cmdw4
acall delay_100us
ret
;-----------------------------------------------------------------
;-----------------lcd initialisation ends here--------------------
;-----------------------------------------------------------------
;-----------------------------------------------------------------
;--------command and data write subroutines starts here-----------
;-----------------------------------------------------------------
;-------------------------------------------------
;--------------4bit command write-----------------
;-------------------------------------------------
cmdw4:
push acc
clr RS
clr En
acall write_port
;--------------wait 8 machine cycles-------------
acall delay_8us
;------------------------------------------------
setb En
clr En
pop acc
ret
;------------------------------------------------
;--------------8bit command write----------------
;------------------------------------------------
cmdw:
push acc
clr RS
clr En
acall write_port
;--------------wait 8 machine cycles-------------
acall delay_8us
;------------------------------------------------
setb En
clr En
;-------------wait 8 machine cycles-------------
acall delay_8us
;------------------------------------------------
acall write_port
;--------------wait 8 machine cycles-------------
acall delay_8us
;------------------------------------------------
setb En
clr En
pop acc
ret
;-------------------------------------------------
;----------------8bit data write------------------
;-------------------------------------------------
datw: push acc
setb RS
clr En
acall write_port
;--------------wait 8 machine cycles-------------
acall delay_8us
;------------------------------------------------
setb En
clr En
;-------------wait 8 machine cycles--------------
acall delay_8us
;------------------------------------------------
acall write_port
;--------------wait 8 machine cycles-------------
acall delay_8us
;------------------------------------------------
setb En
clr En
pop acc
ret
;-------------------------------------------------
;----------------4bit port write------------------
;-------------------------------------------------
write_port:
mov a, lcd_data
rlc a
mov p1.0, c ;D7
rlc a
mov p1.1, c ;D6
rlc a
mov p1.2, c ;D5
rlc a
mov p1.3, c ;D4
mov lcd_data, a
ret
;-------------------------------------------------------------
;--------command and data write subroutine ends here----------
;-------------------------------------------------------------
;--------------------------------------------------
;--------lcd line select(1/2) starts here----------
;--------------------------------------------------
line1: mov lcd_data,#80h ;initial positio of cursor
acall cmdw
acall delay_100us
ret
line2: mov lcd_data,#0c0h ;initial position of cursor
acall cmdw
acall delay_100us
ret
;--------------------------------------------------
;---------lcd line select(1/2) ends here-----------
;--------------------------------------------------
;___________________________________________________________________________
;-----------------------LCD subroutines ends here---------------------------
;___________________________________________________________________________
;__________________________________________________
;-----------------main code------------------
;__________________________________________________
lcd4_main: mov p1, #00h
clr En
clr RS
clr rw
mov IE,#00h
acall lcd4_initialise
;---------------------------------------------------------
lcd4_main1: lcall line1
mov dptr,#disp_initialising
acall disp_LCD_message
acall delay_half_second
acall delay_half_second
acall delay_half_second
acall delay_half_second
lcall line1
mov dptr,#disp_testing
acall disp_LCD_message
acall delay_half_second
acall delay_half_second
lcall line2
mov dptr,#disp_ready
acall disp_LCD_message
acall delay_half_second
acall delay_half_second
acall delay_half_second
acall delay_half_second
sjmp lcd4_main1
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -