AN2658
Application note
Using the analog to digital converter of the STM8S microcontroller
Introduction
The purpose of this application note is to explain how to use the Analog to Digital Converter implemented in many of the STM8S microcontroller family devices. It provides useful information on how to configure the ADC registers and microcontroller resources and use the ADC in different modes.
The STM8 firmware library, containing source code of all the examples described in this application note, can be downloaded from the STMicroelectronics website: www.st.com.
July 2009 |
Doc ID 14152 Rev 2 |
1/23 |
www.st.com
Contents |
AN2658 |
|
|
Contents
1 |
ADC hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
4 |
|
|
1.1 |
General properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
4 |
|
1.2 |
ADC operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
6 |
2 |
Summary of features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
8 |
2.1 Resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.2 Clock selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.3 Conversion triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.4 ADC speed/sampling time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.5 Voltage reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.6 Input analog channel selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.7 Data storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.8 Extended functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.8.1 Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.8.2 Buffer overrun flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.8.3 Analog watchdog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.8.4 Scan mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.8.5 Bandgap reference channel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3 |
Configuring ADC registers for conversion . . . . . . . . . . . . . . . . . . . . . . |
12 |
||
|
3.1 |
Selecting the operating mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
12 |
|
|
|
3.1.1 |
Single conversion mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
12 |
|
|
3.1.2 |
Continuous conversion mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
12 |
|
|
3.1.3 |
Conversion on external trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
12 |
|
|
3.1.4 |
Scan conversion mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
12 |
3.2 Conversion speed selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.3 Analog input selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.4 Storing converted values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.5 Analog watchdog function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 3.6 ADC interrupt management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4 |
Practical application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
15 |
|
|
4.1 |
Areas of use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
15 |
2/23 |
Doc ID 14152 Rev 2 |
AN2658 |
Contents |
|
|
4.2 Hardware connection examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 4.3 Methods for precision improvement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5 |
Design recommendations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
19 |
|
6 |
Displaying the ADC conversion result using LEDs . . . . . . . . . . . . . . . |
20 |
|
|
6.1 |
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
20 |
|
6.2 |
Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
20 |
|
6.3 |
Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
20 |
7 |
ADC conversion triggered by TIM1 timer . . . . . . . . . . . . . . . . . . . . . . . |
21 |
|
|
7.1 |
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
21 |
|
7.2 |
Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
21 |
|
7.3 |
Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
21 |
8 |
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
22 |
Doc ID 14152 Rev 2 |
3/23 |
ADC hardware description |
AN2658 |
|
|
STM8 family microcontrollers include one Analog to Digital Converter which has up to 16 multiplexed inputs. Depending on the STM8 device, the ADC can be one of two types:
●A basic ADC type named ADC2
●An extended ADC type named ADC1
The resolution of both ADC types is 10 bits. The number of external analog inputs depends on the package size of the chosen STM8 family device.
The ADC is a successive approximation Analog to Digital Converter (SAR). Conversion can be performed in continuous mode or single mode. The analog input channels can be selected individually or in scan mode. In scan mode, several channels are converted in sequence, the channel number is incremented for each conversion. The digital result is then stored in registers. Because the ADC resolution is 10 bits and the ADC data register bit length is 16, one ADC result is stored in two 8-bit registers. The 10-bit data is either right or left aligned (this is selectable) in the 16-bit register. Devices with ADC1 can use buffered mode for data storage. In this case, the values converted by the ADC are stored in N data buffers (to decrease CPU overhead).
The event used to start conversion can be generated by software or by the STM8 microcontroller's internal timer 1 (any type of timer event can be used). The start of A/D conversion can also be triggered by an external pin.
The reference voltage for the ADC depends from package. It comes either from an external source - in this case the reference must be connected to two external pins - or is connected internally to analog power supply pins. The value of this reference voltage is restricted in its
magnitude - must be from 2.75V up to VDDA. The measured voltage must be between VREF+ and VREF-. Resolution can be increased by so-called analog zooming - using a smaller
reference voltage around the range of values to be measured.
Devices with ADC1 have an analog watchdog function. This function compares the converted data with high and low thresholds and if a threshold is reached then the analog watchdog flag is set.
If needed, an interrupt can be generated at the end of conversion and if an analog watchdog event occurred.
The ADC is driven by a clock derived from the MCU master clock through a programmable divider. This allows you to select the ADC clock speed according to your application requirements.
4/23 |
Doc ID 14152 Rev 2 |
AN2658 |
ADC hardware description |
|
|
VREF+ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Data bus |
|||||||
VREF- |
|
|
|
|
|
|
Power/Analog |
|
|
|
|
|
|
|
|
|
|
|
|
Data register (10-bits) |
|
|
|
|
|
|
|||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||||||||||||
VDDA |
|
|
|
|
|
|
pins |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||
VSSA |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EOC interrupt |
|||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||||||||
AIN0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
End of conversion |
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||
AIN1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fADC |
|
|
|
fMASTER |
|||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Clock |
|
|
|||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Analog to digital converter |
|
prescaler |
|
|
|
|
|
|
|||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GPIOs
AIN15
ADC registers
ADC_ETR
trigger
Timer 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AWD interrupt |
|||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Analog watchdog |
|
|
|
|
|
|
|
|
|
|
|
||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Analog watchdog registers |
|
|
|
|
|
|
|
|
|
|
|
|||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Data buffer register n |
|
|
|
||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||||||||
|
|
|
|
|
|
|
|
|
|
|
Bandgap reference |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Overrun flag |
|||||||||||||||
|
|
|
|
|
|
|
|
|
|
|
U = 1.232V |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||||
VREF+ |
|
|
|
|
|
|
|
|
|
|
|
|
|
Data buffer register 1 |
|
|
Data bus |
|||||||||||||||||||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||||||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||||||||
VREF- |
|
|
|
|
|
Power/Analog |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Data register (10-bits) |
|
|
|
|
|
|
|
|
|
|
|||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||||||||||
VDDA |
|
|
|
|
|
|
pins |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||
VSSA |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EOC interrupt |
||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||||||||
AIN0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
End of conversion |
|
|
|
|
|
|
|
|
|
|
|||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||
AIN1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fADC |
|
|
|
|
|
|
|
|
fMASTER |
||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Clock |
|
|
|
|
||||||||||
AIN15 |
|
|
|
|
|
|
|
GPIOs |
|
|
|
|
|
Analog to digital converter |
|
|
|
|
prescaler |
|
|
|
|
|
|
|
||||||||||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||||||||||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ADC registers |
|
|
|
|
|||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ADC_ETR
trigger
Timer 1
Doc ID 14152 Rev 2 |
5/23 |
ADC hardware description |
AN2658 |
|
|
The ADC can operate in single or continuous mode. In single mode, the ADC stops after one conversion. In continuous mode, it continues converting (on the same channel or different channel).
Figure 3. and Figure 4. describe ADC operation in single and continuous mode.
CLK |
|
|
|
|
ADON |
|
|
|
|
|
|
ADC inactive |
next conversion |
|
EOC |
|
|
|
|
|
conversion time (14 clocks) |
|
|
|
tSTAB (7µs) |
sync clock* |
|
|
|
3 clocks sampling time |
Software clears |
Start conversion |
||
|
||||
|
10 clocks conversion |
|||
|
EOC bit |
ADON=1 or trigger |
||
|
|
|||
|
|
Software reads |
|
|
Power-on ADC |
Start conversion |
ADC data |
|
|
|
|
|||
ADON=1 |
ADON=1 or trigger |
|
|
|
|
End of conversion |
|
|
CLK |
|
|
ADON |
|
|
EOC |
|
|
tSTAB (7µs) |
conversion time (14 clocks) |
conversion time (14 clocks) |
|
||
|
Software reads |
|
|
ADC data |
|
|
End of conversion |
|
Power-on ADC |
Start conversion |
Software clears |
ADON=1 |
ADON=1 or trigger |
|
|
Next conversion |
EOC bit |
|
starts immediately |
|
In devices with ADC1, these two modes can be usefully combined with scan mode and use of the data buffer registers (buffered mode).
Figure 5. shows all the possible ADC1 operating modes. ADC2 does not support scan and buffered modes.
6/23 |
Doc ID 14152 Rev 2 |
AN2658 |
ADC hardware description |
|
|
Start
Channel X conversion
Stop
Single channel
Single conversion
Start
Channel X conversion Channel X conversion
Channel X conversion Channel X conversion
Single channel Continuous and Buffered conversion
Start
Channel X conversion
Single channel Continuous conversion
Start
Channel 1 conversion
...
Channel n conversion
Stop
Multiple channels
Scan conversion (buffered)
Start
Channel 1 conversion
...
Channel n conversion
Multiple channel Continuous and
Scan conversion (buffered)
Doc ID 14152 Rev 2 |
7/23 |