Professional Documents
Culture Documents
Chapter 12 - LCD and Keyboard
Chapter 12 - LCD and Keyboard
Chapter 12 - LCD and Keyboard
sh
ul
Sh
ar
m
ul
Sh
An
LCD Operation
ar
m
sh
LCD
INTERFACING
VSS
--
Ground
VCC
--
VEE
--
RS
R/W
E
ar
m
I
Sh
I/O
I/O
Enable
DB0
I/O
DB1
I/O
DB2
I/O
10
DB3
I/O
11
DB4
I/O
12
DB5
I/O
13
DB6
I/O
14
DB7
I/O
ul
-Send displayed
information or
instruction
command codes to
the LCD
-Read the contents
of the LCDs
internal registers
Symbol
sh
LCD Pin
Descriptions
Pin
An
LCD
INTERFACING
used by the
LCD to latch
information
presented to
its data bus
Return home
Sh
ar
m
ul
sh
LCD Command
Codes
10
14
18
1C
80
C0
38
An
LCD
INTERFACING
PD.0
D0
VEE
LCD
PD.7
D7
VSS
RS R/W E
PB.0
PB.1
10k
POT
ar
m
Sh
VCC
ul
+5V
PIC
#include <P1BF45BO.h>
#define ldata PORTD
#define rs PORTBbits.RB0
#define rw PORTBbits.RB1
#define en PORTBbits.RB2
void main()
TRI SD = 0;
TRISB = 0;
en = 0;
MSDelay(250);
lcdcmd ( 0x38) ;
MSDelay(250);
lcdcmd (0x 0E) ;
sh
Sending Data/
Commands to
LCDs w/ Time
Delay
An
LCD
INTERFACING
To send any of the commands to the LCD, make pin RS=0. For data,
make RS=1. Then send a high-to-low pulse to the E pin to enable the
internal latch of the LCD. This is shown in the code below.
PB.2
PD.0
D0
LCD
D7
VSS
RS R/W E
PB.0
PB.1
PB.2
10k
POT
a
ar
m
Sh
VCC
VEE
PD.7
+5V
//line 1, position 6
//display letter 'M'
//display letter 'D'
//display letter 'E'
}
void lcdcmd(unsigned char value)
{
ldata = value;
//put the value on the pins
rs = 0;
rw = 0;
en = 1;
//strobe the enable pin
MSDelay(1);
en = 0;
}
ul
PIC
(cont)
sh
Sending Data/
Commands to
LCDs w/ Time
Delay
//clear LCD
An
LCD
INTERFACING
MSDelay ( 15) ;
lcdcmd ( 0x01) ;
MSDelay ( 15) ;
lcdcmd ( 0x06) ;
MSDelay ( 15) ;
lcdcmd ( 0x86) ;
MSDelay ( 15) ;
lcddata ( 'M');
MSDelay ( 15) ;
lcddata( 'D');
MSDelay ( 15) ;
lcddata( 'E');
Sh
ar
m
ul
An
sh
PD.0
VCC
D0
VEE
LCD
PD.7
D7
VSS
RS R/W E
PB.0
PB.1
PB.2
+5V
10k
POT
ar
m
Sh
PIC
(cont)
void main()
{
TRISD = 0;
TRI SB = 0;
en = 0;
MSDelay(250);
lcdcmd ( 0x38) ;
MSDelay(250);
lcdcmd (0x 0E) ;
lcdready () ;
lcdcmd ( 0x01) ;
ul
and Data to
LCDs w/ Busy
Flag
#include <P1BF45BO.h>
#define ldata PORTD
#define rs PORTBbits.RB0
#define rw PORTBbits.RB1
#define en PORTBbits.RB2
sh
Sending Codes
An
LCD
INTERFACING
ar
m
lcdready();
lcdcmd ( 0x06) ;
lcdready () ;
lcdcmd ( 0x86) ;
lcdready () ;
lcddata ( 'M');
lcdready();
lcddata( 'D');
lcdready();
lcddata( 'E');
An
sh
ul
Sh
}
void lcdcmd(unsigned char value)
{
ldata = value;
//put the value on the pins
rs = 0;
rw = 0;
en = 1;
//strobe the enable pin
MSDelay(1);
en = 0;
}
An
sh
ul
Sh
ar
m
{
unsigned inti, j;
for(i=0;i<itime;i++)
LCD
INTERFACING
Data
ar
m
tD
E
tAS
tAH
Sh
R/W
ul
RS
sh
(cont)
D0 D7
An
Sending Codes
and Data to
LCDs w/ Busy
Flag
11
a
ar
m
tH
tAS
tPWH
tAH
Sh
R/W
tDSW
RS
ul
(cont)
Data
sh
Sending Codes
and Data to
LCDs w/ Busy
Flag
An
LCD
INTERFACING
12
RS
R/W
An
sh
ul
Sh
ar
m
LCD
INTERFACING
DB7
Line1 (min) 1
Line1 (max) 1
Line2 (min) 1
Line2 (max) 1
DB6
DB5
DB4
DB3
DB2
DB1
DB0
Keypad
ar
m
An
sh
ul
Sh
ar
m
Sh
sh
ul
In I B M PC ke y bo ards, a si ng l e
mi cro c o nt ro l ler t ake s ca re o f
hardware and software interfacing
An
KEYBOARD
INTERFACING
15
Sh
ar
m
Identifying the
Key Pressed by
Interrupt
An
sh
ul
Sh
ul
sh
An
Keyboard
debounce
ar
m
3. To detect which row the key press belongs to, the microcontroller
grounds one row at a time, reading the columns each time. If it finds
that all columns are HIGH, this means that the key press cannot belong
to that row; therefore, it grounds the next row and continues until it
finds the row the key press belongs to. Upon finding the row that the
key press belongs to, it sets up the starting address for the look-up
table holding the scan codes (or the ASCII value) for that row and goes
to the next stage to identify the key.
4. To identify the key press, the microcontroller rotates the column
bits, one bit at a time, into the carry flag and checks to see if it is LOW.
Upon finding the zero, it pulls out the ASCII code for that key from the
look-up table; otherwise, it increments the pointer to point to the next
element of the look-up table.
a
ar
m
Sh
ul
sh
An
Interrupt method for key press detection
Scanning and
Identifying the
Key
Vcc
3
D2
An
D3
Port 1
(Out)
sh
D1
ul
D0
ar
m
Scanning Method
Sh
KEYBOARD
INTERFACING
D3
D2
D1
D0
Port 2
(In)
If no key has
been pressed,
reading the
input port will
yield 1s for all
columns since
they are all
connected to
high (Vcc)
19
ar
m
Grounding
Rows and
Reading
Columns
Sh
ul
Scanning Method
sh
An
KEYBOARD
INTERFACING
Scanning Method
ul
(cont)
It gro unds t he ne x t ro w,
re a ds t he c o l umns , a nd
che c k s fo r any ze ro
Sh
ar
m
sh
Grounding
Rows and
Reading
Columns
S t ar t i ng w i t h t he t o p ro w, t he
mi cro c o nt ro l ler gro und s i t by
p rov i di ng a l o w t o ro w D 0 o nl y
An
KEYBOARD
INTERFACING
21
ar
m
Sh
(cont)
Solution :
From Figure 13-5 the row and column can be used to identify the key.
(a) The row belongs to D0 and the column belongs to D2; therefore,
key number 2 was pressed.
(b) The row belongs to D1 and the column belongs to D3; therefore,
key number 7 was pressed.
ul
Grounding
Rows and
Reading
Columns
sh
Scanning Method
Example 12-3
From Figure 12-6, identify the row and column of the pressed key for
each of the following.
(a) D3 D0 = 1110 for the row, D3 D0 = 1011 for the column
(b) D3 D0 = 1101 for the row, D3 D0 = 0111 for the column
D0
An
KEYBOARD
INTERFACING
D1
D2
D3
Port 1
(Out)
Vcc
D3
D2
D1
D0
Port 2
(In)
22
Sh
sh
(cont)
An
Grounding
Rows and
Reading
Columns
ar
m
1.
Scanning Method
ul
KEYBOARD
INTERFACING
23
2.
Scanning Method
Sh
ul
sh
(cont)
An
Grounding
Rows and
Reading
Columns
ar
m
KEYBOARD
INTERFACING
24
3.
Scanning Method
Grounding
Rows and
Reading
Columns
Sh
ar
m
KEYBOARD
INTERFACING
sh
An
4.
ul
(cont)
KEYBOARD
INTERFACING
Start
Scanning Method
Grounding
Rows and
Reading
Columns
no
ar
m
All keys
down?
yes
Sh
(cont)
An
sh
ul
All keys
open?
no
yes
no
All keys
down?
yes
2
26
KEYBOARD
INTERFACING
Scanning Method
Grounding
Rows and
Reading
Columns
All keys
down?
Sh
ar
m
no
An
sh
ul
(cont)
yes
27