AN2034
January 16, 2009 Document No. 001-40409 Rev. *A 3
Code 1: Subroutine for Reading Keypad
;-----------------------------------------
; Keypad.asm
;
; This routine reads a 4 column by 4 row
; keypad on port1. The status of key
; closures is returned in A.
;
; P1.4 P1.5 P1.6 P1.7
; C0 C1 C2 C3
; P1.0 R0 --+----+----+----+-
; | | | |
; P1.1 R1 --+----+----+----+-
; | | | |
; P1.2 R2 --+----+----+----+-
; | | | |
; P1.3 R3 --+----+----+----+-
;
;------------------------------------------
export bReadKeypad
export _bReadKeypad
include "m8c.inc"
bReadKeypad:
_bReadKeypad:
mov reg[PRT1DR], f0h ;drive columns
mov X,SP
mov A, reg[PRT1DR] ;read rows
mov reg[PRT1DR], 0fh ;drive rows
push A ;store row info on stack
mov A, reg[PRT1DR] ;Read Columns
and [X], A ;combine them
pop A
ret
The C header shown in example Code 2 can be found in
Keypad.h.” It makes the subroutine shown in example
Code 1 a ‘C’ callable function.
Code 2. C Header Example
// Create a pragma to support
// proper argument and return
// value passing
#pragma fastcall bReadKeypad
extern BYTE bReadKeypad(void);

Using the Output

The output of the function bReadKeypad is a single byte
that shows the status of key closure of the keypad. It is
translated and decoded as follows:
No bits are set if no key is pressed. A single bit in the upper nibble and a single bit in the
lower nibble are set for a single-key press.
Any other condition is a multiple-key closure and is
defined as not valid.
These rules can be decoded with discrete conditional code
that breaks up the byte into two nibbles to determine row
and column information. Use this information to determine
which key, if any, was pressed. This results in a complex
set of rules and is tedious.
Another scheme is to use a lookup table to decode this
data. The advantage is that the table stores the formatted
data. Different programmers could be working on the
same project and each use their own table to decode the
keypad when they are required to read it.
The project file associated with this application note uses
such a table to decode the key closures. A block diagram
of the project is shown in Figure 5.
Figure 5. Block Diagram for the Keypad Project
213
546
879
0*#
R0
R1
R2
R3
C2
C1
C0P1.4
P1.5
P1.3
P1.0
P1.1
P1.2
P1.6 PSoC

MCU

b
c
a
g
d
f
e
P0.6
P0.5
P0.4
P0.3
P0.2
P0.1
P0.0
Vdd(common anode)
a cathode
b cathode
c cathode
d cathode
e cathode
f cathode
g cathode
MAN71A
For this project, set the drives for the port 1 pins to the Pull
Down mode (default) and the port 0 pins to either the Pull
Down or Strong mode.
The keypad is scanned and the appropriate bits are set on
the output port to turn on the desired LED segments. This
display is a single digit 7-segment common anode LED
display. Any particular segment is lit when its cathode is
pulled low. As an example, all output pins low result in an
“8” being displayed. All output pins high result in a blank
display.
[+] Feedback [+] Feedback