AN1799
APPLICATION NOTE
STR71x GPIO
SCANNING A 4x4 MATRIX KEYPAD
INTRODUCTION
The aim of this application note is to show how to scan the 4x4 matrix keypad multiplexed with
a four 7-segment display. The software attached to this application note scans the pressed
key and displays it on the multiplexed 7- segment LEDs.
This application note describes how the interrupt capability of the STR710 device is used to
offer a better key scan.
Rev. 2
AN1799/0205
1/7
1
STR71x GPIO SCANNING A 4x4 MATRIX KEYPAD
1 HARDWARE LAYOUT
A 4x4 keypad can be very easily interfaced to the STR710’s PORT1 & PORT2 (Figure
1).
Eight lines (P1.0 - 7) are assigned to LED segments. Four lines (P1.8 - 11) are used to drive
and select the 7-segment displays through sink transistors. The same lines are used for
pressed key checking. Keypad rows are connected with pull-down resistors to the four exter-
nal interrupts pins (P2.8 - 11).
Figure 1. Multiplexing matrixed keypad with four 7-segment displays
4 x 7-segment displays
P1.0
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
P1.8
P1.9
P1.10
P1.11
a
b
c
d
e
f
g
DP
LED Module
0.33K
1.2K
1.2K
1.2K
1.2K
0.33K
0.33K
0.33K
NPN
BC 547C
NPN
BC 547C
NPN
BC 547C
STR710
NPN
BC 547C
0.12K
P2.8
1
P2.9
P2.10
P2.11
2
5
8
0
4x4 KEYPAD
3
6
9
F
A
B
C
D
0.12K
0.12K
0.12K
4
7
E
2/7
2
STR71x GPIO SCANNING A 4x4 MATRIX KEYPAD
2 STR710 CONFIGURATION
This part is dedicated to show the STR710 Microcontroller configuration. The STR710 is used
since it offers the possibility to use external interrupts.
2.1 GPIO PORT CONFIGURATION
Rows are connected to P2.8-11 pins configured as Input Pull Up /Pull Down Weak Push-Pull
mode. For the columns, the sink transistors base pins are connected to P1.8-11 pins config-
ured as Output Push-Pull. External interrupts are triggered by a high level applied to a pin of
P2.8-11 (caused by a pressed key), they generate an interrupt on the external interrupt (XTI)
channel and can wakeup the system from STOP mode.
P1.0-7 configured as Output Push-Pull to send the value of the pressed key to the desired 7
segment display.
2.2 XTI CONFIGURATION
The External Interrupts Unit (XTI) manages 14 external interrupt lines.
In this aplication the XTI is configured to generate interrupts when a rising edge is detected on
line 2-5; any transition from low to high level on the P2.8-11 pins will trigger an external inter-
rupt.
3/7
STR71x GPIO SCANNING A 4x4 MATRIX KEYPAD
3 SOFTWARE
3.1 KEYPAD
The keypad used is a 4x4 matrixed keypad. Rows are connected to the P2.8-11 pins config-
ured as Input Pull Up /Pull Down Weak Push-Pull mode and pulled low through pull-down re-
sistors. Columns are connected to the P1.8-11 pins configured as Output Push-Pull.
Biasing is achieved by setting high (3.3 V) each row for 5 ms duration every 20 ms. This gives
an update rate of 50 Hz. The 5 ms time-base can be generated using a timer overflow inter-
rupt.
When a key is pressed, a rising edge is applied to the row the key belong. The MCU executes
the XTI IRQ handler routine and decodes the pressed key (Table
1).
The keypad is coded as follows:
Table 1. Key codes
Key
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
Row value
0x0800
0x0100
0x0100
0x0100
0x0200
0x0200
0x0200
0x0400
0x0400
0x0400
0x0100
0x0200
0x0400
0x0800
0x0800
0x0800
Column value
0x0400
0x0800
0x0400
0x0200
0x0800
0x0400
0x0200
0x0800
0x0400
0x0200
0x0100
0x0100
0x0100
0x0100
0x0800
0x0200
Key Code
0x84
0x18
0x14
0x12
0x28
0x24
0x22
0x48
0x44
0x42
0x11
0x21
0x41
0x81
0x88
0x82
4/7
STR71x GPIO SCANNING A 4x4 MATRIX KEYPAD
3.2 DISPLAYS
Depending on which display is selected, and using a Hexadecimal to 7-segment display cor-
respondance table, the corresponding key code is extracted, then decoded to a 7-segment
display and finally sent to the 7-segment LED display.
3.3 FLOWCHARTS
main
Configure XTI
Configure GPIO ports
Configure Timer 0
Configure EIC
Displayer = 0
KeyPressed = True
No
Yes
KeyPressed = False
Display Pressed Key
Displayer = Displayer +1
No
Displayer = 4
Yes
5/7