eperiment3
eperiment3
Table of Content
Introduction
LCD 4-bit interfacing with 8051
Microcontroller
-bit mode – Using 8 data lines in
8 cmd
(
0x06
)
;
LCD (totally 8 data lines are there)
cmd
(
0x8f
)
;
-bit mode – Usingonly4datalines
4
in theLCD module
}
-bitmodeisalreadyworkingandthat
8
looks awesome. Then why are we
going to 4-bit mode? This is the Sending command
question that comes to everyone’s
mindwheneverIsay4-bitmode.Yeah,
that 8-bit mode is nice. But but but… ere everything is the same except
H
Just assume. I’m doing one project the way of datawriting.Herewehave
which requires more hardware. But only4bits.Soweneedtosendnibble
8051 has only 32 GPIOs. So in that bynibble.Sofirstweneedtosendthe
time I can use this 4-bit mode and first nibble then followed by the
reduce the pin required for the LCD second.Seethatcode.I’mwritinginto
module. Am Iright?Great.That’swhy Port 2’s last4bits.Becausethelast4
4-bit mode is also important. Already bits are connected to the LCD.
we know the LED’s operation. If we
want to enable 4-bit modewehaveto
void
cmd
(
unsigned
char
a
)
do small modifications in the normal
method. Let’s see that.
{
In initializing time we have to give a
0x28 command. That’s all. rs=0;
P2&=
0x0F
;
//clearing
void
lcd_init
()
the last 4 bits without
disturbing first 4 bits
{
P2|=
(
a&
0xf0
)
;
//writing
cmd
(
0x02
)
;
higher nibble
cmd
(
0x28
)
;
e=1;
cmd
(
0x0c
)
;
delay
()
;
e=0;
P2&=
0x0F
;
//clearing the last 4 bits
delay
()
;
without disturbing first 4 bits
P2&=
0x0f
;
//clearing
P2|=
(
b&
0xf0
)
;
P2|=
(
a
<<
4&
0xf0
)
;
//writing
lower nibble
delay
()
;
e=1;
e=0;
delay
()
;
delay
()
;
e=0;
P2&=
0x0f
;
//clearing the last 4 bits
delay
()
;
without disturbing first 4 bits
}
P2|=
(
b
<<
4&
0xf0
)
;
//writing lower nibble
delay
()
;
Same as sending data.
void
dat
(
unsigned
char
b
) e=0;
{
delay
()
;
rs=1;
}
rw=0;
PIN DIAGRAM
void
show
(
unsigned
char
*s
)
;
void
delay
()
;
void
main
()
{
int
j;
CODE cmd
(
0x18
)
;
#include<reg51.h>
for
(
j=0;j
<
25000;j++
)
;
sbit rs=P2^0;
}
sbit rw=P2^1;
}
sbit e=P2^2;
void
lcd_init
()
void
lcd_init
()
;
{
void
cmd
(
unsigned
char
a
)
;
cmd
(
0x02
)
;
void
dat
(
unsigned
char
b
)
;
cmd
(
0x28
)
;
cmd
(
0x0c
)
;
delay
()
;
cmd
(
0x06
)
;
e=0;
cmd
(
0x8f
)
;
delay
()
;
}
}
void
cmd
(
unsigned
char
a
) void
dat
(
unsigned
char
b
)
{
{
rs=0;
rs=1;
rw=0;
rw=0;
P2&=
0x0F
;
P2&=
0x0F
;
P2|=
(
a&
0xf0
)
;
P2|=
(
b&
0xf0
)
;
e=1;
e=1;
delay
()
;
delay
()
;
e=0;
e=0;
delay
()
;
delay
()
;
P2&=
0x0f
;
P2&=
0x0f
;
P2|=
(
a
<<
4&
0xf0
)
;
P2|=
(
b
<<
4&
0xf0
)
;
e=1;
e=1;
delay
()
;
OUTPUT
e=0;
delay
()
;
}
void
show
(
unsigned
char
*s
)
{
{
dat
(
*s++
)
;
}
}
void
delay
()
{
unsigned
int
k,l;
for
(
k=0;k
<
2;k++
)
for
(
l=0;l
<
1275;l++
)
;
}