Case Study_LCD_Keypad 2
Case Study_LCD_Keypad 2
Case Study_LCD_Keypad 2
Case Study:
Interfacing LCDs &
Keypads
Purpose
The purpose of this lecture is to train the students on
interfacing input and output devices that have special
interfacing requirements.
5
LCD Samples
16x2 characters
(More characters and 5x8 font size
lines are also available) (5x11 is another size)
6
LCD Pin Configuration
LCD displays differ in their sizes (number of lines, number
of characters per line) and number of pins.
Here, we use the LM016L LCD display
PIN Function
VSS Ground
VDD 5V Vcc
VEE Contrast control
RS Command mode (0) or Data mode (1)
RW Read (1) or Write (0)
E Enable
D7-D0 8-bit I/O
7
LM016L
When RS pin is cleared, data placed on the D bus are
considered as commands. Examples of commands are:
Clear display screen (0x01)
Shift display right (0x05) or left (0x07)
Display off, cursor off (0x08)
Display on, cursor on (0x0A)
9
Enable (E)
Read Mode
10
epulse Function
Write a C function that generates the enable pulse (high-
to-low). Add a delay of 5ms after each change in signal
state (you can call my_delay function).
Assume that E pin is connected to PC5. You do NOT have
to set the direction of Port C, since this will be done in the
main program later. Make sure not to alter the other pins
in Port C.
void epulse ()
{
}
11
4-bit Mode
We need at least 11 pins to interface
AVR to LM016L (RS, E, RW, D7 ... D0).
However, the ATmega8 microcontroller
has limited number of I/O pins.
}
13
4-bit Mode
In order to use LM016L in 4-bit mode, it has to be reset
and initialized properly.
14
ResetLCD Function
Write a C function that resets the LM016L.
First, the function switches to Command mode by
clearing RS pin.
Then, the function transfers the sequence 0x03, 0x03,
0x03, 0x02 using the SendChar() function.
Then, the function switches back to Data mode by setting
RS.
Assume that RS is connected to pin PC4. Make sure not
to alter the other pins in Port C.
void ResetLCD ()
{
}
15
LCD Initialization
The LM016L display supports a number of features that
can be enabled/disabled through Commands.
Below are few examples:
Function Set
D7 D6 D5 D4 D3 D2 D1 D0
0 0 1 DL N F - -
16
LCD Initialization
Display ON/OFF
D7 D6 D5 D4 D3 D2 D1 D0
0 0 0 0 1 D C B
Entry Mode
D7 D6 D5 D4 D3 D2 D1 D0
0 0 0 0 0 1 I/D SH
void InitLCD ()
{
}
18
ClearDisplay Function
Write a C function that clears the display
First, the function switches to Command mode by
clearing RS pin.
Then, the functions sends the command 0x01 to clear the
display
Then, the function switches back to Data mode by setting
RS.
void ClearDisplay ()
{
}
Similarly, you can implement other functions for “Return
Home”, “Shift Display”, “Go to 2nd line”, etc.
19
DisplayText Function
This function allows you to display text on the LCD
whenever needed. You will use this function later!
void DisplayText (char string[16], uint8_t LineNo)
{
int len,count;
if(LineNo==1)
SendChar(0x80); // Move cursor to Line 1
else
SendChar(0xC0); // Move cursor to Line 2
len = strlen(string);
for (count=0; count<len; count++)
SendChar(string[count]);
} 20
TestLCD Function
Fill in the blanks in the following function. Make use
of the functions that you wrote earlier!
void TestLCD ()
{
// Set the direction of Port C (See slide 12)
21
Keypad
Keypads
An input device that is made up of push buttons
F1 F2 F3 F4
F5 F6 F7 F8
24
Reading Keypad
Reading keypad input requires two steps:
Step #1: Scanning the keypad to detect a key press
Step #2: Encoding the pressed key (e.g. F1 = “0”, F15 = “#”)
F1 F2 F3 F4
F5 F6 F7 F8
25
Keypad Scanning
Keys are active low: when pressed (0), when released (1)
Rows (R1 – R4) are input to keypad, while Columns (C1 –
C4) are outputs.
R1
C1
26
Keypad Scanning
We assume that Rows are connected to the AVR as outputs,
while Columns as inputs.
Process of scanning the keypad:
Supply (0) to R1, (1) to other rows.
Check if any column is producing (0). If yes, then key at that intersection is
pressed (i.e. key press is detected)
Otherwise, Supply R2 with (0), (1) to other rows and repeat the process until all
rows are scanned.
F1 F2 F3 F4
F5 F6 F7 F8
29
Keypad Encoding
You can develop a C function that encodes the pressed key
into a value, operation, or command as desired.
F1 7
F2 8
...
F16 +
F1 F2 F3 F4
F5 F6 F7 F8
30
MM74C922 (Key Encoder)
Scanning the keypad is a tricky process that requires
careful implementation in order to correctly detect and
identify the pressed key
ISR(INT0_vect)
{
}
32
Calculator Application
Write a main function that implements the following tasks.
Make sure to use all the functions that you developed
earlier:
Reset and initialize the LCD
Configure the interrupt on INT0
Prompt the user to enter a number. Use ISR function to
store value.
Prompt the user to enter a second number. Use ISR
function to store value.
Prompt the user to select the operation (+, -, x, or ), and
store it in a global variable.
Calculate the operation and display the output on the LCD
display.
33