STMicroelectronics AN2979 Implementing a simple adc using the stm8l101xx comparator Datasheet

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
AN2979
Contents
1
2
Application description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1
Comparator features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2
ADC implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Software description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1
Application flowcharts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3
Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4
Measurements and calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5
2/12
4.1
Typical measurements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.2
Precision of the measured value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.3
How to get a better accuracy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.3.1
Hardware solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.3.2
Software solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Doc ID 15651 Rev 1
AN2979
Application description
1
Application description
1.1
Comparator features
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.
Note:
For external comparison be aware that the STM8L101xx comparator maximum input value
is VDD -1.25 V with VDDmax = 3.6 V
1.2
ADC implementation
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
●
VDD is the input voltage
●
t 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
Application description
Figure 1.
AN2979
Capacitor charging curve
PWM output
Capacitor charging curve
VDD(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
+
COMP_OUT
-
R=10 k7
C=100 nF
AI
1. Condition: VDD=3.3 V, R=10 kΩ, C=100 nF
Note:
4/12
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
2
Software description
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
main.c
param.c
Application layer
stm8l10x.h
API layer
stm8l10x.h
stm8l10x.h
param.h
stm8l10x tim
stm8l10x.h
stm8l10x comp
stm8l10x cl
stm8l10x api
Standard
peripheral drivers
stm8l10x tim
stm8l10x.comp
stm8l10x cl
stm8l10x api
STM8L101x
microcontroller
Note:
ai15372
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
2.1
AN2979
Application flowcharts
Figure 4.
Main loop flowchart
Start
Clock configuration
Configure the system clock to provide a master
clock frequency fMASTER = 16 MHz
Enable TIM2 peripheral clock
GPIO configuration
Configure PC4 as input floating
Configure PB0 as output push pull for the PWM
A/D converter
initialization
LCD configuration
Optional
Calibration
Start A/D conversion
Comparator configuration
Enable comparator interface
Configure COMP2 channel3
With external reference and polarity high
Connect Comp2 output to the TIM2 input capture1
TIM configuration
TIM2 to generate the PWM
Enable TIM2 capture / compare interrupt enable
Enable general interrupts
SPI configuration
Display on LCD
Configure COMP2 channel4
Compare to the reference value
Enable TIM2
Display on LCD
End
ai15766
Figure 5.
Get conversion value flowchart
Start
VIN > VREF
Get timer value
Get the conversion value
Clear TIM2 pending bit
Display on the LCD
End
ai15767
6/12
Doc ID 15651 Rev 1
AN2979
3
Hardware description
Hardware description
Figure 6.
Circuit diagram
PB0
STM8L10x
R=10 kΩ
PC4
VDD
PD2
(COMP2_CH3)
PA6
(COMP_REF)
!$# INPUT
VSS
C=100 nF
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
AN2979
4
Measurements and calibration
4.1
Typical measurements
The following values are given for information only.
Table 1.
4.2
Measured value after conversion and margin error for VDD=3.3 V
Value to be measured (V)
Measured value after conversion (V)
Error (%)
0.25
0.204
18.4%
0.5
0.419
16.2%
0.8
0.704
12%
1
0.903
9.7%
1.25
1.14
8.8%
1.5
1.39
7.33%
1.75
1.628
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: Cacc1 =90 nF or Cacc2 =110 nF
if R=10 kΩ with 2% accuracy: Racc1=9.8 kΩ or Racc2=10.2 kΩ
Calculation of the RC constant:
3
–9
T = R × C = ( 10 × 10 ) × ( 100 × 10 ) = 1 ms
3
–9
T ACC1 = R ACC1 × C ACC1 = ( 9.8 × 10 ) × ( 90 × 10 ) = 882 μs
3
–9
T ACC2 = R ACC2 × C ACC2 = ( 10.2 × 10 ) × ( 110 × 10 ) = 1.122 ms
Charge equations:
V meas = V DD × ⎛ 1 – exp ⎛ – --t-⎞ ⎞
⎝
⎝ T⎠ ⎠
⎛
⎛
t ⎞⎞
V measacc1 = V DD × ⎜ 1 – exp ⎜ – -----------------⎟ ⎟
⎝ T acc1⎠ ⎠
⎝
⎛
⎛
t -⎞ ⎞
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
y = 3.3(1-exp(-x/0.882))
1.5
y = 3.3(1-exp(-x))
y = 3.3(1-exp(-x/1.122))
1
tmax = 0.970ms
tacc1max = 0.856ms
0.5
tacc2max = 1.09 ms
0.5
tacc1max
1
1.5
2
2.5
ms
tmax tacc2max
1. In blue: Vmeasacc1 ---- In red: Vmeas ---- In green: Vmeasacc2
2. Condition: VDD=3.3 V
Note:
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.
4.3
How to get a better accuracy
4.3.1
Hardware solution
The first solution is to choose components with a high accuracy value (this may imply a
higher cost).
4.3.2
Software solution
The second solution is to calibrate the RC constant in the software using VDD 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
VDD via a resistor bridge.
Figure 8.
Evaluation board calibration schematic
0"
2 K7
34 - , X
6$$
0 $ # / - 0 ? # ( 0#
0 $ # / - 0 ? # ( 0!
6 33
# / - 0?2%&
K7
#ALIBRATION INPUT
!$# INPUT
K7
# N&
AI
As VDD=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
VDD=3.3 V, R=10 kΩ and C=100 nF.
Table 2.
10/12
Accuracy measurement when the calibration is performed
Value to be measured (V)
Measured value after conversion (V)
Error (%)
0.25
0.233
6.8%
0.5
0.475
5%
0.8
0.792
1.25%
1
0.99
1%
1.2
1.211
0.92%
1.5
1.512
0.8%
1.75
1.784
1.9%
Doc ID 15651 Rev 1
AN2979
5
Revision history
Revision history
Table 3.
Document revision history
Date
Revision
12-Feb-2010
1
Changes
Intitial release.
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
Similar pages