PICAPPS.PDF
(
334 KB
)
Pobierz
Note 1: Interfacing a Matrix Keypad
Introduction.
This application note covers the use of matrix-encoded
keypads with PIC microcontrollers. It presents an example program in
Parallax assembly language for reading a 4 x 4 keypad.
Background.
In order to use as few input/output (I/O) pins as
possible, most keypads of eight or more switches are wired in a matrix
arrangement. Instead of interfacing each set of contacts to an I/O pin,
switches are wired to common row and column connections, as shown
in the figure. For a given number of switches, this method can save quite
a few I/O pins:
No. of
Switches
8
12
16
20
Matrix
(rows x columns)
2x4
3x4
4x4
5x4
I/O Pins
Required
6
7
8
9
The disadvantage of matrix encoding is that it requires some additional
programming. The listing shows a program that reads a 4 x 4 keypad
using an 8-bit I/O port and presents the number of the key pressed on
a 4-bit port.
How it works.
Code at the beginning of the program sets the lower four
bits of port
RB
(connected to the columns of the keypad) to output, and
the upper four bits (rows) to input. It also sets port
RA
, which will drive
LEDs indicating the binary number of the key pressed, to output.
The routine
scankeys
does the actual work of reading the keypad. It
performs the following basic steps:
• Assert a “1” on the current column.
• Does the “1” appear on the current row?
> No: increment key and try the next row.
> Yes: exit the subroutine.
• Try the next column.
If the first key, key 0, is pressed, the routine exits with a 0 in the variable
key,
because it ends before the first increment instruction. If no key is
pressed, the variable
key
is incremented 16 (010h) times. Therefore, this
PIC16Cxx Applications Handbook 1.1 • Parallax, Inc. • (916) 624-8333 • Page 85
Note 1: Interfacing a Matrix Keypad
number serves as a flag to the calling program that no key has been
pressed.
On the issue of switch debouncing: After a switch is pressed, it may take
20 milliseconds or more for it to settle into its new state. That means that
one key press can register as many repeated presses. The easiest way to
defeat this problem is to read the switches, and then wait a while before
reading them again. That’s the purpose of the
:delay
loop in the main
program.
470
470
470
1
18
LED
LED
LED
LED
470
RA2
2
RA1
17
You may want to add 1k series resistors in
each column (RB0 - RB3). This prevents
direct shorting if two or more buttons are
pressed at the same time.
Vcc
3
4
5
6
7
8
9
RA3
RTCC
RA0
16
220 pF
15
OSC1
10k
14
13
12
MCLR
PIC
OSC2
16C54
Vss
RC/P
Vdd
RB0
RB1
RB2
RB3
RB7
RB6
Vcc
10k
10k
10k
10k
11
RB5
10
RB4
Modifications.
In circumstances where electrical noise might be a
problem, Microchip’s data sheet on the PIC indicates that it might be
wise to move the port I/O assignments to the beginning of
scankeys.
The
reason is that electrostatic discharge (ESD) from the user’s fingertips, or
presumably any other strong electrical noise, could corrupt an I/O
control register and switch an input pin to output. This would prevent
the routine from reading one or more rows of the keypad.
Page 86 • PIC16Cxx Applications Handbook 1.1 • Parallax, Inc. • (916) 624-8333
Note 1: Interfacing a Matrix Keypad
A milder zap could conceivably cause a false input to the keypad. Some
routines check for this condition by comparing two or more consecutive
readings of the keys. Unless several readings match, no data is returned
to the main program.
Program listing.
This program may be downloaded from the Parallax
BBS as
KEYPAD
.
SRC
. You can reach the BBS at (916) 624-7101.
; PROGRAM: KEYPAD
; This program demonstrates a simple method for scanning a matrix-encoded
; keypad. The 4 columns of the pad are connected to
RB
.0 through
RB
.3; 4 rows to
;
RB
.4 through
RB
.7. The scanning subroutine returns the code of key pressed
; (0h—0Fh) in the variable key. If no switch is pressed, the out-of-range value 10h is
; returned in key (this avoids the use of a separate flag variable).
keypad
row1
row2
row3
row4
=
=
=
=
=
rb
rb.4
rb.5
rb.6
rb.7
; Variable storage above special-purpose registers.
org
8
cols
ds
1
key
ds
1
index
ds
1
; Remember to change device info if programming a different PIC.
device
pic16c54,rc_osc,wdt_off,protect_off
reset
start
; Set starting point in program ROM to zero.
org
0
start
mov
!rb, #11110000b
mov
!ra, #0
:keys
call
scankeys
cje
key, #16, :delay
mov
ra, key
:delay
nop
nop
djnz
index,:delay
goto
:keys
; cols out, rows in
; ra all outputs
; Subroutine to scan the keypad. Assumes that the calling routine will delay long
; enough for debounce.
scankeys
clr
key
PIC16Cxx Applications Handbook 1.1 • Parallax, Inc. • (916) 624-8333 • Page 87
Note 1: Interfacing a Matrix Keypad
clr
mov
setb
keypad
cols,#4
c
; 4 x 4 keypad
; put a 1 into carry
; On the first time through the following loop, the carry bit (1) is pulled into keypad.
; On subsequent loops, the lone 1 moves across the column outputs.
:scan
rl
keypad
clrb
c
; follow the 1 with zeros
jb
row1, press
; If a 1 is detected, quit the routine with the current value of key. If row1, column1 is
; pressed, the value of key is 0 (on the first loop). If not, increment key and try the
; next row.
inc
key
jb
row2, press
inc
key
jb
row3, press
inc
key
jb
row4, press
inc
key
djnz
cols, :scan
; Try all 4 columns.
press
ret
; Return with value in key.
; If no key is pressed, the value will be 10h due to execution of the final increment
; instruction. The program should interpret this out-of-range value as ‘no press.’
Page 88 • PIC16Cxx Applications Handbook 1.1 • Parallax, Inc. • (916) 624-8333
BLANK PAGE
PIC16Cxx Applications Handbook 1.1 • Parallax, Inc. • (916) 624-8333 • Page 89
Plik z chomika:
fred1144
Inne pliki z tego folderu:
PICAPPS.PDF
(334 KB)
PICKEZDO.PDF
(77 KB)
PICTANF.ZIP
(97 KB)
Inne foldery tego chomika:
Zgłoś jeśli
naruszono regulamin