0% found this document useful (0 votes)
42 views20 pages

FA21 - Lec15 - 2021-11-03 - Keypad Interfacing

The document discusses keypad interfacing with a microprocessor. It describes how a keypad works as an input device using fewer wires than buttons by scanning rows and columns to identify pressed keys. It explains a two-step process to identify keys by driving rows high and columns low, then reversing to find the specific row and column of a pressed key. An alternative technique to sequentially drive each row low while observing columns is also presented to detect the row and column of pressed keys.

Uploaded by

Mahreen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views20 pages

FA21 - Lec15 - 2021-11-03 - Keypad Interfacing

The document discusses keypad interfacing with a microprocessor. It describes how a keypad works as an input device using fewer wires than buttons by scanning rows and columns to identify pressed keys. It explains a two-step process to identify keys by driving rows high and columns low, then reversing to find the specific row and column of a pressed key. An alternative technique to sequentially drive each row low while observing columns is also presented to detect the row and column of pressed keys.

Uploaded by

Mahreen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Keypad

Interfacing
LECTURE# 15
MICROPROCESSOR SYSTEMS AND INTERFACING

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 1


Last Lecture
Context Saving in Multitasking (Interrupt and general subroutines)
Interrupt Latency
Interrupt Handling in C
LCD Interfacing
◦ 8-bit date mode (using separate data and command port)
◦ 4-bit data mode (using separate data and command port)
◦ Four bits were unused in the data port
◦ 4-bit data mode, using single port
◦ Same port for data and commands

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 2


Keypad ----
An input device
◦ Containing a bunch of buttons
◦ Used in telephones, calculators, secure entry systems
3 2 1 0

Uses less number of wires 7 6 5 4


◦ Compared to each wire for each push button
◦ Here, we have 16 buttons, but only 8 wires are needed B A 9 8

F E D C

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 3


Keypad
Key identification
Consider a key is pressed
◦ Key ‘6’ in this case,
◦ which lies at the intersection of row#2 and column #2 3 1 0
2

We need to find out the row and column 7 6 5 4


◦ On which a key is pressed
◦ To identify the key B A 9 8

F E D C

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 4


Keypad
Key identification – step 1A
Initially drive both rows and columns, for example
◦ Drive rows with logic high, and
◦ Drive columns with logic low 3 2 1 0
◦ You may postpone driving of columns, until the next step 1
7 6 5 4

Drive High
1
B A 9 8

1
F E D C

0 0 0 0
Drive Low

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 5


Keypad
Key identification – step 1B
Keep driving the columns
◦ Observe rows to find out the row on which key is pressed
◦ Row #2 in this case has key pressed 3 2 1 0
◦ row identified H
7 6 5 4

Observe
B A 9 8

H
F E D C

0 0 0 0
Drive Low
H means that the line will maintain its logic high value when undriven
0 means that the line is driven low due to a key pressed

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 6


Keypad
Key identification – step 2A
Drive both rows and columns, for example
◦ Drive rows with logic low, and
◦ You may postpone driving of rows, until the next step 3 1 0
2
◦ Drive columns with logic high 0
7 6 5 4

Drive Low
0
B A 9 8

0
F E D C

1 1 1 1
Drive High

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 7


Keypad
Key identification – step 2B
Keep driving the rows
◦ Observe columns, to find out the column on which key is pressed
◦ Column #2 in this case has key pressed 3 2 1 0
◦ Column identified 0
7 6 5 4

Drive Low
0
B A 9 8

0
F E D C

H 0 H H
Observe
H means that the line will maintain its logic high value when undriven
0 means that the line is driven low due to a key pressed

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 8


Keypad – Another Technique
Key Identification
Instead of alternatively driving
◦ Columns and rows

We can drive just rows


◦ One row at a time

Drive one by one


◦ While observing columns

If rows are being driven


◦ Keep the columns pull-up
or
Always observe
◦ briefly drive high before observing

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 9


Keypad – Another Technique
Key Identification – 1) Detect [✗]
While rows are driven low
◦ And no key is pressed
◦ All the columns will show logic high (1111)
◦ Due to pull-resistors
0

Drive Low
0

H H H H

Always observe

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 10


Keypad – Another Technique
Key Identification – 1) Detect [✓]
While rows are driven low
◦ And no key is pressed
◦ All the columns will show logic high (1111)
◦ Due to pull-resistors
0
◦ When a key is pressed
6
◦ At least one column will go low 0

Drive Low
0

H 0 H H

Always observe

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 11


Keypad – Another Technique
Key Identification – 2) Validate
While rows are driven low
◦ And no key is pressed
◦ All the columns will show logic high (1111)
◦ Due to pull-resistors
0
◦ When a key is pressed
6
◦ At least one column will go low 0

Drive Low
Wait for a few milliseconds 0

◦ To validate that a key is pressed


0
◦ Avoid glitches
H 0 H H

Always observe

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 12


Keypad – Another Technique
Key Identification – 3) Identify
When the pressed has been validated
◦ Identify the row on which they key is pressed
◦ by driving rows to zero, one at a time
◦ While other rows are driven logic high 0

If the columns show all high 1


6

Drive Low
◦ No key is pressed on this row
◦ Check next row 1

H H H H

Always observe

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 13


Keypad – Another Technique
Key Identification – 3) Identify
When the pressed has been validated
◦ Identify the row on which they key is pressed
◦ by driving rows to zero, one at a time
◦ While other rows are driven logic high 1

If the columns show all high 0


6

Drive Low
◦ No key is pressed on this row
◦ Check next row 1

