Case Study_LCD_Keypad 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

ECCE4227: Embedded Systems

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.

 The lecture is split into the following components:


 System layout
 Requirements of LCD and Keypad
 Development of software
 Pin configurations and interfacing

 Students are expected to participate and workout the software


design with some support from the instructor.
 You will be requested to write functions that will be used later
to interface the LCD and keypad
2
System Layout

 Consider the layout below, where an LCD and a Keypad


are interfaced to the microcontroller.

 The main objective is to develop a C program for the


microcontroller, such that it reads input from keypad and
displays output to LCD.
3
LCD
LCD
 Liquid Crystal Display (LCD)

 LCDs have replaced LEDs in several application domains


for the following reasons:
 Low price
 More flexible in terms of displaying number, characters, and graphics.
 LCDs contain a refreshing controller, which relieves the CPU from
refreshing the contents of the LCD.
 Ease of programming for characters and graphics.

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)

 However, when RS is set, then the D bus is used to


exchange data

 LCD display can be written to (RW = 0) or


read from (RW = 1)

 In order to read or write, the enable pin (E) must be supply


high-to-low pulse.
8
Enable (E)
 Write Mode

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.

 LM016L supports 4-bit mode, where PC4


8-bit data can be transferred as two PC5
nibbles (4-bit data), sending 4 MSb first.

 4-bit mode reduces number of PC0


PC1
pins to 7. PC2
PC3

 Furthermore, if LCD will never be read,


then RW can be connected to ground,
reducing number of pins to 6. 12
SendChar Function
 Write a C function that accepts a char. The received char
will be sent as two nibbles, where the 4 MSb is sent first.
 After sending each nibble, call epulse() in order to latch
the data.
 Assume that D7-D4 are connected to PC3-PC0.
 Make sure to not to alter the other pins in Port C.

void SendChar (char D)


{

}
13
4-bit Mode
 In order to use LM016L in 4-bit mode, it has to be reset
and initialized properly.

 Resetting the LM016L requires sending the following


sequence in order (0x03, 0x03, 0x03, 0x02).
 The first 3 (0x03) is to reset the LCD
 0x02 indicates 4-bit mode. If 0x03 is sent then 8-bit mode.

 Initializing the LM016L, involves a number of


configurations that we will discuss later.

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 - -

 Data Length (DL): (0  4-bit mode 1  8-bit mode)


 Number of display lines (N): (0  1 line 1  2 lines)
 Font type (F): (0  5x8 1  5x11)

16
LCD Initialization
 Display ON/OFF
D7 D6 D5 D4 D3 D2 D1 D0
0 0 0 0 1 D C B

 Display (D): 0  Display OFF 1  Display ON


 Cursor (C): 0  No cursor 1  Cursor ON
 Blinking of cursor (B): 0  blink OFF 1  Blinking cursor

 Entry Mode

D7 D6 D5 D4 D3 D2 D1 D0
0 0 0 0 0 1 I/D SH

 Increment, Decrement (I/D): 0  cursor moves left 1 cursor moves


right
 Shift of display (SH): 0  Do not shift display 1 Shift according to I/D
17
InitLCD Function
 Write a C function that initializes the LM016L.
 First, the function switches to Command mode by
clearing RS pin.
 Feature Set: 4-bit mode, 2 lines, 5x8 font
 Display: Turn ON display, Turn OFF cursor and blinking
 Entry mode: automatically move to right, no shifting
 Then, the function switches back to Data mode by setting
RS.

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;

PORTC &= ~(1<<4); // Switch to Command mode

if(LineNo==1)
SendChar(0x80); // Move cursor to Line 1
else
SendChar(0xC0); // Move cursor to Line 2

PORTC |= (1<<4); // Switch to Data mode

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)

// Wait for 10ms until the LCD boots

// Reset and initialize the LCD

// Display your first name on the 1st line

// Display your ID on the 2nd line

21
Keypad
Keypads
 An input device that is made up of push buttons

4x3 Keypad 4x4 Keypad

 Keypads are organized in a matrix of rows and columns


23
Keypad Organization
 The figure shows a 4x4 keypad.
 A total of 8 pins
 Four Rows (input)
 Four columns (output)

F1 F2 F3 F4

F5 F6 F7 F8

F9 F10 F11 F12

F13 F14 F15 F16

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

F9 F10 F11 F12

F13 F14 F15 F16

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.

 Assume that F1 key is pressed. This key is


the intersection of R1 & C1. Therefore, if R1
is supplied with (0), then C1 will produce (0).

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.

 Alternatively, interrupts can


be used, where rows are
grounded one after another,
and all columns are
connected to external
interrupts pins
 This will not work with
ATmega8, why? 27
Debouncing
 Similar to mechanical switches, keys bounce when
pressed and released.

 Debouncing is necessary to avoid misinterpreting the


pressed key.
 Hardware circuit can be used to debounce signal
 Alternatively, software delay (20 ms) can be added to avoid reading a
spike. In addition, this delay helps in avoiding reading the pressed key
twice.
28
Keypad Encoding
 Once a key press is detected and the key is identified, the next
step is to encode the pressed key.
 Encoding the pressed key depends on the application.
 For example, assume that the keypad is used as a calculator,
as shown in the figure below:

F1 F2 F3 F4

F5 F6 F7 F8

F9 F10 F11 F12

F13 F14 F15 F16

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

F9 F10 F11 F12

F13 F14 F15 F16

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

 The MM74C922 is a 16-key encoder


that abstracts the complexity of
interfacing 4x4 keypad to AVR.
 Keypad Rows are connected to Y bus
 Columns are connected to X bus
 Output Enable should be grounded
 The encoded output (0 – 15) will be made
available on the (D, C, B, A)
 The output is valid only when DA goes from low-to-high (i.e. rising
edge)
31
ISR(INT0_vect)Function
 Write ISR function that is invoked when INT0 external
interrupt is triggered (Signal DA of the encoder is connected
to this pin).
 Assume that the output of the MM74C922 encoder (D, C, B,
A) are connected to ping PORT D7, D6, D5, D4, respectively.
 Each time ISR function is invoked, the function translates
the pressed key into a calculator value as shown in slide 30.
 The ISR function will store the value in a global variable
“EncodedValue”

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

You might also like