AN979
APPLICATION NOTE
By Microcontroller Division Applications
The goal of this application note is to present a standard example of the use of the Analog to Digital Converter (ADC) of the ST7.
In this note, the ADC is used to convert a 16-key analog keyboard. The technique for the keyboard is to connect the keys by resistive dividers to one of the converter inputs. The Application note is based on ST72F324 which has an embedded 10-bit ADC.
Connect the analog keyboard to one of the analog inputs of the ST7 and connect the high and low voltage references as shown in Figure 1.
Figure 1. ST7 / keyboard interface set-up
KEYBOARD
ST7
VDD high voltage reference
VSS |
|
|
|
low voltage reference |
|
|
|||
|
|
|
ADC analog input
AINx keyboard output
The application has been tested with a ST72F324 configured as described below. Refer to the datasheet for more details on configuring the ST72F324.
Depending on the number of pins available on the device package, the ST7 theoretically allows up to 16 multiplexed analog inputs to the ADC, but in practice, on the ST72F324 device, there can be up to 12 analog inputs. They are alternate functions I/O Port D (PD0-PD5), Port
AN979/0303 |
1/14 |
DRIVING AN ANALOG KEYBOARD WITH THE ST7 ADC
F (PF0, PF4), and Port C (PC0, PC1, PC5, PC7). The I/O port pins used by the ADC must be configured as floating inputs to avoid conflicts in alternate function mode.
Refer to the Data Sheet for information on configuring the I/O ports.
The ST7 ADC is a 10-bit successive approximation converter, with internal sample and holdcircuitry.
You control the ADC using the ADC Control Status Register (ADCCSR).
EOC SPEED ADON 0 CH3 CH2 CH1 CH0
The EOC bit is the end of conversion bit:
- When this bit is set, the conversion is done and result can be read from the ADC Data Registers (ADCDRL & ADCDRH).
- When the bit is reset, the conversion is not complete.
The SPEED bit:
-When this bit is set, fADC = fCPU / 2.
-When this bit is reset, fADC = fCPU / 4. The ADON bit:
-Enable ADC and start conversion when this bit is set.
-Disable ADC and stop conversion when this bit is reset. CH3-CH0 bits:
-They are used to select which analog input to convert. In the ST72324 there are 12 analog pins.
The conversion time is 15 ADC cycles including a sampling time of 4 ADC cycles. The ADC is linear and the digital result of the conversion is given by the formula:
1023*Input Voltage
Digital Result =
Reference Voltage
First the analog input pins must be configured as floating inputs (see Section 3.1).
2/14
DRIVING AN ANALOG KEYBOARD WITH THE ST7 ADC
Then the analog channel to convert must be selected using CH3-CH0 bits of ADCCSR regsister.
Then select the SPEED of conversion.
Setting the ADON bit will switch the converter on.
Figure 2. Flowchart: initialization of the ADC
KBD_init
I/O Initialization
Channel Selection
SPEED Selection
ADC ON
return
Once a conversion is done, the EOC bit is set by hardware. It will be reset when the ADCDRH register is read.
Once enabled, conversions will run continuously until the peripheral is disabled.
3/14
DRIVING AN ANALOG KEYBOARD WITH THE ST7 ADC
Figure 3. Flowchart: conversion process
Conversion Process
KBD_init
no
EOC = 1 ?
yes
read ADCDRL
read ADCDRH
The purpose is to recognize a key when pressed. In an analog keyboard each key is associated with a voltage. The description of an analog keyboard is given by Figure 4.
Figure 4. Hardware description of a keyboard with 16 keys
|
|
VDD |
||||||||||
Vkey |
Rup |
|
|
|
|
|
|
|
|
key 0 |
||
|
|
|
|
|
|
|
|
|||||
R0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
(Keyboard Value) |
|
|
|
|
|
|
|
|
key 1 |
|||
|
R1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with Σ Rj >> Rup |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
R13 |
|
|
|
|
|
|
|
|
key 14 |
||
|
|
|
|
|
|
|
|
|
||||
|
R14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
key 15 |
|||
|
|
|
|
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
VSS
Rup is a pull-up resistor. So, when no key is pressed, Vkey is equal to VDD.
4/14
DRIVING AN ANALOG KEYBOARD WITH THE ST7 ADC
When ‘key i’ is pressed (where i is in the range 1 to 15), the resistor Ri-1 is connected to Vss. Then we have a resistive divider
and Vkey is given by the formula:
i - 1
(VDD - VSS) Σ Rj
Vkey i =
j = 0
i - 1
Rup + Σ Rj j = 0
So the corresponding voltage of each key is given by the values of the resistors. An equal distribution of voltage between VDD and VSS is usually recommended.
To recognize a key, the user will measure Vkey and will be able to decide which key was pressed.
Theoretically, with an 10-bit ADC, 1023 keys can be decoded. But potential errors must be taken into account. They can come from the power supply, the key resistivity, the resistor tolerance, the ADC conversion errors.
The resistor tolerance is the main limitation as usually 5% tolerance resistors are used. It is advised to use a 1% tolerance resistor for the pull-up. Changing this resistor greatly improves the keyboard as the pull-up has an influence on every key.
The ADC total unadjusted error (TUE) for the device is specified as 4 LSB. So, it has to be taken into account to avoid any key decision error.
These parameters will reduce the number of keys that can be efficiently decoded.
5/14