2003apr25 Id MPR An
2003apr25 Id MPR An
2003apr25 Id MPR An
8-bit
Microcontroller
Features
Application
Note
Introduction
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. Keypad and LED Connections
AT90S1200
R1
PB7
R2
PB6
R3
PB5
C1 C2
C3
R4
C4
PB4
D1 R1
R5
D2 R2
R6
D3 R3
R7
D4 R4
R8
ALL
1N4148
4 x 4 KEYPAD
VCC
PB3
ALL
470
PB2
PD2
PB1
PD1
PB0
PD0
R10
R9
ALL
330
LED2
GREEN
LED1
RED
Rev. 1232BAVR05/02
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) 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.
Implementation
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.
Reset Routine
The flowchart for the Reset Routine is shown in Figure 2. 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 1s for outputs, 0s for inputs, and then writing 1s to
the input bits in the PORT Register. The inputs can then be read or tested from the PIN
register. This program looks for 0s 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 0s into the ISC00/01 bits. This
will set the external interrupt INT0 to trigger on a LOW level. When using Power-down
mode the AVR can only be woken up by LOW LEVEL trigger.
Power consumption is reduced further by turning off the Analog Comparator. This is
done by setting the ACD bit in the ACSR Register. This must be done with care, otherwise an unwanted interrupt can be generated. This program disables global interrupts
until the program is ready to be interrupted, solving this problem. If you wish to use the
Analog Comparator this code can be removed, but you will need to change ports for the
keypad since port B is used for this.
The AVR then enters sleep mode. This is placed in the main loop to ensure that it goes
back to sleep after it has finished its interrupt function and carried out the Flash test
routine. When the AVR wakes up after a keypress, the Flash routine is called after the
AVR240
1232BAVR05/02
AVR240
interrupt routine is finished. When the Flash routine is done, the external interrupt is
enabled, so that another interrupt can occur.
Figure 2. Flowchart for Flash Function
Start
Initialize Ports
Set Up
Onterrupts and
Sleep Mode
Disable
Analog
Comparator
Disable Global
Interrupts
Reset Port
Configuration
Enable Global Interrupts
Sleep
Flash
Enable External
Interrupt
3
1232BAVR05/02
for contact bounce. Wake-up from sleep mode typically takes 16 ms or so, although this
is being reduced on the newer devices. This also provides some debouncing.
Figure 3. Flowchart for Reset and Main Routine
Flash
Read
EEPROM at
Key Value
Value
=0?
N
Flash Green
LED Value
Ttimes
Flash Red
LED Ten
Times
Return
AVR240
1232BAVR05/02
AVR240
Long Time Delay
Subroutine (delay)
To see the LEDs flash requires a delay of about 0.25 second. This is achived using a
conventional FOR loop to keep the Timer/Counter free for other work. To achieve 0.25
second with a 4 MHz clock requires three nested loops. Three local variables contained
in registers fine, medium and coarse are used for the loop. The fine and medium
counters run the maximum of 255 times with the coarse Counter set to 8, giving just
over 0.25 second delay. The flowchart is shown in Figure 4.
Figure 4. Flowchart for Delay Subroutine
delay
Coarse=8
Medium=255
Fine=255
Fine = Fine-1
0?
Y
Medium=
Medium-1
0?
Y
Coarse=
Coarse-1
0?
Y
Return
Interrupt Service
Routine
On entry the Status Register is preserved to avoid corrupting any work the main program was doing. In this application it may be left out for optimisation if you wish. The
flowchart is shown in Figure 5.
The key row is first detected by testing each row input in turn looking for 0. A base
number 0, 4, 8, or 12 is then assigned to the variable key. The ports are then reinitialized with Port B I/O swapped over so that the key rows are tested.
A short time delay settle is used to allow the pins time to discharge. This takes the form
of a conventional time waste loop using a FOR loop arrangement.
5
1232BAVR05/02
The key column is then detected and a number assigned in a temporary variable temp
of 0, 1, 2, or 3. The final keypress is then computed by adding key and temp, placing
the result in key ready for use by the Flash function. This method is easier to code
than the conventional single bit scan in this application.
The Port B configuration is the swapped back prior to restoring the Status Register. This
saves using the settling delay again.
At the end, the external interrupt is disabled. This is done to avvoid the interrupt routine
being triggered again immediately upon exit.
This short delay is required when changing the Port B I/O contiguration to allow time for
the pin values to settle. The routine uses the Global Scratch Register temp as a single
loop counter for the FOR loop, set at maximum 255 passes. This provides a delay of
0.129 ms at 4 MHz. This value could be shortened by experimentation if time is of the
essence or the pins are set high prior to reconfiguration to speed things up. This might
remove the need for this delay completely.
Figure 5. Flowchart for Interrupt Service Routine
Scan
Preserve Status
Register
Test Rows
Row
1?
Key=0
Row
2?
Key=4
Row
3?
Key=8
Row
4?
Key=12
Swap Port
I/O Nibbles
Settle
Delay
Test Columns
AVR240
1232BAVR05/02
AVR240
Figure 6. Flowchart for Interrupt Service Routine Continued
A
Col
1?
Key=key+0
Col
2?
Key=key+1
Col
3?
Key=key+2
Col
4?
Key=key+3
Reset
Port B I/O
Restore Status
Register
Disable External
Interrupt
Return From
Interrupt
Resources
Code Size
Cycles
Register Usage
Interrupt
Description
Main
24 words
19 cycles
R16
Initialization
Flash
20 words
R16
Example program
Scan
31 words
47 typical
INTO
Delay
10 words
1,000,000
Settle
4 words
516 cycles
R16
Total
87 words
7
1232BAVR05/02
Description
Interrupts
16 bytes EEPROM
8 I/O pins
4 x 4 keypad connections
2 I/O pins
;**** A P P L I C A T I O N
N O T E
A V R 240 ************************
;*
;* Title:
;* Version:
1.1
;* Last Updated:
98.05.30
;* Target:
;*
;* Support E-mail: avr@atmel.com
;*
;* DESCRIPTION
;* This Application note scans a 4 x 4 keypad and uses sleep mode
;* causing the AVR to wake up on keypress.
The example runs on the AT90S1200 but can be any AVR with
;* suitable changes in vectors, EEPROM and stack pointer. The timing assumes
;* a 4 MHz clock.
;* A look up table is used in EEPROM to enable the same structure to be used
;* with more advanced programs e.g ASCII output to displays.
;***************************************************************************
ROW1
=3;
.equ
ROW2
=2
.equ
ROW3
=1
.equ
ROW4
=0
.equ
COL1
=7
.equ
COL2
=6
.equ
COL3
=5
.equ
COL4
=4
;Port D pins
.equ
GREEN
=0
;green LED
AVR240
1232BAVR05/02
AVR240
.equ
RED
=1
;red LED
.equ
INT
=2
;interrupt input
.include "1200def.inc"
;***** Registers used by interrupt service routine
.def
key
.def
status =r21
=r17
fine
.def
medium =r19
.def
coarse =r20
;EEPROM segment
.org 0
.db
1,2,3,15,4,5,6,14,7,8,9,13,10,0,11,12
;****Source code***************************************************
.cseg
;CODE segment
.org 0
rjmp reset
;Reset handler
rjmp scan
reti
reti
ldi temp,0xFB
out DDRD,temp
ldi temp,0x30
out MCUCR,temp
ldi temp,0x40
out GIMSK,temp
sbi ACSR,ACD
main:
cli
ldi temp,0xF0
out DDRB,temp
; 4 OUT
ldi temp,0x0F
out PORTB,temp
ldi temp,0x07
out PORTD,temp
4 IN
sei
sleep
;fall asleep
rcall flash
9
1232BAVR05/02
rjmp main
sbis PINB,ROW1
ldi key,0
sbis PINB,ROW2
ldi key,4
sbis PINB,ROW3
ldi key,8
sbis PINB,ROW4
ldi key,12
ldi temp,0x0F
out DDRB,temp
ldi temp,0xF0
out PORTB,temp
;write 0s to rows
rcall settle
sbis PINB,COL1
ldi temp,0
sbis PINB,COL2
ldi temp,1
sbis PINB,COL3
ldi temp,2
sbis PINB,COL4
ldi temp,3
add key,temp
ldi temp,0xF0
out DDRB,temp
; 4 OUT
ldi temp,0x0F
4 IN
out PORTB,temp
out SREG,status
ldi temp,0x00
out GIMSK,temp
reti
out EEAR,key
;address EEPROM
sbi EECR,EERE
;strobe EEPROM
in temp,EEDR
tst temp
;is it zero?
breq zero
green_flash:
cbi PORTD,GREEN;flash green LED 'temp' times
rcall delay
10
AVR240
1232BAVR05/02
AVR240
sbi PORTD,GREEN
rcall delay
dec temp
brne green_flash
exit:
ret
zero:
ldi temp,10
flash_again:
cbi PORTD,RED
rcall delay
sbi PORTD,RED
rcall delay
dec temp
brne flash_again
rjmp exit
cagain:
magain:
ldi fine,255
fagain:
dec fine
brne fagain
dec medium
brne magain
dec coarse
brne cagain
ret
dec temp
brne tagain
ret
11
1232BAVR05/02
Atmel Headquarters
Atmel Operations
Corporate Headquarters
Memory
Europe
Atmel Sarl
Route des Arsenaux 41
Case Postale 80
CH-1705 Fribourg
Switzerland
TEL (41) 26-426-5555
FAX (41) 26-426-5500
Asia
Room 1219
Chinachem Golden Plaza
77 Mody Road Tsimhatsui
East Kowloon
Hong Kong
TEL (852) 2721-9778
FAX (852) 2722-1369
Japan
9F, Tonetsu Shinkawa Bldg.
1-24-8 Shinkawa
Chuo-ku, Tokyo 104-0033
Japan
TEL (81) 3-3523-3551
FAX (81) 3-3523-7581
RF/Automotive
Theresienstrasse 2
Postfach 3535
74025 Heilbronn, Germany
TEL (49) 71-31-67-0
FAX (49) 71-31-67-2340
Microcontrollers
2325 Orchard Parkway
San Jose, CA 95131
TEL 1(408) 441-0311
FAX 1(408) 436-4314
La Chantrerie
BP 70602
44306 Nantes Cedex 3, France
TEL (33) 2-40-18-18-18
FAX (33) 2-40-18-19-60
ASIC/ASSP/Smart Cards
Biometrics/Imaging/Hi-Rel MPU/
High Speed Converters/RF Datacom
Avenue de Rochepleine
BP 123
38521 Saint-Egreve Cedex, France
TEL (33) 4-76-58-30-00
FAX (33) 4-76-58-34-80
Zone Industrielle
13106 Rousset Cedex, France
TEL (33) 4-42-53-60-00
FAX (33) 4-42-53-60-01
1150 East Cheyenne Mtn. Blvd.
Colorado Springs, CO 80906
TEL 1(719) 576-3300
FAX 1(719) 540-1759
Scottish Enterprise Technology Park
Maxwell Building
East Kilbride G75 0QR, Scotland
TEL (44) 1355-803-000
FAX (44) 1355-242-743
e-mail
literature@atmel.com
Web Site
http://www.atmel.com
0M
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: