The ST6 on-chip Analog to Digital Converter (ADC) is a useful peripheral integrated into the sili
con of the ST6 family members.The flexibility of the I/O port structure allows the multiplexing of
up to 13/8 Analog Inputs into the converter in a 28/20 pin device for the ST6210/15 2k ROM and
ST6220/25 4k ROM families, enabling full freedom in circuit layout. Many other members of the
ST6 family also offer the Analog to Digital converter.
One of the more novel and practical applications of this converter, is to decode a number ofkeys.
The technique is to connect the keys by resistive voltage dividers to the converter inputs.An example of key detection using 10 keys is illustrated in this note.
Using the Analog to Digital converter in this fashion does not require a static current and avoids
false key detection.
BASIC CIRCUIT
The basic circuit of the key decoder consists of a pull-up resistor connected to the ST6 Analog to
Digital converter input with the first key directly switching to ground. The following keys are then
connected in sequenceto the ADCinput through serial resistors.The number of keys which may
be detected depends on the tolerance of the resistors used. It can be seen that if more than one
key is pressed at the same time, the key detected will be the next key in the chain closest to the
ADC input. This also allows the keys in the keyboard to be prioritized.
-
June 2008
Rev 21/15
ANALOG KEYBOARD
PRINCIPLE OF OPERATION
The combination of the pull-up resistor, the serial resistors and the pressed key form a resistive
voltage divider, generating a different voltage at the ADC input for each key pressed. The serial
resistors are selected in order to give an equal distribution of voltage between V
and VSSfor
DD
each switch combination to give the best noise margin between keys.
When a key is pressed, the voltage at the ADC input is given by the activated voltage divider.
This analog voltage is converted by the ADC and the digital value is used to determine which
switch is closed. Two successive conversions may be madeto avoid the influence of key bounce.
If the top key is pressed, the voltage measured is always zero.For n keys, the resistor values should
be selected such that the voltage for the second key from top is V
/n, for the 3rd - 2xVDD/n, for the
DD
Figure 1. Analog Keyboard resistor key matrix
Figure 2. Multiple key press
2/15
Table 1. Key code ranges
Key Nr
10E5-E625
Valid Code
Range
1024
218-1A22
330-3322
4
563-6820
67C-8122
797-9B21
8B0-B422
9CA-CD24
49-4E21
Distance to
next key
Table 2. Used resistors and Tolerance
ANALOG KEYBOARD
4th - 3xV
/n and for the nth - (n-1)xVDD/n. Re
DD
sistor values from the tolerance set used
must be selected to meet this requirement.
The recommended resistor values for a
10-key keyboard with 2% resistors from the
E24 series, used with a 10kΩ pull-up resistor,
are shown intable 2. If more current can be al
lowed, then a 1kΩ resistor can be used in
which case the serial resistor values should
be divided by 10.
-
-
Resistor
Rp100009800
R111001078
R213001274
R318001764
R424002352
R533003234
R651004998
R782008036
R81600015680
Active KeyR Error Range (LSB)Distance to next Key
S0024
Value ( )-2% ( )
3/15
ANALOG KEYBOARD
PRACTICAL LIMITATIONS
Theoretically, for an ideal power supply, ADC and resistors, 255 keys could be detected. Practi
cally however, it is necessary to take into account potential errors coming from:
- the power supply - the key resistivity - the resistor tolerance - the ADC error
The power supply tolerance can normally be neglected providing noise is not present at a fre
quency within or above the frequency range of the RC delay of the resistive divider, as the ADC
reference is normally provided by the powersupply of the ST6. For ST6 family members with ex
ternal ADC reference voltage inputs, AV
and AVSSmay be used instead of VDDand VSS.
DD
The sensitivity of the key can normally be neglected, as the resistance of the divider is high in
comparison to it. If the key resistivity is significant, it should be added to the “serial” pull-down re
sistance of the different dividers. The key resistivity variation must also be added to the toler
ance of the serial pull-down resistor (see resistor tolerance following).
The resistor tolerance affects the tolerance of the dividers. Two situations must be taken into ac
count:
a) minimum value of pull-up combined withmaximum values of pull-down = maximum voltage of
the divider at the ADC input.
b) maximum value of the pull-up combined with the minimum values of pull-down = minimum
voltage at the ADC input. These two cases give the maximum voltage variation of each divider
(see Table 3). The voltage variation ranges of two dividers must not overlap otherwise the key
cannot be decoded, even with an ideal converter.
Table 3. Effective Divider Resistors RX
Active
Key
S0
S1
S2
S3
S4
S5
S6
S7
S8
S9
R -2% ( )R +2% ( )
00
10781122
23522448
41164284
64686732
970210098
1470015300
2273623664
3841639984
8839692004
Realistic converters require a margin between the range of variation. In the case of a
significant variation in the key resistivity, the
maximum resistivity of the key has to be
added to the value of the pull-down resistor in
case a). For case b) no error needs to be
added as the resistivity cannot be less than 0
Ω.
-
-
-
-
-
-
4/15
ANALOG KEYBOARD
The linearity of the ADC converter of the ST6 is normally specified for ⎛2 LSB, therefore a minimum
distance of 4 LSB is needed between the edges of the resistance tolerance ranges.For the best re
sults, a minimum of 8 LSB should be used (see Table 4).
Table 4. Voltage at the ADC-Input,Converter Results (5V supply)
V (Rxmin-Rpmax)V (Rxmax-Rpmin)
Active Key
Vhex.dec.Vhex.dec.
S00.000000.00000
S10.4818240.511A26
S20.9430481.003351
S31.4449731.524E78
S41.9463992.0468104
S52.447C1242.5481129
S62.95971513.059B155
S73.45B01763.54B4180
-
S83.95C92014.02
Table 5. AD-Converter Results
Active Key
S00240-0
S122218-1A
S232230-33
S342149-4E
S452063-68
S55227C-81
S652197-9B
S7422B0-B4
S8324C9-CD
S9225E5-E6
R Error Range
(LSB)
Distance to next
Key
CD
Valid Key Range
205
5/15
ANALOG KEYBOARD
EXTENSION FOR WAKE UP
ST6 family members with the Analog input
capacity can also generate a wake-up opera
tion (from WAIT or STOP modes) on the
pressing of a key. This can be achieved by a
modification of the circuit shown in figure 1.
The pull-up resistor is not connected to V
DD
but to an additional I/O port bit. During key
polling, this additional port bit is set to output
mode active high, thus effectively switching
to the pull-up resistor. The resistance of
V
DD
the pull-up resistor must be high enough to
give no significant voltage drop, or the result
ing error must be calculated and taken into
account. The other I/O bit is used as the Ana
log input tothe ADC as in theoriginal circuit.
During the wait for the key press, the first I/O
pin, used to pull the pull-up resistor high to
while polling, is switched into a high im-
V
DD
pedance state (e.g. open drain output mode).
The second I/O pin, used as the ADC input
while polling, is switched to the interrupt input
with pull-up mode. The internal pull-up is in
the range of 100k, in comparison to the 1k 10k of the external resistor used during polling. If any key is now pressed an interrupt will
be generated if the voltage at the second I/O
pin is below the Schmitt trigger low level
threshold.The serial resistors in the keyboard
chain must not be too high in this case, therefore the maximum number of keys is reduced
in comparison to the normal mode.
*
;**
;*Use of ADC inputs for multiple key decoding*
;**
*
;*With the inbuilt A/D converter of any ST6 it is easy to*
;*implement a small routine which enables ONE port pin, con-*
;*figured as an ADC input, to decode up to ten different switches*
;*All that is necessary is to set one port pin as an ADC input*
;*Then the program runs in an endless loop until one of the*
*
;*connected keys is pushed.*
*
;*The value from the ADC data register is then used to decide*
;*how the program will continue,on reaction to the key-push.*
ddrpb.def0c5h ;port B data direction register
orpb.def0cdh ;port B option register
drpb.def0c1h ;port B data register
adr.def0d0h ;A/D data register
adcr.def0d1h ;A/D control register
a.def0ffh ;accumulator
;***CONSTANTS***
inpall.equ000h;used for setting all pins input
peg1_2.equ00ch;border to distinguish between switch1 and switch2
peg2_3.equ025h;border to distinguish between switch2 and switch3
peg3_4.equ03eh;border to distinguish between switch3 and switch4
peg4_5.equ058h;border to distinguish between switch4 and switch5
peg5_6.equ072h;border to distinguish between switch5 and switch6
peg6_7.equ08ch;border to distinguish between switch6 and switch7
peg7_8.equ0a5h;border to distinguish between switch7 and switch8
peg8_9.equ0beh;border to distinguish between switch8 and switch9
7/15
ANALOG KEYBOARD
peg9_10.equ0d9h;border to distinguish between switch9 and switch10
ldiddrpb,inpall;sets all port B pins low — all input
ldiorpb,01h;option register:
;sets bit b0 high, the rest low
ldidrpb,01h;direction register:
;sets bit b0 high, the rest low
;— pb0 becomes analog input
;pb1-7 become input with pull-up, but
;are not used here (only one pin may be
;analog input for A/D at the same time)
;*SGS-THOMSON GRAFING*
;**
;*APPLICATION NOTE 431 -ST6*
;**
;*Use of ADC inputs for multiple key decoding*
;**
;*With the inbuilt A/D converter of any ST6 it is easy to*
;*implement a small routine with which you can recognize*
;*if one of nine connected keys is pushed by creating an*
;*interrupt. The program can then decide how it will react*
;*to the key pushed.*
;**
;**
ddrpb.def0c5h ;port B data direction register
orpb.def0cdh ;port B option register
drpb.def0c1h ;port B data register
ior.def0c8h ;interrupt option register
adr.def0d0h ;A/D data register
adcr.def0d1h ;A/D control register
a.def0ffh ;accumulator
;***CONSTANTS***
inpall.equ000h;used for setting all pins input
peg1_2.equ00ch;border to distinguish between switch1 and switch2
peg2_3.equ025h;border to distinguish between switch2 and switch3
peg3_4.equ03eh;border to distinguish between switch3 and switch4
peg4_5.equ058h;border to distinguish between switch4 and switch5
peg5_6.equ072h;border to distinguish between switch5 and switch6
peg6_7.equ08ch;border to distinguish between switch6 and switch7
peg7_8.equ0a5h;border to distinguish between switch7 and switch8
peg8_9.equ0beh;border to distinguish between switch8 and switch9
; en_kint (enable key-interrupt) sets the registers in a way that pushing
; any key will cause an interrupt. This subroutine must be called to
; re-enable the key interrupt (e.g. after handling the key service routine)
10/15
ANALOG KEYBOARD
en_kint:
ldiddrpb,inpall;sets all port B pins low — all input
ldiorpb,02h;option register:
; sets bit b1 high, the rest low
ldidrpb,01h;data register:
; sets bit b0 high, the rest low
;— pb0 becomes input, no pull-up, no int
;pb1 becomes input with pull-up and int.
;pb2-7 become input with pull-up, but
;are not used here
ldiior,10h;interrupt option register:
;— set D4: enable all interrupts
;reset D5: falling edge on int.input(#2)
ret;return to the calling address
;*** hd_kint (handle key interrupt) interrupt service routine
;*** evaluates the data resulting in pushing a key.
;*** Interrupt vector #2 (0ff4h and 0ff5h) must point (jump) to hd_kint.
hd_kint:ldidrpb,03h;data register:
; 0000 0011
ldiddrpb,01h;data direction register:
; 0000 0001
; — pb0 becomes output
ldiorpb,03h;option register:
; 0000 0011
; — pb0: push-pull output
; — pb1: ADC-input
;pb2-7 become input with pull-up, but
;are not used here
loop:jrr6,adcr,loop;waits until the End Of Conversion
; bit is set (indicator that a conversion
; has been completed)
lda,adr;load acc with the result of the A/D
; conversion
;now the result is compared with the
; values which represent the different
; switches
11/15
ANALOG KEYBOARD
sw1:cpia,peg1_2;compare with peg1_2
jrnzsw2;A/D result was smaller than peg1_2
jps1; — switch1 was pressed: jump to s1
sw2:cpia,peg2_3;compare with peg2_3
jrnzsw3;A/D result was smaller than peg2_3
jps2; — switch2 was pressed: jump to s2
sw3:cpia,peg3_4;compare with peg3_4
jrnzsw4;A/D result was smaller than peg3_4
jps3; — switch3 was pressed: jump to s3
sw4:cpia,peg4_5;compare with peg4_5
jrnzsw5;A/D result was smaller than peg4_5
jps4; — switch4 was pressed: jump to s4
sw5:cpia,peg5_6;compare with peg5_6
jrnzsw6;A/D result was smaller than peg5_6
jps5; — switch5 was pressed: jump to s5
sw6:cpia,peg6_7;compare with peg6_7
jrnzsw7;A/D result was smaller than peg6_7
jps6; — switch6 was pressed: jump to s6
sw7:cpia,peg7_8;compare with peg7_8
jrnzsw8;A/D result was smaller than peg7_8
jps7; — switch7 was pressed: jump to s7
sw8:cpia,peg8_9;compare with peg8_9
jrnzsw9;A/D result was smaller than peg8_9
jps8; — switch8 was pressed: jump to s8
sw9:jps9;A/D result was bigger than peg8_9
; — switch9 was pressed: jump to s9
;
;*** The routines handling the reaction to the individual key presses
;*** are to be included here
12/15
s1:
s2:
s3:
s4:
s5:
s6:
s7:
s8:
s9:
;*** Each routine must end with the following lines in order to enable
;*** another interrupt when the next key is pressed.
call en_kint ; enable another interrupt
return:reti
ANALOG KEYBOARD
13/15
ANALOG KEYBOARD
Table 6. Revision history
DateRevisionDescription of changes
September 19921Initial release
19-June-20082Logo modified
14/15
ANALOG KEYBOARD
Please Read Carefully:
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its
subsidiaries (“ST”) reserve the right to make changes, corrections, modifications or improvements, to this document,
Purchasers are solely responsible for the choice, selection and use of the ST products and services described
herein, and ST assumes no liability whatsoever relating to the choice, selection or use of the ST products and
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this
document. If any part of this document refers to any third party products or services it shall not be deemed a license
grant by ST for the use of such third party products or services, or any intellectual property contained therein or
considered as a warranty covering the use in any manner whatsoever of such third party products or services or any
UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY
EXPRESS OR IMPLIED WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS
INCLUDING WITHOUT LIMITATION IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS OF ANY JURISDICTION), OR
INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZED ST REPRESENTATIVE, ST PRODUCTS
ARE NOT RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE,
LIFE SAVING, OR LIFE SUSTAINING APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE
OR MALFUNCTION MAY RESULT IN PERSONAL INJURY, DEATH, OR SEVERE PROPERTY OR
ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS “AUTOMOTIVE GRADE” MAY
and the products and services described herein at any time, without notice.
All ST products are sold pursuant to ST’s terms and conditions of sale.
services described herein.
intellectual property contained therein.
ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK.
Resale of ST products with provisions different from the statements and/or technical features set forth in this
document shall immediately void any warranty granted by ST for the ST product or service described herein and shall
ST and the ST logo are trademarks or registered trademarks of ST in various countries.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective
not create or extend in any manner whatsoever, any liability of ST.