Example Code 3 is the main function that implements the design in Figure 5 on page 3.
Code 3. Keypad Project Implemented
;This program reads the keypad
;at port 1 and control the LEDs
;on port0.
;
;Copyright (c)
;Cypress Semiconductor
;All Rights Reserved.
include "m8c.inc" export _main
_main: loop:
call bReadKeypad index KeyToLED
mov reg[PRT0DR], A jmp loop
ret
xxh: equ 30h ;illegal character “E”
.Literal
KeyToLED:
db 7fh,xxh,xxh,xxh,xxh,xxh,xxh,xxh db xxh,xxh,xxh,xxh,xxh,xxh,xxh,xxh db xxh,4fh,4ch,xxh,0fh,xxh,xxh,xxh db 78h,xxh,xxh,xxh,xxh,xxh,xxh,xxh db xxh,12h,24h,xxh,00h,xxh,xxh,xxh db 01h,xxh,xxh,xxh,xxh,xxh,xxh,xxh db xxh,xxh,xxh,xxh,xxh,xxh,xxh,xxh db xxh,xxh,xxh,xxh,xxh,xxh,xxh,xxh db xxh,06h,20h,xxh,04h,xxh,xxh,xxh db 18h,xxh,xxh,xxh,xxh,xxh,xxh,xxh db xxh,xxh,xxh,xxh,xxh,xxh,xxh,xxh db xxh,xxh,xxh,xxh,xxh,xxh,xxh,xxh db xxh,xxh,xxh,xxh,xxh,xxh,xxh,xxh db xxh,xxh,xxh,xxh,xxh,xxh,xxh,xxh db xxh,xxh,xxh,xxh,xxh,xxh,xxh,xxh db xxh,xxh,xxh,xxh,xxh,xxh,xxh,xxh
.EndLiteral
The “.Literal” and “.EndLiteral” macros are used to disable the compiler’s code compression algorithms so that the literal information in the KeyToLED table is not compressed.
This code can also be found in the project associated with this application note. The table makes for a simple translation from key press to displayed character. Develop your own table for a particular application.
AN2034
Debouncing
Debouncing solutions are specific to each system’s program structure and switch selection. However, debouncing is simple; it requires two sequential keypad scans to agree for the scan to be valid.
Interrupt Driven Keypad Scans
Execute the following steps for an
Configure the lower four (row) pins to be I/O interrupts on a rising edge.
Set the upper four (column) pins high.
Write an interrupt handler that calls bReadKeypad.
Enable the GPIO interrupt.
The row pins stay low until a key press connects a column to a row causing an interrupt.
One possible application is a security gate keypad reader where the MCU is placed in the
Summary
The PSoC microcontroller GPIO structure is ideal for fast keypad scans. The “index” instruction enables simple translation key presses to a user’s desired format. The GPIO to interrupts makes for an
January 16, 2009 | Document No. | 4 |
[+] Feedback