If one of the columns is low 1

◦ You have identified H 0 H H

◦ The row and column

Always observe

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 14


Keypad – Another Technique
Key Identification – 4) Decode
The row and column of the key pressed
◦ Has been identified

Use a look-up table


1
◦ to figure out the key code
6
0

Drive Low
1

H 0 H H

Always observe

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 15


Steps for interfacing Keypad
Start
Steps
0. Wait for key release Ground all rows

1. Detect key press 1

2. Validate key press Wait for already Read all columns Ground next row
key press release
3. Identify key press No Read all columns
Identify the
All keys open? pressed key
4. Decode key press Yes No Any key press (Find the column
in this row?
and row of
Yes pressed key)
Detect Key press
Find which key is
Read all columns pressed
(continue only if a
key is pressed) Get the key ascii
No Any key Decode
down? or number

Yes
Return
Validate Key Wait for debounce
press
Read all columns
(continue only if
legitimate key is
No Any key Yes
pressed, and not
down?
a glitch)

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 16


Keypad
Declarations & Wait for Release
;Keyboard Program. This program sends the ASCII code
Rows and Columns ;for pressed key to Port D
;PC0-PC3 connected to columns, PC4-PC7 connected to rows
◦ Connected to single port .INCLUDE "M32DEF.INC"
.EQU KEY_PORT = PORTC
.EQU KEY_PIN = PINC
.EQU KEY_DDR = DDRC
LDI R20,HIGH(RAMEND)
OUT SPH,R20
LDI R20,LOW(RAMEND) ;init. stack pointer
0 OUT SPL,R20
1 LDI R21,0xFF

2 Columns OUT DDRD,R21


LDI R20,0xF0
PORTC

3 OUT KEY_DDR,R20
GROUND_ALL_ROWS:
4 LDI R20,0x0F
5 OUT KEY_PORT,R20
Rows WAIT_FOR_RELEASE:
6 NOP
7 IN R21,KEY_PIN ;read key pins
ANDI R21,0x0F ;mask unused bits
CPI R21,0x0F ;(equal if no key)
BRNE WAIT_FOR_RELEASE ;do again until keys released

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 17


Keypad
Detect(1) & Validate(2)
WAIT_FOR_KEY:
All rows are driven low NOP ;wait for sync. circuit
◦ Observe columns IN R21,KEY_PIN ;read key pins

Detect
ANDI R21,0x0F ;mask unused bits
CPI R21,0x0F ;(equal if no key)
BREQ WAIT_FOR_KEY ;do again until a key pressed

CALL WAIT15MS ;wait 15 ms

Validate
IN R21,KEY_PIN ;read key pins
ANDI R21,0x0F ;mask unused bits
CPI R21,0x0F ;(equal if no key)
BREQ WAIT_FOR_KEY ;do again until a key pressed

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 18


Keypad
Key Identification (3)
Drive rows one by one with logic low
◦ While all other rows are logic high

Keep observing the columns

LDI R21,0b11101111 ;ground row 0 LDI R21,0b10111111 ;ground row 2


OUT KEY_PORT,R21 OUT KEY_PORT,R21
NOP ;wait for sync. circuit NOP ;wait for sync. circuit
IN R21,KEY_PIN ;read all columns IN R21,KEY_PIN ;read all columns
ANDI R21,0x0F ;mask unused bits ANDI R21,0x0F ;mask unused bits
CPI R21,0x0F ;(equal if no key) CPI R21,0x0F ;(equal if no key)
BRNE ROW0 ;row 0, find the column BRNE ROW2 ;row 2, find the column

LDI R21,0b110111111 ;ground row 1 LDI R21,0b01111111 ;ground row 3


OUT KEY_PORT,R21 OUT KEY_PORT,R21
NOP ;wait for sync. circuit NOP ;wait for sync. circuit
IN R21,KEY_PIN ;read all columns IN R21,KEY_PIN ;read all columns
ANDI R21,0x0F ;mask unused bits ANDI R21,0x0F ;mask unused bits
CPI R21,0x0F ;(equal if no key) CPI R21,0x0F ;(equal if no key)
BRNE ROW1 ;row 1, find the column BRNE ROW3 ;row 3, find the column

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 19


Keypad
Decode
ROW0:
We have last read port value in R21 LDI R30,LOW(KCODE0<<1)
LDI R31,HIGH(KCODE0<<1)
◦ Lower nibble RJMP FIND_COL
ROW1:
Key codes are stored in program mem. LDI R30,LOW(KCODE1<<1)
LDI R31,HIGH(KCODE1<<1)
◦ Access using LPM instruction RJMP FIND_COL
ROW2:
◦ Reads program memory LDI R30,LOW(KCODE2<<1)
◦ location pointed by Z-register (R31:R30) LDI R31,HIGH(KCODE2<<1)
RJMP FIND_COL
ROW3:
LDI R30,LOW(KCODE3<<1)
LDI R31,HIGH(KCODE3<<1)
RJMP FIND_COL
FIND_COL:
LSR R21
BRCC MATCH ;if Carry is low go to match
LPM R20,Z+ ;INC Z
.ORG 0x300
RJMP FIND_COL
KCODE0: .DB '0','1','2','3' ;ROW 0
MATCH:
KCODE1: .DB '4','5','6','3' ;ROW 1
LPM R20,Z
KCODE2: .DB '8','9','A','B' ;ROW 2
OUT PORTD,R20
KCODE3: .DB 'C','D','E','F' ;ROW 3
RJMP GROUND_ALL_ROWS

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 20

You might also like