Vous êtes sur la page 1sur 43

Keypads

ECET 209 – Lecture 13


Introduction to Microcontrollers
Overview

• Keypads
• Review the Bitwise C Operators to interface
with Hardware

ECET 209 Purdue University 2


Keypads

ECET 209 Purdue University 3


How Do Keypads Work?

ECET 209 Purdue University 4


How Do Keypads Work?

ECET 209 Purdue University 5


How Do Keypads Work?

ECET 209 Purdue University 6


How Do Keypads Work?

ECET 209 Purdue University 7


Scanning a Keypad

ECET 209 Purdue University 8


Scanning a Keypad

High
High
High

High

ECET 209 Purdue University 9


Scanning a Keypad
Pull one line
low at a time

High
High
High

High

ECET 209 Purdue University 10


Scanning a Keypad

Low Low Low Low

High
High
High

High

ECET 209 Purdue University 11


Scanning a Keypad

Low

High
High
High

High

ECET 209 Purdue University 12


Scanning a Keypad

Low

High
High
High

High

ECET 209 Purdue University 13


Scanning a Keypad
Low vs. High
Low

High
High
High

High

ECET 209 Purdue University 14


Scanning a Keypad

Low

Low
High
High
High

High

ECET 209 Purdue University 15


Scanning a Keypad w/ Micro

Microcontroller

ECET 209 Purdue University 16


Buy a Chip!!

ECET 209 Purdue University 17


Fairchild MM74C922

ECET 209 Purdue University 18


Fairchild MM74C922

ECET 209 Purdue University 19


Fairchild MM74C922

ECET 209 Purdue University 20


How does an encoded keypad work?

• Encoder Chip constantly scans the keypad


• When any key is pressed:
– DAV goes HIGH
– 4-bit data is Binary for the key pressed
• When key is released:
– DAV goes LOW
– Data removed from data lines.

ECET 209 Purdue University 21


Press Release Pressing “5”
DAV
DATA 0101

74922
KEYPAD
ENCODER
C D E F 1
DAV
8 9 A B
4 5 6 7 D3 0
D2 1
0 1 2 3 D1 0
D0 1
ECET 209 Purdue University 22
Keypad Example

• Create a program that will read the value on


the keypad when a key is pressed.

– Check for a key to be pressed


– Read the value

ECET 209 Purdue University 23


Keypad Example
• Create a program
that will read the
value on the
keypad when a
key is pressed.

– Check for a key to


be pressed
– Read the value
ECET 209 Purdue University 24
Keypad Example

• Create a program that will wait for a key on


the keypad to be pressed and then read the
value.

– Wait for a key to be pressed


– Read the value

ECET 209 Purdue University 25


Keypad Example
• Create a program
that will wait for a
key on the keypad
to be pressed and
then read the
value.

– Wait for a key to


be pressed
– Read the value
ECET 209 Purdue University 26
Keypad Example

• Create a program that will wait for a key to


be pressed, then read the value on the
keypad. After reading the value on the
keypad, wait until the key is released before
continuing.

– Wait for a key to be pressed


– Read the value
– Wait for the key to be released
ECET 209 Purdue University 27
Keypad Example

– Wait for a key


– Read the key
– Wait for release

ECET 209 Purdue University 28


Flowchart vs. Application

ECET 209 Purdue University 29


I/O Hookup

74922
KEYPAD
ENCODER

DAV PORTA.4

D3 PORTA.3
D2 PORTA.2
D1 PORTA.1
D0 PORTA.0
ECET 209 Purdue University 30
Masking for DAV

PORT Value xxx1xxxx


“MASK” AND 00010000
00010000

ECET 209 Purdue University 31


Masking for Data

PORT Value xxxx1111


“MASK” AND 00001111
00001111

ECET 209 Purdue University 32


Solution Steps

• For Lab #5
– Wait for a key to be pressed (DAV goes high)
– Read the key value
– Wait for key release (DAV goes low)
– Place the value on the LEDs
– Send the value out the serial port
– Loop back to the Top

ECET 209 Purdue University 33


Flowchart vs. Application

ECET 209 Purdue University 34


Handshaking
• CodeVisionAVR allows direct control or
access to the port pins for input and output

– Symbolically refer to port pins


– Can allow for easier to read code*

PORTC.7 = 0; // turn on MSB


if( PINB.0 == 1) // test LSB

ECET 209 Purdue University 35


Handshaking with the Keypad

while ( (PINA & 0x10) == 0) while ( PINA.4 == 0)


key = PINA & 0x0F; key = PINA & 0x0F;
while ( (PINA & 0x10) != 0) while (PINA.4 != 0)

ECET 209 Purdue University 36


Handshaking with the Keypad

while ( (PINA & 0x10) == 0) while ( PINA.4 == 0)


key = PINA & 0x0F; key = PINA & 0x0F;
while ( (PINA & 0x10) != 0) while (PINA.4 != 0)

while (PINA.4 == 1)

ECET 209 Purdue University 37


Press Release

DAV
DATA 0101

#define DAV PINA.4


while( DAV == 0 )
{
}
ECET 209 Purdue University 38
Press Release

DAV
DATA 0101

#define DAV PINA.4


while( DAV == 0 )
{
}
ECET 209 Purdue University 39
Press Release

DAV
DATA 0101

#define KEYPAD_DATA (PINA & 0x0F)

key = KEYPAD_DATA;
ECET 209 Purdue University 40
Press Release

DAV
DATA 0101

#define DAV PINA.4


while( DAV == 1 )
{
}
ECET 209 Purdue University 41
Keypad Handshaking

Wait for Dav Wait for


Not Dav

Read the Key


Wait for Dav

Wait for
Not Dav Read the Key

ECET 209 Purdue University 42


Solution Steps

• For Lab
– Wait for key release (DAV goes low)
– Wait for a key to be pressed (DAV goes high)
– Read the key value
– Place the value on the LEDs
– Send the value out the serial port
– Loop back to the Top

ECET 209 Purdue University 43

Vous aimerez peut-être aussi