APPLICATION NOTE ST7 KEYPAD DECODING TECHNIQUES, IMPLEMENTING WAKE-UP ON KEYSTROKE by Microcontroller Division Application Team INTRODUCTION The goal of this application note is to present an example of the use of the HALT mode. In this application, the MCU (here a ST72251) is waked up by an external interrupt caused by someone pressed a key on the 4x4 matrixed keypad. 1 ST7 / KEYBOARD INTERFACE Rows are connected to inputs with pull-up and interrupts (Port C). Columns are connected to Port A configured as output. The result of the interrupt (the value of the pressed key) is sent on LEDS (Port B) and stored into the X register. In our configuration, we have to add 4 pull-up resistors on Port A (from PA0 to PA3) to be able to apply a high level on the corresponding pads. Figure 1. ST7 / keypad interface set-up 4x100k 8x560 VDD PC0 PC1 PC2 PC3 ST7 PA0 PA1 PA2 GND PA3 AN980/1098 1/10 1 ST72251 CONFIGURATION 2 ST72251 CONFIGURATION The application has been validated with a ST72251. Its configuration is described in this part. Refer to your datasheet for more details. 2.1 I/O CONTROL Rows are connected to pins configured as inputs (Port C as input with pull up and interrupts). Columns are connected to pins configured as outputs (Port A). External interrupts are caused by a low level applied to a pin of Port C (caused by a key pressed), they wake up the MCU which was in HALT mode. Port B is configured as outputs to send the value of the pressed key on LEDS. Please, refer to the Data Book to configure pins properly. 2.2 MISCELLANEOUS REGISTER Bits 7 and 6 have to be set to configure events correctly: the external interrupt (EI1) has here to be caused by a falling edge only. Please, refer to the datasheet for more details. 2.3 HALT MODE The HALT instruction places the ST72251 in its lowest power consumption mode. The core and all peripherals are frozen. In this mode, the internal oscillator is turned off, causing all internal processing to be halted. The data remain unchanged. During the HALT mode, external interrupts are still enabled. The MCU stays in this state until an external interrupt or a reset occurs. Then the internal oscillator is restarted and the core waits for 4096 CPU clock cycles (512 µs for a fCPU = 8MHz) before running the external interrupt subroutine. Then the MCU comes back to the main program (in our application to the HALT state). Please, refer to the datasheet for more details. 3 EXTERNAL INTERRUPTS The MCU is in HALT mode. When a key is pressed, a low level is applied to the pin corresponding to the row the key belongs (pins configured as inputs with pull-up). It’s a falling edge applied to a pin of Port C which creates an external interrupt (EI1) and wakes up the MCU. The MCU executes then the external interrupt subroutine (decoding the pressed key) and comes back to its previous state (HALT state in the main program). 2/10 KEYPAD 4 KEYPAD The keypad used is a 4x4 matrixed keypad. Rows are connected to pins configured as inputs with pull-up. So the initial state of these pins are a high level (1). When a key is pressed, a low level is applied to the corresponding pin. For this reason, the keypad is coded as follows: Table 1. Key values row column KEY row column value value KEY value value 1 0x0E 0x0E 7 0x0B 0x0E 2 0x0E 0x0D 8 0x0B 0x0D 3 0x0E 0x0B 9 0x0B 0x0B F 0x0E 0x07 D 0x0B 0x07 4 0x0D 0x0E A 0x07 0x0E 5 0x0D 0x0D 0 0x07 0x0D 6 0x0D 0x0B B 0x07 0x0B E 0x0D 0x07 C 0x07 0x07 1 2 3 F PC0 4 5 6 E PC1 7 8 9 D PC2 A 0 B C PC3 PA1 PA2 PA3 PA0 You have to press the chosen key at least 0.5 to 1 second depending on which key you choose (table read from keypad_top to keypad). The faster the key is read into the table, the faster it will be decoded and the faster the result will be sent on LEDS. 3/10 KEYPAD The keypad code is in the file constant.asm as follows: .keypad keypad_top 4/10 DC.B $0E,$0E,$1 ;PC0PA0 DC.B $0E,$0D,$2 ;PC0PA1 DC.B $0E,$0B,$3 ;PC0PA2 DC.B $0E,$07,$F ;PC0PA3 DC.B $0D,$0E,$4 ;PC1PA0 DC.B $0D,$0D,$5 ;PC1PA1 DC.B $0D,$0B,$6 ;PC1PA2 DC.B $0D,$07,$E ;PC1PA3 DC.B $0B,$0E,$7 ;PC2PA0 DC.B $0B,$0D,$8 ;PC2PA1 DC.B $0B,$0B,$9 ;PC2PA2 DC.B $0B,$07,$D ;PC2PA3 DC.B $07,$0E,$A ;PC3PA0 DC.B $07,$0D,$0 ;PC3PA1 DC.B $07,$0B,$B ;PC3PA2 DC.B $07,$07,$C ;PC3PA3 FLOWCHARTS 5 FLOWCHARTS Figure 2. Flowchart: Main program Initializations infinite loop HALT Figure 3. Flowchart: external interrupt (EI1) X = 48 (3x16 coded values) one column activated (PA0...PA3) Is there a low level on the intersection of the stored row and column? yes no store into X next column value corresponding value of the pressed key stored into A, X and sent on LEDS EXIT 5/10 SOFTWARE 6 SOFTWARE The assembly code given below is guidance only. The complete software with all the files can be found in the software library. st7/ ; the first line is reserved ; for specifying the instruction set ; of the target processor ;************************************************************************ ; TITLE: WAKE.ASM ; AUTHOR: PPG Microcontroller Applications Team ; DESCRIPTION: Main program (use of the Halt mode for decoding ; a keypad). ; ;************************************************************************ TITLE "WAKE.ASM" ; this title will appear on each ; page of the listing file MOTOROLA ; this directive forces the Motorola ; format for the assembly (default) #INCLUDE “st72251.inc”; include st72251 registers and memory mapping file #INCLUDE “constant.inc” ; include general constants file ;*********************************************************************** ; Variables, constants defined and referenced locally ; You can define your own values for a local reference here ;*********************************************************************** ;************************************************************************ ; Public routines (defined here) ;************************************************************************ WORDS segment 'rom' .Init LD A,#$80 ; interrupts are caused by falling edge (on Port C). LD MISCR,A LD LD LD A,#$0F PBDDR,A PBOR,A LD PADDR,A CLR PAOR CLR PADR LD A,#$0F 6/10 ; PB0 to PB3 configures as outputs(LEDS). ; PA0 to PA3 configured as outputs. SOFTWARE LD CLR CLR PCOR,A PCDDR PCDR ; PCO to PC3 configured as input with pull-up and interrupt. RET .delay_45 LD A,#200 loop2 LD X,#$FF loop1 DEC X JRNE loop1 DEC A JRNE loop2 RET ;********************************************************** ; Program code ;********************************************************** .main CALL Init loop HALT JRA loop ; MCU put in lowest power mode. ; Infinite loop, wait an interrupt occurs. ; ***************************************************************** ; This set of instructions uses simple assembly mnemoniques. ; We can notice that the loop label is defined only locally (no dot ; in front of it) so it can not be seen by others modules linked ; with this file. ; ***************************************************************** ; ******************************************** ; * * ; * INTERRUPT SUB-ROUTINES LIBRARY SECTION * ; * * ; ******************************************** .dummy iret .sw_rt iret ; Empty subroutine. Go back to main (iret instruction) .ext0_rt iret .ext1_rt LD X,#48 ; Size of the table stored in X. ; 3 x 16 coded values. begin 7/10 SOFTWARE LD OR AND LD PUSH CALL POP LD AND CP JREQ DEC DEC DEC JRPL A,PADR A,#$0F A,({keypad+1},X) PADR,A X delay_45 X A,PCDR A,#$0F A,(keypad,X) OK X X X begin JRA exit LD LD LD A,({keypad+2},X) X,A PBDR,X ; Outputs forced to 1 (initial value). ; one column of the tested row is activated. ; wait 45ms (debouncing procedure). ; value of Port C stored into A. ; search of the low level column after column. ; if key found -> OK. ; 3 times to have next value of the column. ; do it again to read the whole table(matrix). OK ; Store the key value in A. ; Copy it into X. ; Output the result on LEDS. exit CLR CLR PADR PCDR iret .spi_rt iret .tima_rt iret .timb_rt iret .i2c_rt iret ; ; ; ; ; ; ; ; segment 'vectit' ******************************************************************* This last segment should always be there in your own programs. It defines the interrupt vector adresses and the interrupt routines' labels considering the microcontroller you are using. Refer to the MCU's datasheet to see the number of interrupt vector used and their addresses. Remind that this example is made for a ST72251 based application. ******************************************************************* 8/10 SOFTWARE ; ******************************************************************* ; Each interrupt vector uses two addresses in rom, that's what the directive ; DC.W means. It says "reserve a word location (.W) in rom (DC) and code ; the routine's label in those two addresses. ; Yet, when an interrupt occurs, for example from the timerB, timerb's routine ; address (timb_rt) will be loaded in the PC and the program will jump to this ; label if allowed. It will execute this routine and then will go back to the main ; program (see interrupt chapter in the datasheet for a more precise description ; of how to handle interrupts in ST72 micros). ; ******************************************************************* DC.W dummy ;FFE0-FFE1h location DC.W dummy ;FFE2-FFE3h location .i2c_it DC.W i2c_rt ;FFE4-FFE5h location DC.W dummy ;FFE6-FFE7h location DC.W dummy ;FFE8-FFE9h location DC.W dummy ;FFEA-FFEBh location DC.W dummy ;FFEC-FFEDh location .timb_it DC.W timb_rt ;FFEE-FFEFh location DC.W dummy ;FFF0-FFF1h location .tima_it DC.W tima_rt ;FFF2-FFF3h location .spi_it DC.W spi_rt ;FFF4-FFF5h location DC.W dummy ;FFF6-FFF7h location .ext1_it DC.W ext1_rt ;FFF8-FFF9h location .ext0_it DC.W ext0_rt ;FFFA-FFFBh location .softit DC.W sw_rt ;FFFC-FFFDh location .reset DC.W main ;FFFE-FFFFh location ; This last line refers to the first line. ; It used by the compiler/linker to determine code zone END ; Be aware of the fact that the END directive should not ; stand on the left of the page like the labels's names. 9/10 SOFTWARE THE PRESENT NOTE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH A NOTE AND/OR THE USE MADE BY CUSTOMERS OF THE INFORMATION CONTAINED HEREIN IN CONNEXION WITH THEIR PRODUCTS. Information furnished is believed to be accurate and reliable. However, STMicroelectronics assumes no responsibility for the consequences of use of such information nor for any infringement of patents or other rights of third parties which may result from its use. No license is granted by implication or otherwise under any patent or patent rights of STMicroelectronics. Specifications mentioned in this publication are subject to change without notice. This publication supersedes and replaces all information previously supplied. STMicroelectronics products are not authorized for use as critical components in life support devices or systems without the express written approval of STMicroelectronics. The ST logo is a registered trademark of STMicroelectronics 1998 STMicroelectronics - All Rights Reserved. Purchase of I2C Components by STMicroelectronics conveys a license under the Philips I2C Patent. Rights to use these components in an I2C system is granted provided that the system conforms to the I2C Standard Specification as defined by Philips. STMicroelectronics Group of Companies Australia - Brazil - Canada - China - France - Germany - Italy - Japan - Korea - Malaysia - Malta - Mexico - Morocco - The Netherlands Singapore - Spain - Sweden - Switzerland - Taiwan - Thailand - United Kingdom - U.S.A. http://www.st.com 10/10 This datasheet has been download from: www.datasheetcatalog.com Datasheets for electronics components.