Implementing a simple ADC using the STM8L101xx comparator

AN2979
Application note
Implementing a simple ADC using the STM8L101xx comparator
Introduction
This application note gives a simple method for implementing an A/D converter with a minimum amount of external components: one resistor and one capacitor.
The pratical application example described in this document uses the STM8L101xx microcontroller comparator.
February 2010 Doc ID 15651 Rev 1 1/12
www.st.com
Contents
Contents
AN2979
Measurements and calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Precision of the measured value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
How to get a better accuracy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2/12 Doc ID 15651 Rev 1
AN2979 Application description
Note:
The STM8L101xx microcontroller embeds two zero-crossing comparators sharing the same current bias and the same voltage reference.
This voltage reference can be:
●
● internal (comparison to ground) or external (comparison to a reference pin voltage)
Each comparator is connected to 4 channels which can be used to generate an interrupt, a timer input capture or a timer break. Their polarity can be inverted.
For external comparison be aware that the STM8L101xx comparator maximum input value is V
DD
-1.25 V with V
DDmax
= 3.6 V
Each comparator can be used to implement an ADC. This technique is based on a simple principle: the signal to be measured is connected to the non-inverted input and the reference signal is an external signal connected to the inverting input.
In the demonstration software, the comparator used is COMP2 and the channel used as the signal to be measured is the channel 3 (pin PD2).
The reference signal is generated by charging a capacitor through a resistor. While the voltage across the capacitor is being charged, it follows an exponential curve.
This exponential equation has been implemented in the software. The time taken by the capacitor voltage to rise above the voltage value to be converted is used in the charge equation to retrieve the digital conversion value.
Charge equation:
V meas
= V
DD
×
1 – exp t
T
●
●
●
Where:
Vmeas
is the value to be mesaured
●
V t
DD
is the input voltage
is the time measured by timer2 when the comparator detects that the input voltage is above the reference voltage
T
is the RC constant (here R=10 k
Ω
and C=100 nF so T= 1 ms)
The capacitor is charged and discharged using the timer 2 PWM on channel1/ PB0.
A timer is programmed to generate a 2 ms PWM with a duty cycle of 0.25. This 2-ms period permits an ADC implementation using the full voltage range that the comparator tolerates.
The capacitor charging curve is shown in the following figure.
Doc ID 15651 Rev 1 3/12
4/12
Application description
Figure 1.
Capacitor charging curve
Note:
AN2979
PWM output
Capacitor charging curve
V
DD
(V)
3.3
3
2.5
2
1.5
1
0.5
1 2 3 4 5 6 7 8 9
T(ms)
The figure below shows the connection of the comparator to the required external components.
Figure 2.
Comparator connections
PD2
PA6
+
PB0/PC4
R=10 k
7
-
C=100 nF
COMP_OUT
AI
1.
Condition: V
DD
=3.3 V, R=10 k
Ω
, C=100 nF
When using the STM8L101-EVAL evaluation board you need to connect the PB0 pin to the
PC4 pin. This way you use the resistor and capacitor already present on the board (no hardware needed). Pin PC4 is configured as input floating to avoid any conflict.
Doc ID 15651 Rev 1
AN2979 Software description
The software provided with this application note describes a way of implementing an A/D converter (using a timer and analog comparator interrupts).
●
●
●
●
●
The application uses four peripherals:
CLK: the clock enables and provides the correct clock frequency for the peripherals.
COMP: the comparator detects when the input voltage is above the reference voltage
TIM2: Timer 2 provides the 2 ms PWM with a duty cycle of 0.25 generating the capacitor charge and discharge. Timer 2 Capture/compare interrupt routine handles the conversion.
GPIO: the general purpose I/O handles the I/O used.
SPI: used to communicate with the LCD.
A generic file param.h contains the parameter values that can be modified in order to re-use the application and adapt it easily to other conditions.
At the start, the LCD displays “STM8L ADC using COMP”. Then it continously displays the measured values.
Figure 3.
Application architecture & description
m a in.c
Application layer
param.c
s tm 8 l10x.h
API layer s tm 8 l10x.h
param.h
s tm 8 l10x.h
s tm 8 l10x.h
s tm 8 l10x tim s tm 8 l10x comp s tm 8 l10x cl s tm 8 l10x a pi s tm 8 l10x tim s tm 8 l10x.comp
s tm 8 l10x cl s tm 8 l10x a pi
Standard peripheral drivers
Note:
S TM 8 L101x microcontroller a i15 3 72
The software can be compiled with Cosmic and Raisonance compilers. It contains projects for STVD and Raisonance IDE.
A calibration of some parameters can be optionally set if “#define Calibration” is uncommented in the param.h file.
Doc ID 15651 Rev 1 5/12
Software description
Figure 4.
Main loop flowchart
S t a rt
Clock config u r a tion
GPIO config u r a tion
A/D converter initi a liz a tion
LCD config u r a tion
Option a l
C a li b r a tion
S t a rt A/D conver s ion
Config u re the s y s tem clock to provide a m as ter clock fre qu ency f
MA S TER
= 16 MHz
En ab le TIM2 peripher a l clock
Config u re PC4 as inp u t flo a ting
Config u re PB0 as o u tp u t p us h p u ll for the PWM
Comp a r a tor config u r a tion
En ab le comp a r a tor interf a ce
Config u re COMP2 ch a nnel 3
With extern a l reference a nd pol a rity high
Connect Comp2 o u tp u t to the TIM2 inp u t c a pt u re1
TIM config u r a tion
TIM2 to gener a te the PWM
En ab le TIM2 c a pt u re / comp a re interr u pt en ab le
En ab le gener a l interr u pt s
S PI config u r a tion
Di s pl a y on LCD
Config u re COMP2 ch a nnel4
Comp a re to the reference v a l u e
En ab le TIM2
Di s pl a y on LCD
End
Figure 5.
Get conversion value flowchart
S t a rt
V
IN
> V
REF
Get timer v a l u e
Get the conver s ion v a l u e
Cle a r TIM2 pending b it
Di s pl a y on the LCD
End a i15766
AN2979
a i15767
6/12 Doc ID 15651 Rev 1
AN2979 Hardware description
Figure 6.
Circuit diagram
R=10 kΩ
C=100 nF
PB0
STM8L10x
PC4
PA6
(COMP_REF)
VDD
PD2
(COMP2_CH3)
VSS
!$#INPUT
AI
■
■
■
STM8L101 evaluation board settings:
■
Remove jumper JP2 to connect an external input voltage instead of the potentiometer input. With the potentiometer RV1, the value is limited to 0.8 V and the signal is noisy (due to the LCD). So the display on the LCD screen would not be stable.
Connect the external ADC voltage input directly to PD2.
Ground together the evaluation board and the external ADC signal.
An accurate 3.3 V MCU voltage can be tuned on the evaluation board using RV4.
Doc ID 15651 Rev 1 7/12
Measurements and calibration
4 Measurements and calibration
AN2979
4.2
The following values are given for information only.
Table 1.
Measured value after conversion and margin error for V
DD
=3.3 V
Value to be measured (V) Measured value after conversion (V) Error (%)
0.25
0.5
0.8
1
1.25
1.5
1.75
0.204
0.419
0.704
0.903
1.14
1.39
1.628
18.4%
16.2%
12%
9.7%
8.8%
7.33%
6.97%
Precision of the measured value
When using the RC charge equation, the precision of the measured value depends on the accuracy of the capacitor C and the resistor R.
Accuracy example:
if C=100 nF with 10% accuracy: C acc1
=90 nF or C acc2
=110 nF if R=10 k
Ω with 2% accuracy: R acc1
=9.8 k
Ω
or R acc2
=10.2 k
Ω
Calculation of the RC constant
:
T
=
R
×
C
=
(
10
×
10
3
) × (
100
×
T
T
ACC1
ACC2
=
=
R
R
ACC1
ACC2
×
×
C
C
ACC1
ACC2
=
=
10
–
9
)
=
1 ms
(
9.8
(
10.2
×
×
10
3
)
10
3
)
×
×
(
90
×
(
110
10
–
9
)
×
10
–
=
9
)
882
μ s
=
1.122
ms
Charge equations:
V meas
=
V
DD
×
1
– exp t
T
V measacc1
=
V
DD
×
1
– exp
– -----------------
T acc1
⎠
V measacc2
= V
DD
×
1 – exp
⎝
–
T acc2
8/12 Doc ID 15651 Rev 1
AN2979 Measurements and calibration
The following figure shows the impact of the capacitor and resistor accuracy.
Figure 7.
Charge equation depending on RC accuracy
V
3
2.5
Vmeas_max = 2.05
2
1.5
1
0.5
0.5
tacc1max
1 tmax tacc2max
1.5
y = 3.3(1-exp(-x/0.882)) y = 3.3(1-exp(-x)) y = 3.3(1-exp(-x/1.122)) tmax = 0.970ms
tacc1max = 0.856ms
tacc2max = 1.09 ms
2
2.5
ms
Note:
4.3
1.
2.
In blue: V measacc1
---- In red: V meas
---- In green: V measacc2
Condition: V
DD
=3.3 V
The results are not only impacted by the marging error due to hardware components but also by the accuracy of the MCU clock. In this example, the device works at a HSI frequency of 16 MHz at 25 °C, that is, with a clock accuracy of 1% (refer to the datasheet).
The temperature variation also impacts the accuracy of the measured value as the microcontroller, resistors and capacitors are temperature dependent.
How to get a better accuracy
The first solution is to choose components with a high accuracy value (this may imply a higher cost).
The second solution is to calibrate the RC constant in the software using V
DD
as a reference.
Implementation description
In order to perform the calibration, you need to uncomment the “#define calibration” in the
“param.h” file.
Doc ID 15651 Rev 1 9/12
Measurements and calibration AN2979
The calibration is then performed by firware and the RC constant is updated. This constant is used in the charge equation.
On the STM8L101-EVAL evaluation board, the COMP2 channel 4 (PD3) is connected to
V
DD
via a resistor bridge.
Figure 8.
Evaluation board calibration schematic
2 K 7
# N &
3 4 - , X 6
$ $
0 "
0 $ # / - 0 ? # (
0 #
0 $ # / - 0 ? # (
0 !
# / - 0 ? 2 % &
6 33
#ALIBRATIONINPUT
!$#INPUT
K 7
K 7
AI
As V
DD
=3.3 V, PD3 is a fixed value equal to 0.4 V
In the file “param.h” you need to define the “expected_value” equal to 0.4 ( this value needs to be updated if a different resistor bridge is used).
The specific function “Calibration()” configures the COMP2 Channel 4 and compares the value measured on this channel with the expected value. This function then updates the RC constant that will be used afterwards.
When the calibration is performed, the COMP2 configuration changes to use the Channel 3
(PD2).
The following table lists a few measurement examples with a calibration performed at
V
DD
=3.3 V, R=10 k
Ω
and C=100 nF.
Table 2.
Accuracy measurement when the calibration is performed
Value to be measured (V) Measured value after conversion (V)
0.25
0.5
0.8
1
1.2
1.5
1.75
0.233
0.475
0.792
0.99
1.211
1.512
1.784
Error (%)
6.8%
5%
1.25%
1%
0.92%
0.8%
1.9%
10/12 Doc ID 15651 Rev 1
AN2979
Table 3.
Date
Document revision history
Revision
12-Feb-2010 1 Intitial release.
Changes
Revision history
Doc ID 15651 Rev 1 11/12
AN2979
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, 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.
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 services described herein.
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 intellectual property contained therein.
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 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 not create or extend in any manner whatsoever, any liability of ST.
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 owners.
© 2010 STMicroelectronics - All rights reserved
STMicroelectronics group of companies
Australia - Belgium - Brazil - Canada - China - Czech Republic - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan -
Malaysia - Malta - Morocco - Philippines - Singapore - Spain - Sweden - Switzerland - United Kingdom - United States of America
www.st.com
12/12 Doc ID 15651 Rev 1
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project