4-bit Initialization

Initialization of LCD is completed only after the reset sequence and basic initialization commands. We have already discussed about the reset sequence of the lcd in the previous section. So lets look at the programming now...

Assembly Program
;In this whole 4-bit tutorial LCD is connected to
;my controller in following way...
;D4 - P3.0
;D5 - P3.1
;D6 - P3.2
;D7 - P3.3
;EN - P3.7
;RS - P3.5

	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
	
lcd_reset:                  ;LCD reset sequence
	mov lcd_port, #0FFH
	mov delay,#20           ;20mS delay
	acall delayms
	mov lcd_port, #83H      ;Data = 30H, EN = 1, First Init
	mov lcd_port, #03H      ;Data = 30H, EN = 0
	mov delay,#15           ;Delay 15mS
	acall delayms
	mov lcd_port, #83H      ;Second Init, Data = 30H, EN = 1

	mov lcd_port, #03H      ;Data = 30H, EN = 0
	mov delay,#5            ;Delay 5mS
	acall delayms
	mov lcd_port, #83H      ;Third Init

	mov lcd_port, #03H
	mov delay,#5            ;Delay 5mS
	acall delayms
	mov lcd_port, #82H      ;Select Data width (20H for 4bit)

	mov lcd_port, #02H      ;Data = 20H, EN = 0
	mov delay,#5            ;Delay 5mS
	acall delayms
	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

C Program
//The pins used are same as explained earlier
#define lcd_port    P3

//LCD Registers addresses
#define LCD_EN      0x80
#define LCD_RS      0x20

void lcd_reset()
{
	lcd_port = 0xFF;
	delayms(20);
	lcd_port = 0x03+LCD_EN;
	lcd_port = 0x03;
	delayms(10);
	lcd_port = 0x03+LCD_EN;
	lcd_port = 0x03;
	delayms(1);
	lcd_port = 0x03+LCD_EN;
	lcd_port = 0x03;
	delayms(1);
	lcd_port = 0x02+LCD_EN;
	lcd_port = 0x02;
	delayms(1);
}

void lcd_init ()
{
	lcd_reset();         // Call LCD reset
	lcd_cmd(0x28);       // 4-bit mode - 2 line - 5x7 font. 
	lcd_cmd(0x0C);       // Display no cursor - no blink.
	lcd_cmd(0x06);       // Automatic Increment - No Display shift.
	lcd_cmd(0x80);       // Address DDRAM with 0 offset 80h.
 }

Sending Dommand/Data to LCD in 4-bit mode

Assembly Program
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 delay,#1          ;delay 1 ms
	acall delayms
	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 delay,#1          ;Delay 1mS
	acall delayms
	ret

C Program
void lcd_cmd (char cmd)
{ 
	lcd_port = ((cmd >
>
 4) & 0x0F)|LCD_EN;
	lcd_port = ((cmd >
>
 4) & 0x0F);

	lcd_port = (cmd & 0x0F)|LCD_EN;
	lcd_port = (cmd & 0x0F);

	delayus(200);
	delayus(200);
}

void lcd_data (unsigned char dat)
{ 
	lcd_port = (((dat >
>
 4) & 0x0F)|LCD_EN|LCD_RS);
	lcd_port = (((dat >
>
 4) & 0x0F)|LCD_RS);
	
	lcd_port = ((dat & 0x0F)|LCD_EN|LCD_RS);
	lcd_port = ((dat & 0x0F)|LCD_RS);

	delayus(200);
	delayus(200);
}

Share it! Like it!

Conversation

ExperimenterUK
Thu Oct 30 2014, 06:02 PM
This is a chat box..create a thread if you need help...but we don't do homework
Tue Oct 28 2014, 07:33 PM
vinni
hi rickey...anyone got the pgm...pls gimme now
Tue Oct 28 2014, 06:35 PM
potla
hi vinni even i need that program. help me if you get it
Tue Oct 28 2014, 03:22 PM
vinni
1. Using a look-up table in Flash ROM and accumulator offset indexed addressing mode, write a program that acts as a timer. The timer should count from 0 to 59 seconds. After reaching 59, it should go back to 0 on the next second and continue to count up. The timer should use the seven-segment display. The look-up table should contain information on how to display the digits 0 – 9 on the seven-segment display
Tue Oct 28 2014, 03:22 PM
vinni
hello Rickey..i need this pgm very urgently
Tue Oct 28 2014, 01:42 AM
Boy Donald
Dear sir, thanks before. i have some project urgent for my school and my project need this digital countdown timer using 7 segmen display. but your project only show ten minutes countdown, i need your help, can you show me more to change the minute or set the time manually. thank you, sorry for disturbing you.
Tue Oct 28 2014, 01:32 AM
Boy Donald
dear sir
Tue Oct 28 2014, 01:19 AM
Boy Donald
Rickey you inspired me a lot but why i can not change the time ( minute
Sun Oct 26 2014, 02:51 AM
sandeep bankar
dear sir I m facing problem about..how to read number of incoming call in gsm 900 a. pls help how to read it in atmel avr microcontroller.
ajay_bhargav
Mon Oct 20 2014, 11:22 AM
@skrishna, please post in forum so we can help you better.

Downloads

Comments

prash5054
Thu Oct 30 2014, 06:34 AM
Max.otto
Mon Oct 27 2014, 01:23 PM
toypoy
Sun Oct 26 2014, 01:06 AM
hiswe
Thu Oct 23 2014, 04:18 PM
pddung93
Sun Oct 19 2014, 01:56 PM
msideal
Thu Oct 16 2014, 08:48 AM
robotgirl
Tue Oct 14 2014, 06:23 PM
BRV64
Mon Oct 13 2014, 05:48 AM

Online

Guests: 47, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 37535
Newest member: prash5054
Forum Activity
rkodaganur Avatar
Posted By rkodaganur
Fri Oct 31 2014, 01:44 AM
Hi Ajay, I wanted to know can we use transformer ...
Max.otto Avatar
Posted By Max.otto
Thu Oct 30 2014, 11:09 AM
Hello all, before generating i have to give PWM p...
prash5054 Avatar
Posted By prash5054
Thu Oct 30 2014, 06:40 AM
can u post the updated code for the above problem ...
ExperimenterUK Avatar
Posted By ExperimenterUK
Wed Oct 29 2014, 05:36 PM
Thank you for the feedback.It's good to hear when ...
msideal Avatar
Posted By msideal
Tue Oct 28 2014, 05:34 PM
ExperimenterUK: Thankyou so much for helping me......
msideal Avatar
Posted By msideal
Tue Oct 28 2014, 05:18 PM
thanks ExperimenterUK for your help.... I will try...
ExperimenterUK Avatar
Posted By ExperimenterUK
Mon Oct 27 2014, 10:29 PM
Your error seems to be the use of PINC, which is f...
msideal Avatar
Posted By msideal
Mon Oct 27 2014, 05:45 PM
i want to extend my i/o's by writing this code. i ...
Max.otto Avatar
Posted By Max.otto
Mon Oct 27 2014, 01:30 PM
Hi all , i am working with PSoC 5LP. I have contin...
gaurav7577 Avatar
Posted By gaurav7577
Mon Oct 27 2014, 09:53 AM
hello everyone i wanna interface blood pressure se...