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

Mon May 08 2017, 12:45 PM
ivan muvunyi
please can you send me wiring diagram for automatic control of 3 phase induction motor for production of maize flour
Wed May 03 2017, 08:39 PM
imran qureshi
hello friend
Sun Apr 30 2017, 05:49 AM
Lijin G Varghese
Can u send me the circuit diagram for Program to interface GSM Module with 8051 microcontroller (AT89C51) using PC and LCD
Fri Apr 28 2017, 03:47 PM
SANDEEP T S
I need pcb wizard 3.5 version kindly send valid link of yours
ExperimenterUK
Mon Apr 17 2017, 11:56 PM
Sorry..We had a problem with registration.Confirmation emails were not sent.Please re-register
Thu Apr 13 2017, 07:29 PM
Piyush Aughad
I want circuit & program for radar detection using Microcontroller 8051
Tue Apr 11 2017, 03:27 AM
Shabbir Ahmed
Hi Ajay ....hope you and your family all will be fine are you working on andriod aplications if yes then plz reply me
Sun Apr 09 2017, 06:33 PM
ATHULYA NAIR
Dear Sir, I need to know what are the complexity for programming in 8051 Microcontroller for three phase inverter?
Sun Apr 09 2017, 06:32 PM
ATHULYA NAIR
Dear Sir,
Sun Apr 09 2017, 04:43 AM
Rohit chandra joshi
Sir I need ur help urgently.i am trying to make pc controlled led scrolling display using 8051.when i am trying to generate hex file of the code that is given by pratik suthar.it gives the error Target not created Limit:0800h I am using keil uvision4 software. Even i have tried to burn the given hex file that is atteched with the code but my led are not glowing. Pls help me sir .i struck in this problem from last month and till now i am not able to short out the problem. Pls suggest me to solve the problem.mail me at [email protected] Thanks

Downloads

Comments

lubnakhan
Sat May 20 2017, 10:23 AM
Utsavi
Wed May 17 2017, 10:35 AM
Samimash
Tue May 16 2017, 01:15 PM
gangusa
Mon May 15 2017, 04:34 AM
raghuzala
Sun May 14 2017, 02:32 PM
Prakashlimbachiya
Sat May 13 2017, 07:34 AM
east
Fri May 12 2017, 08:16 AM
ajay24
Fri May 12 2017, 08:09 AM

Online

Guests: 48, Members: 0 ...

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

Members: 37827
Newest member: lubnakhan
Forum Activity
ajay_bhargav Avatar
Posted By ajay_bhargav
Fri May 19 2017, 10:17 AM
I think you are asking in the wrong forum. We do n...
ajay_bhargav Avatar
Posted By ajay_bhargav
Fri May 19 2017, 10:16 AM
You should start with something, We will help you ...
ajay_bhargav Avatar
Posted By ajay_bhargav
Fri May 19 2017, 07:01 AM
To me it looks like your development environment i...
ExperimenterUK Avatar
Posted By ExperimenterUK
Thu May 18 2017, 11:28 PM
Linux can be a minefield of make files, scripts an...
ExperimenterUK Avatar
Posted By ExperimenterUK
Thu May 18 2017, 11:18 PM
Can you provide me a demo of XAG-49 counter so tha...
Utsavi Avatar
Posted By Utsavi
Thu May 18 2017, 05:57 AM
HelloThanks for reply.No the input pin is not faul...
ExperimenterUK Avatar
Posted By ExperimenterUK
Wed May 17 2017, 07:38 PM
Just from reading the data sheet you seem to doing...
Utsavi Avatar
Posted By Utsavi
Wed May 17 2017, 11:01 AM
HelloI am working with PXAG49KBA. In my project I ...
Samimash Avatar
Posted By Samimash
Wed May 17 2017, 04:37 AM
thank you for your reply friend 'cc1' is a file ac...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue May 16 2017, 05:05 PM
I'm sure this is a compiler / Linux problem.In Lin...