beautypg.com

Using the output, No bits are set if no key is pressed, Psoc mcu – Cypress PSoC Style AN2034 User Manual

Page 3

background image

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

2

1

3

5

4

6

8

7

9

0

*

#

R

0

R

1

R

2

R

3

C

2

C

1

C

0

P

1.4

P

1.5

P

1.3

P

1.0

P

1.1

P

1.2

P

1.6

PSoC

MCU

b

c

a

g

d

f

e

P

0.6

P

0.5

P

0.4

P

0.3

P

0.2

P

0.1

P

0.0

V

dd

(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