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

Fri Nov 28 2014, 06:31 AM
babar
aoa ,, i need a project for 8051 microcontroler can some one plz give me the pdf form of this project android control robot
Fri Nov 28 2014, 06:30 AM
babar
hi
ExperimenterUK
Thu Nov 20 2014, 06:00 PM
@Mehboob.. I don't think you can do that.
Sat Nov 15 2014, 06:41 AM
Mehboob
Pls can you send me code in c of PWM generation using UART/serial port in 8051.
Fri Nov 14 2014, 02:51 PM
mohammad aqil
NEED A BOOK ON MICROPROCESSOR AND INTERFACING BY D.V.HALL. RIGHT NOW, I SHALL BE VERY THANK FULL TO U....
Thu Nov 13 2014, 02:15 PM
m
please code hex project avr blood pressure monitor
Thu Nov 13 2014, 05:56 AM
Lambo
hi mate, whats the ajay-bhargav? tell us more pls
ExperimenterUK
Wed Nov 12 2014, 12:56 AM
@tmtm000.. have you been able to join ?
ExperimenterUK
Wed Nov 12 2014, 12:54 AM
@Alice..become a member,then send a PM to ajay_bhargav the site owner.
Tue Nov 11 2014, 03:58 AM
Abhishek Bansal
Thanks a lot for your informative tutorials!

Downloads

Comments

PsySc0rpi0n
Thu Nov 27 2014, 02:00 PM
hanifnoor
Thu Nov 27 2014, 09:42 AM
dpkraju
Wed Nov 26 2014, 02:18 PM
sankalp_s
Tue Nov 25 2014, 05:13 PM
dodul
Mon Nov 24 2014, 10:08 PM
VikiPeter
Tue Nov 18 2014, 09:13 AM
gentlboy
Sun Nov 16 2014, 11:13 PM
philipeastbourne
Wed Nov 12 2014, 05:32 AM

Online

Guests: 64, Members: 0 ...

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

Members: 37552
Newest member: PsySc0rpi0n
Forum Activity
PsySc0rpi0n Avatar
Posted By PsySc0rpi0n
Fri Nov 28 2014, 07:05 AM
Good morning. Thanks for replying. The problem in ...
ajay_bhargav Avatar
Posted By ajay_bhargav
Fri Nov 28 2014, 04:44 AM
You can read status of pin before starting the gam...
ajay_bhargav Avatar
Posted By ajay_bhargav
Fri Nov 28 2014, 04:37 AM
As ExperimenterUK (Phil) said, searching for speci...
ajay_bhargav Avatar
Posted By ajay_bhargav
Fri Nov 28 2014, 04:31 AM
[Topic moved to AVR Discussion Forum]
ajay_bhargav Avatar
Posted By ajay_bhargav
Fri Nov 28 2014, 04:29 AM
If you have worked on normal C language then Embed...
PsySc0rpi0n Avatar
Posted By PsySc0rpi0n
Thu Nov 27 2014, 02:30 PM
Hi...I'm new in forum and with programming 8051 co...
hanifnoor Avatar
Posted By hanifnoor
Thu Nov 27 2014, 09:45 AM
while(msg_array[i]!='\0') { if(msg_array[i]=...
rearthur2003 Avatar
Posted By rearthur2003
Thu Nov 27 2014, 05:23 AM
there are two types of diodes ir rx which is coat...
rearthur2003 Avatar
Posted By rearthur2003
Thu Nov 27 2014, 05:19 AM
First you will need to let us know what IDE you pl...
engr1931 Avatar
Posted By engr1931
Thu Nov 27 2014, 05:18 AM
yes sir! i only know assembly, and still in proces...