• • • • • • • • 16 Key Pushbutton Pad in 4 x 4 Matrix Very Low Power Consumption AVR in Sleep Mode and Wakes Up on Keypress Minimum External Components ESD Protection Included if Necessary Efficient Code Complete Program Included for AT90S1200 Suitable for Any AVR MCU
8-bit Microcontrollers Application Note
This application note describes a simple interface to a 4 x 4 keypad designed for low power battery operation. The AVR spends most of its time in Power-down mode, waking up when a key is pressed to instigate a simple test program that flashes one of two LEDs according to the key pressed. If “0” (zero) is pressed the RED LED flashes 10 times. All other keys flash the GREEN LED the number of times marked on the key (e.g., if “C” is pressed the GREEN LED flashes twelve times). Figure 1-1. Keypad and LED Connections AT90S1200
R1 PB7 R2 PB6 R3 PB5 C1 C2 D1 R1 D2 R2 D3 R3 D4 R4 ALL 1N4148 1 4 7 A C3 2 5 8 0 C4 3 6 9 B F E D C R4 PB4 R5 PB3 R6 PB2 R7 PB1 R8 PB0 ALL 470Ω PD0 ALL 330Ω PD1 R9 PD2 R10 LED2 GREEN LED1 RED VCC
4 x 4 KEYPAD
2 Theory of Operation
The keypad columns are connected to the high nibble of port B. The keypad rows are connected to the low nibble. Resistors R1 to R8 (this is shown in Figure 1-1) serve to limit input current to a safe level in the event of ESD from the keypad. They can be omitted in most applications. In the steady state condition the high nibble is configured as outputs and are in the low state. The low nibble is configured as inputs and has the internal pull-ups enabled, removing the need for external pull-up resistors. After initialization the AVR is put to sleep. When a key is pressed one of the diodes D1 - D4 pull down the external interrupt line PD2, which also has internal pull-ups enabled. This wakes up the AVR and causes it to run the interrupt service routine, which scans the keypad and calculates which key is pressed. It then returns to the main program and drives the LEDs according to the key pressed, putting the AVR back to sleep when it has finished. Resistors R9 and R10 are the traditional current limit resistors for the LEDs and can be any suitable value for the supply rail. This application note was tested using 330Ω on a 5V supply. The LEDs are driven in current sink mode (“0” = ON) and provide about 10 mA of forward current with the values specified.
The firmware consists of three sections, the reset routine, the test program and the interrupt service routine sets up the ports, sleep mode, power saving and the interrupts. The test program flashes the LED on wake-up and the interrupt service routine responds to the keypress.
3.1 Reset Routine
The flowchart for the Reset Routine is shown in Figure 3-1. On reset the ports are initialized with their starting directions. These are fixed on port D, with all bits as outputs except PD2, which must be an input for the external interrupt. This bit has its pull-up enabled by setting bit 2 of Port D. The unused bits are configured as outputs to avoid noise pickup or excessive power consumption, which could otherwise occur if left floating. Port B starts with the high nibble as outputs sending out zeroes, and the low nibble set as inputs with the pull-ups enabled. Since we are using a minimum of external components, we must ensure that internal pull-ups are turned on for all those bits set up as inputs. This is achieved by configuring the Data Direction Register with “1”s for outputs, “0”s for inputs, and then writing “1”s to the input bits in the PORT Register. The inputs can then be read or tested from the PIN register. This program looks for “0”s and uses the SBIS instruction to skip over the keypress action if not a “0”. Power-down mode is selected by setting the SE and SM bits of the MCUCR. At the same time the external interrupt configured by writing “0”s into the ISC00/01 bits. This...