An
sh
ul
Sh
ar
m
LCD AND KEYBOARD
INTERFACING
LCD is finding widespread use
replacing LEDs
ul
Sh
Ease of programming for characters and
graphics
An
LCD Operation
The declining prices of LCD
The ability to display numbers,
characters, and graphics
Incorporation of a refreshing controller
into the LCD, thereby relieving the CPU of
the task of refreshing the LCD
ar
m
sh
LCD
INTERFACING
Pin Descriptions for LCD
Descriptions
VSS
--
Ground
VCC
--
+5V power supply
VEE
--
Power supply to control contrast
RS
RS=0 to select command register,
RS=1 to select data register
R/W
E
ar
m
I
Sh
I/O
R/W=0 for write,
R/W=1 for read
I/O
Enable
DB0
I/O
The 8-bit data bus
DB1
I/O
The 8-bit data bus
DB2
I/O
The 8-bit data bus
10
DB3
I/O
The 8-bit data bus
11
DB4
I/O
The 8-bit data bus
12
DB5
I/O
The 8-bit data bus
13
DB6
I/O
14
DB7
I/O
The 8-bit data bus
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
The 8-bit data bus
3
LCD Command Codes
Code (Hex) Command to LCD Instruction Register
2
Return home
Decrement cursor (shift cursor to left)
Increment cursor (shift cursor to right)
Shift display right
Shift display left
Display off, cursor off
Display off, cursor on
Sh
ar
m
Clear display screen
Display on, cursor off
Display on, cursor blinking
ul
Display on, cursor blinking
sh
LCD Command
Codes
10
Shift cursor position to left
14
Shift cursor position to right
18
Shift the entire display to the left
1C
Shift the entire display to the right
80
Force cursor to beginning to 1st line
C0
Force cursor to beginning to 2nd line
38
2 lines and 5x7 matrix
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
//PORTD =LCD data pins
//rs PORTB. 0
//rw PORTB .1
//en PORTB. 2
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
;calls a time delay before sending next data/command
;PD.0-PD.7 are connected to LCD data pins D0-D7
;PB.0 is connected to RS pin of LCD
;PB.1 is connected to R/W pin of LCD
;PB.2 is connected to E pin of LCD
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.
//both ports Band Das output
//enable idle low
//init. LCD 2 lines, 5x7 matrix
//display on, cursor on
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)
//shift cursor right
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
void lcddata(unsigned char value)
{
ldata = value;
//put the value on the pins
rs = 1;
rw = 0;
en = 1;
//strobe the enable pin
MSDelay(1);
en = 0;
}
void MSDelay(unsigned int itime)
{
unsigned inti, j;
ul
for(i=0; i<itime; i++)
An
sh
for(j=0; j<135; j++);
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
//PORTD =LCD data pins
//rs PORTB. 0
//rw PORTB .1
//en PORTB. 2
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
;PD.0-PD.7 are connected to LCD data pins D0-D7
;PB.0 is connected to RS pin of LCD
;PB.1 is connected to R/W pin of LCD
;PB.2 is connected to E pin of LCD
An
LCD
INTERFACING
//both ports B and D as output
//enable idle low
//long delay
//long delay
//check the LCD busy flag
//check the LCD busy flag
//check the LCD busy flag
//line 1, position 6
//check the LCD busy flag
//check the LCD busy flag
//check the LCD busy flag
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
void lcddata(unsigned char value)
{
ldata = value;
//put the value on the pins
rs = 1;
rw = 0;
en = 1;
//strobe the enable pin
MSDelay(1);
en = 0;
}
void lcdready ()
{
TRISD = 0xFF;
//make PORTD an input
rs = 0;
rw = 1;
do
//wait here for busy flag
{
en = 1;
//strobe the enable pin
MSDelay(1);
en = 0;
}while(busy==1);
TRISD = 0;
}
void MSDelay(unsigned int itime)
{
unsigned inti, j;
for(i=0;i<itime;i++)
for(j=0; j<135; j++);
}
LCD Timing for Read
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
tD = Data output delay time
tAH = Hold time after E has
come down for both RS and
R/W = 10 ns (minimum)
tAS = Setup time prior to E
(going high) for both RS and
R/W = 140 ns (minimum)
Note : Read requires an L-to-H pulse for the E pin
11
LCD Timing for Write
a
ar
m
tH
tAS
tPWH
tAH
Sh
R/W
tH = Data hold time
= 10 ns (minimum)
tDSW
RS
ul
(cont)
Data
sh
Sending Codes
and Data to
LCDs w/ Busy
Flag
tDSW = Data set up time
= 195 ns (minimum)
An
LCD
INTERFACING
tAH = Hold time after E has
come down for both RS and
R/W = 10 ns (minimum)
tPWH = Enable pulse width
= 450 ns (minimum)
tAS = Setup time prior to E
(going high) for both RS and
R/W = 140 ns (minimum)
12
RS
R/W
DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
1
AAAAAAA=000_0000 to 010_0111 for
line1
Y AAAAAAA=100_0000 to 110_0111 for
line2
An
sh
ul
Sh
The upper address
range can go as
high as 0100111
for the 40character-wide
LCD, which
corresponds to
locations 0 to 39
LCD Data Sheet
One can put data at any location in the
LCD and the following shows address
locations and how they are accessed
ar
m
LCD
INTERFACING
DB7
Line1 (min) 1
Line1 (max) 1
Line2 (min) 1
Line2 (max) 1
DB6
DB5
DB4
DB3
DB2
DB1
DB0
LCD Addressing for the LCDs of 402 size
13
Keypad
ar
m
A keypad is simply an array of push buttons
connected in rows and columns.
An
sh
ul
Sh
Two processes of keypad programming:
1. key press detection
1. Interrupt method
2. Scanning method
2. key identification.
Ke y bo ards are o rgani ze d i n a
ma t r i x o f rows and columns
The CPU accesses both rows and
columns through ports
ar
m
Therefore, with two 8-bit ports, an 8 x 8 matrix
of keys can be connected to a microprocessor
Sh
When a key is pressed, a row and
a column make a contact
sh
ul
Otherwise, there is no connection between
rows and columns
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
Key identification is done in following way:
1. To make sure that the preceding key has been released, 0 s are
output to all rows at once, and the columns are read and checked
repeatedly until all the columns are HIGH. When all columns are
found to be HIGH, the program waits for a short amount of time
before it goes to the next stage of waiting for a key to be pressed.
2. To see if any key is pressed, the columns are connected to the
PORTB-Change interrupt. Therefore, any key press will cause an
interrupt and the microcontroller will execute the ISR. The ISR must do
two things: (a) ensure that the first key press detection was not
erroneous due to spike noise, and (b) wait 20 ms to prevent the same
key press from being interpreted as multiple key presses.
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
and the columns are connected to an
input port
Matrix Keyboard Connection to ports
Vcc
3
D2
An
If all the rows are
grounded and a key
is pressed, one of
the columns will
have 0 since the key
pressed provides the
path to ground
D3
Port 1
(Out)
sh
D1
ul
D0
ar
m
Scanning Method
A 4x4 matrix connected to two
ports The rows are connected to an output port
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
If the data read from columns is D3 D0
= 1111, no key has been pressed and the
process continues till key press is detected
If one of the column bits has a zero,
this means that a key press has
occurred
ul
Scanning Method
It is the function of the microcontroller
to scan the keyboard continuously to
detect and identify the key pressed
To de t e c t a pre s s e d ke y, t he
mi c ro c o nt ro l ler gro und s a l l
ro w s by prov i di ng 0 t o t he
o ut put l at c h, t he n i t re a d s t he
co l um ns
sh
An
KEYBOARD
INTERFACING
For example, if D3 D0 = 1101, this means that
a key in the D1 column has been pressed
After detecting a key press, microcontroller will
go through the process of identifying the key 20
Scanning Method
It reads the columns, if the data read is
all 1s, no key in that row is activated and
the process is moved to the next row
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
This process continues until the row
is identified
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
After identification of the row in which
the key has been pressed
Y
Find out which column the pressed key
belongs to
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
When all columns are found to be high, the
program waits for a short amount of time
before it goes to the next stage of waiting for
a key to be pressed
sh
(cont)
An
Grounding
Rows and
Reading
Columns
To make sure that the preceding key has
been released, 0s are output to all rows
at once, and the columns are read and
checked repeatedly until all the columns
are high
ar
m
1.
Scanning Method
For detection and identification of
key activation goes through the
following stages:
ul
KEYBOARD
INTERFACING
23
To see if any key is pressed, the columns
are scanned over and over in an infinite
loop until one of them has a 0 on it
2.
Scanning Method
Sh
ul
sh
(cont)
An
Grounding
Rows and
Reading
Columns
Remember that the output latches connected
to rows still have their initial zeros (provided
in stage 1), making them grounded
After the key press detection, it waits 20 ms
for the bounce and then scans the columns
again
(a) it ensures that the first key press
detection was not an erroneous one due a
spike noise
(b) the key press. If after the 20-ms delay the
key is still pressed, it goes back into the
loop to detect a real key press
ar
m
KEYBOARD
INTERFACING
24
3.
Scanning Method
To detect which row key press belongs to,
it grounds one row at a time, reading the
columns each time
Grounding
Rows and
Reading
Columns
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
ASCII) for that row
Sh
ar
m
KEYBOARD
INTERFACING
sh
To identify the key press, it rotates the
column bits, one bit at a time, into the
carry flag and checks to see if it is low
An
4.
ul
(cont)
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
25
KEYBOARD
INTERFACING
Flowchart for Program 12-4
Read all columns
Start
Scanning Method
Grounding
Rows and
Reading
Columns
no
ar
m
Ground all rows
All keys
down?
yes
Read all columns
Sh
Wait for debounce
(cont)
An
sh
ul
All keys
open?
no
Read all columns
yes
no
All keys
down?
yes
2
26
KEYBOARD
INTERFACING
Scanning Method
Grounding
Rows and
Reading
Columns
Ground next row
All keys
down?
Sh
ar
m
no
An
sh
ul
(cont)
yes
Find which key
is pressed
Get scan code
from table
Return
27