4 BIT LCD CODE
Discussion in "Project Addition or Changes" started by SGH Sep 5, 2007.
Wed Sep 05 2007, 08:34 am
Rickey,
Pls. see the attached code for 4 bit lcd.
1). How to clear the lcd screen so I can write routine for 'Display_2' and "Display_3'. Code for 'Display_1' came together with the lcd code and was downloaded from the internet.
2). At first start, the author of the code wrote lcall resetlcd4 followed by initlcd4. At the end of routine 'resetlcd4', there was a 'ajmp initlcd4'. This meant the 'initlcd4' was executed 2 times. First at the end of routine 'resetlcd4' and second time from 'lcall initlcd4'. Why the 'initlcd4' was executed 2 times, is this correct ?.
3). Is it a correct way to use 'lcall resetlcd4' followed by 'lcall initlcd4' (as routine display_1) to clear the lcd screen for writing new words ?.
Thanks,
SGH
Pls. see the attached code for 4 bit lcd.
1). How to clear the lcd screen so I can write routine for 'Display_2' and "Display_3'. Code for 'Display_1' came together with the lcd code and was downloaded from the internet.
2). At first start, the author of the code wrote lcall resetlcd4 followed by initlcd4. At the end of routine 'resetlcd4', there was a 'ajmp initlcd4'. This meant the 'initlcd4' was executed 2 times. First at the end of routine 'resetlcd4' and second time from 'lcall initlcd4'. Why the 'initlcd4' was executed 2 times, is this correct ?.
3). Is it a correct way to use 'lcall resetlcd4' followed by 'lcall initlcd4' (as routine display_1) to clear the lcd screen for writing new words ?.
Thanks,
SGH
Wed Sep 05 2007, 08:38 am
Rickey,
The attachment didn't show at first post. Here I try again.
Thanks,
SGH
The attachment didn't show at first post. Here I try again.
Thanks,
SGH
Wed Sep 05 2007, 08:44 am
Rickey,
Still didn't see the attachment. Here is the code.
;==================================================================================
lcd_data equ p0
lcd_db4 equ p0.4
lcd_db5 equ p0.5
lcd_db6 equ p0.6
lcd_db7 equ p0.7
lcd_rs equ p1.2
lcd_rw equ p1.1
lcd_e equ p1.0
config equ 28h
entrymode equ 6
offcur equ 0ch
blinkcur equ 0dh
clrdsp equ 01h
ondsp equ 0eh
;==================================================================================
org 0H
lcall resetlcd4
lcall initlcd4
loop: lcall display_1
lcall disp_delay
lcall display_2
lcall disp_delay
lcall display_3
lcall disp_delay
sjmp loop
;==================================================================================
display_1: mov a,#1 ; line 1
mov b,#0 ; column 1
lcall placecur4
lcall prtlcd4
db 'Hello',0
mov a,#2 ; line 2
mov b,#0 ; column 1
lcall placecur4
lcall prtlcd4
db 'World',0
mov r4,#offcur
lcall wrlcdcom4
ret
;==================================================================================
; How to clear the LCD screen to write new words ?. For example I want to write
; 'Microcontroller' at line 1 and column 1 and write 'Project' at line 2 column 1
display_2:
ret
;==================================================================================
; How to clear the LCD screen to write new words ?. For example I want to write
; 'Testing' at line 1 and column 1 and write 'LCD 16 x 2' at line 2 column 1
display_3:
ret
;==================================================================================
initlcd4: clr lcd_rs
clr lcd_rw
clr lcd_e
mov r4,#config
lcall wrlcdcom4
mov r4,#ondsp
lcall wrlcdcom4
mov r4,#entrymode
lcall wrlcdcom4
mov r4,#clrdsp
lcall wrlcdcom4
ret
resetlcd4: clr lcd_rs
clr lcd_rw
clr lcd_e
clr lcd_db7
clr lcd_db6
setb lcd_db5
setb lcd_db4
setb lcd_e
clr lcd_e
mov a,#4
lcall mdelay
setb lcd_e
clr lcd_e
mov a,#1
lcall mdelay
setb lcd_e
clr lcd_e
mov a,#1
lcall mdelay
clr lcd_db4
setb lcd_e
clr lcd_e
mov a,#1
lcall mdelay
mov r4,#config
lcall wrlcdcom4
mov r4,#08h
lcall wrlcdcom4
mov r4,#1
lcall wrlcdcom4
mov r4,#entrymode
lcall wrlcdcom4
ajmp initlcd4
wrlcdcom4: clr lcd_e
clr lcd_rs
clr lcd_rw
push acc
mov a,r4
mov c,acc.4
mov lcd_db4,c
mov c,acc.5
mov lcd_db5,c
mov c,acc.6
mov lcd_db6,c
mov c,acc.7
mov lcd_db7,c
setb lcd_e
clr lcd_e
mov c,acc.0
mov lcd_db4,c
mov c,acc.1
mov lcd_db5,c
mov c,acc.2
mov lcd_db6,c
mov c,acc.3
mov lcd_db7,c
lcall pulseewait4
pop acc
ret
wrlcddata: clr lcd_e
setb lcd_rs
clr lcd_rw
push acc
mov a,r4
mov c,acc.4
mov lcd_db4,c
mov c,acc.5
mov lcd_db5,c
mov c,acc.6
mov lcd_db6,c
mov c,acc.7
mov lcd_db7,c
setb lcd_e
clr lcd_e
mov c,acc.0
mov lcd_db4,c
mov c,acc.1
mov lcd_db5,c
mov c,acc.2
mov lcd_db6,c
mov c,acc.3
mov lcd_db7,c
lcall pulseewait4
pop acc
ret
pulseewait4: clr lcd_e
setb lcd_e
clr lcd_e
mov lcd_data,#0ffh
setb lcd_rw
push acc
pew: setb lcd_e
mov a,lcd_data
clr lcd_e
setb lcd_e
clr lcd_e
jb acc.7,pew
pop acc
ret
prtlcd4: pop dph
pop dpl
prtnext: clr a
movc a,@a+dptr
cjne a,#0,chrok
sjmp retprtlcd
chrok: mov r4,a
lcall wrlcddata
inc dptr
ajmp prtnext
retprtlcd: mov a,#1h
jmp @a+dptr
placecur4: dec acc
jnz line2
mov a,b
add a,#080h
sjmp setcur
line2: mov a,b
add a,#0c0h
setcur: mov r4,a
lcall wrlcdcom4
ret
setcgram: push b
mov b,#8
mul ab
pop b
add a,b
add a,#40h
mov r4,a
lcall wrlcdcom4
ret
mdelay: push acc
mov a,#0a6h
md_olp: inc a
nop
nop
nop
nop
nop
nop
nop
nop
jnz md_olp
nop
pop acc
ret
;==================================================================================
disp_delay: mov r3,#12
ax0b: mov r2,#0ffh
ax1b: mov r5,#0ffh
ax2b: djnz r5,ax2b
djnz r2,ax1b
djnz r3,ax0b
ret
;==================================================================================
end
Thanks,
SGH
Still didn't see the attachment. Here is the code.
;==================================================================================
lcd_data equ p0
lcd_db4 equ p0.4
lcd_db5 equ p0.5
lcd_db6 equ p0.6
lcd_db7 equ p0.7
lcd_rs equ p1.2
lcd_rw equ p1.1
lcd_e equ p1.0
config equ 28h
entrymode equ 6
offcur equ 0ch
blinkcur equ 0dh
clrdsp equ 01h
ondsp equ 0eh
;==================================================================================
org 0H
lcall resetlcd4
lcall initlcd4
loop: lcall display_1
lcall disp_delay
lcall display_2
lcall disp_delay
lcall display_3
lcall disp_delay
sjmp loop
;==================================================================================
display_1: mov a,#1 ; line 1
mov b,#0 ; column 1
lcall placecur4
lcall prtlcd4
db 'Hello',0
mov a,#2 ; line 2
mov b,#0 ; column 1
lcall placecur4
lcall prtlcd4
db 'World',0
mov r4,#offcur
lcall wrlcdcom4
ret
;==================================================================================
; How to clear the LCD screen to write new words ?. For example I want to write
; 'Microcontroller' at line 1 and column 1 and write 'Project' at line 2 column 1
display_2:
ret
;==================================================================================
; How to clear the LCD screen to write new words ?. For example I want to write
; 'Testing' at line 1 and column 1 and write 'LCD 16 x 2' at line 2 column 1
display_3:
ret
;==================================================================================
initlcd4: clr lcd_rs
clr lcd_rw
clr lcd_e
mov r4,#config
lcall wrlcdcom4
mov r4,#ondsp
lcall wrlcdcom4
mov r4,#entrymode
lcall wrlcdcom4
mov r4,#clrdsp
lcall wrlcdcom4
ret
resetlcd4: clr lcd_rs
clr lcd_rw
clr lcd_e
clr lcd_db7
clr lcd_db6
setb lcd_db5
setb lcd_db4
setb lcd_e
clr lcd_e
mov a,#4
lcall mdelay
setb lcd_e
clr lcd_e
mov a,#1
lcall mdelay
setb lcd_e
clr lcd_e
mov a,#1
lcall mdelay
clr lcd_db4
setb lcd_e
clr lcd_e
mov a,#1
lcall mdelay
mov r4,#config
lcall wrlcdcom4
mov r4,#08h
lcall wrlcdcom4
mov r4,#1
lcall wrlcdcom4
mov r4,#entrymode
lcall wrlcdcom4
ajmp initlcd4
wrlcdcom4: clr lcd_e
clr lcd_rs
clr lcd_rw
push acc
mov a,r4
mov c,acc.4
mov lcd_db4,c
mov c,acc.5
mov lcd_db5,c
mov c,acc.6
mov lcd_db6,c
mov c,acc.7
mov lcd_db7,c
setb lcd_e
clr lcd_e
mov c,acc.0
mov lcd_db4,c
mov c,acc.1
mov lcd_db5,c
mov c,acc.2
mov lcd_db6,c
mov c,acc.3
mov lcd_db7,c
lcall pulseewait4
pop acc
ret
wrlcddata: clr lcd_e
setb lcd_rs
clr lcd_rw
push acc
mov a,r4
mov c,acc.4
mov lcd_db4,c
mov c,acc.5
mov lcd_db5,c
mov c,acc.6
mov lcd_db6,c
mov c,acc.7
mov lcd_db7,c
setb lcd_e
clr lcd_e
mov c,acc.0
mov lcd_db4,c
mov c,acc.1
mov lcd_db5,c
mov c,acc.2
mov lcd_db6,c
mov c,acc.3
mov lcd_db7,c
lcall pulseewait4
pop acc
ret
pulseewait4: clr lcd_e
setb lcd_e
clr lcd_e
mov lcd_data,#0ffh
setb lcd_rw
push acc
pew: setb lcd_e
mov a,lcd_data
clr lcd_e
setb lcd_e
clr lcd_e
jb acc.7,pew
pop acc
ret
prtlcd4: pop dph
pop dpl
prtnext: clr a
movc a,@a+dptr
cjne a,#0,chrok
sjmp retprtlcd
chrok: mov r4,a
lcall wrlcddata
inc dptr
ajmp prtnext
retprtlcd: mov a,#1h
jmp @a+dptr
placecur4: dec acc
jnz line2
mov a,b
add a,#080h
sjmp setcur
line2: mov a,b
add a,#0c0h
setcur: mov r4,a
lcall wrlcdcom4
ret
setcgram: push b
mov b,#8
mul ab
pop b
add a,b
add a,#40h
mov r4,a
lcall wrlcdcom4
ret
mdelay: push acc
mov a,#0a6h
md_olp: inc a
nop
nop
nop
nop
nop
nop
nop
nop
jnz md_olp
nop
pop acc
ret
;==================================================================================
disp_delay: mov r3,#12
ax0b: mov r2,#0ffh
ax1b: mov r5,#0ffh
ax2b: djnz r5,ax2b
djnz r2,ax1b
djnz r3,ax0b
ret
;==================================================================================
end
Thanks,
SGH
Wed Sep 05 2007, 04:27 pm
Sorry there might be some problem in uploading files so attachment is not working.. anyways..
As seen from the code.. in resetlcd4 you can remove the part
and leave rest of the code as it is..
to clear display.. send command 0x1 to LCD. it will clear the display. If you are using a 2 line LCD, then 0x80 is the address for Line1 and 0xC0 is address of Line 2.
As seen from the code.. in resetlcd4 you can remove the part
mov r4,#config lcall wrlcdcom4 mov r4,#08h lcall wrlcdcom4 mov r4,#1 lcall wrlcdcom4 mov r4,#entrymode lcall wrlcdcom4
and leave rest of the code as it is..
to clear display.. send command 0x1 to LCD. it will clear the display. If you are using a 2 line LCD, then 0x80 is the address for Line1 and 0xC0 is address of Line 2.
Thu Sep 06 2007, 07:14 am
Sorry, I don't understand.
1). Quote :
As seen from the code.. in resetlcd4 you can remove the part
mov r4,#config
lcall wrlcdcom4
mov r4,#08h
lcall wrlcdcom4
mov r4,#1
lcall wrlcdcom4
mov r4,#entrymode
lcall wrlcdcom4
Removing above routine to perform what task ?.
2). What about info for my questions #3 in first post ?.
SGH
1). Quote :
As seen from the code.. in resetlcd4 you can remove the part
mov r4,#config
lcall wrlcdcom4
mov r4,#08h
lcall wrlcdcom4
mov r4,#1
lcall wrlcdcom4
mov r4,#entrymode
lcall wrlcdcom4
Removing above routine to perform what task ?.
2). What about info for my questions #3 in first post ?.
SGH
Thu Sep 06 2007, 01:12 pm
ok.. first... removing the above code.. perform nothing.. except reducing the memory used.. because if you see the resetlcd4 clearly.. that part of code is just repeated twice.. first in resetlcd4 and then initlcd4. also there is a call to initlcd4 so you can remove those codes from resetlcd4 as they are there in initlcd4
regarding your question 3, i say reseting the LCD just to clear the screen is not a good idea but one of the idea.. so best you can do is.. send command - LCD Clear and code is 1H.
regarding your question 3, i say reseting the LCD just to clear the screen is not a good idea but one of the idea.. so best you can do is.. send command - LCD Clear and code is 1H.
Thu Sep 06 2007, 02:33 pm
1). I just want to learn and the original code was downloaded from the internet. Didn't know who is the author of this code.
As I mentioned before, at first start and everytime if the author want to display new words on the LCD, the author call resetlcd4 followed by initlcd4. At the end of resetlcd4 routine there is a "jmp initlcd4". This meant, the initlcd4 was called twice. What is the purpose of this arrangment ?.
2). Which arrangment is better, resetlcd4 (without calling initlcd4) and initlcd4 or just send 1H to clear the LCD screen for writing new words ?. What is the advantage and disadvantage of each arrangement ?.
Many thanks,
SGH
As I mentioned before, at first start and everytime if the author want to display new words on the LCD, the author call resetlcd4 followed by initlcd4. At the end of resetlcd4 routine there is a "jmp initlcd4". This meant, the initlcd4 was called twice. What is the purpose of this arrangment ?.
2). Which arrangment is better, resetlcd4 (without calling initlcd4) and initlcd4 or just send 1H to clear the LCD screen for writing new words ?. What is the advantage and disadvantage of each arrangement ?.
Many thanks,
SGH
Thu Sep 06 2007, 02:38 pm
1) well i don't know the state of mind of the author why he did that.. might be some problem or something..
2) sending 1H is better coz.. save time.. other wise the whole reset procedure take lot of time as compared to just one statement..
why don't u give it a try? i mean sending 1 to LCD to clear it.. and see the results..
2) sending 1H is better coz.. save time.. other wise the whole reset procedure take lot of time as compared to just one statement..
why don't u give it a try? i mean sending 1 to LCD to clear it.. and see the results..
Powered by e107 Forum System