View detail for AVR401: 8-Bit Precision A/D Converter using Analog Comp. on tinyAVR and megaAVR devices

AVR401: 8-bit Precision A/D Converter
8-bit
Microcontroller
Features
•
•
•
•
•
Very Low Cost
High Precision
Auto-calibration Eliminates Component Inaccuracy
Measures Voltages for 0 to VCC
Maximum Conversion Time: 1.1 ms
Application
Note
Introduction
This application note describes how to perform a kind of dual slope A/D conversion
with an AVR microcontroller. The converter is very low cost, requiring only six discrete
components in addition to the AVR. Five of the controller pins are used (see Figure 1).
This example is based on the AT90S1200 device, but any AVR device with a comparator can be used.
Figure 1. A/D Converter
VCC
RB
PB7
T
AT90S1200
(AIN0) PB0
(AIN1) PB1
C
PB2
Rref 1
Vin
(Vref)
Rin
Rref 2
PB3
Theory of Operation
The capacitor is charged with a constant current supplied by the transistor. The capacitor voltage will rise linearly. To discharge the capacitor, the AIN0-pin is set to output
with a “0” applied. A reference voltage at VCC/2 is supplied by the resistor network Rref1
and R ref2 . When the PB1 and PB2-pins are configured as inputs, the reference is
turned off, and the voltage level at the AIN1-pin will be the input voltage Vin. By setting
the pins as outputs and applying a “0” and a “1”, the level at the AIN1-pin will be VCC/2
(if the resistors are of equal size). The input resistor Rin have to be at least 100 times
higher than the reference resistors R ref1 and Rref2 to avoid measurement errors.
Rev. 0953C–AVR–02/03
1
The algorithm used for the conversion is as follows:
1. Turn on the reference.
2. Charge the capacitor until the reference voltage is reached. Measure the time
needed for this, Tref.
3. Turn off the reference and discharge the capacitor
4. Charge the capacitor until the input voltage is reached. Measure the time needed
for this, Tin.
The conversion cycle is shown in Figure 2.
The time measurement is performed by the Timer/Counter, which is expanded to nine
bits by using the Timer/Counter Overflow Interrupt.
Figure 2. Conversion Cycle
Calculation
Suppose that VCC is 5 volts. The relationship between the input voltage and the reference voltage is given by:
Equation 1
V ref × T in
V in = --------------------------T
ref
The ideal output from the conversion is an 8-bit number, where 0 volts corresponds to
zero and 5 volts is 255. The reference voltage VCC /2 thus corresponds to 128. The
equation can be re-written as:
Equation 2
V
T in × 128
= ------------------------in
T
ref
However, with inaccuracy in the reference resistors, the reference voltage may vary
slightly. To compensate for this, a calibration can be performed by applying a known
voltage at the input, and compare this to the reference. If the applied calibration voltage
is exactly 2.5 volts, the reference voltage can be found by the equation:
Equation 3
2
V
T ref × V cal
T ref × 128
= ------------------------------ = --------------------------ref
T
T
cal
cal
AVR401
0953C–AVR–02/03
AVR401
The calibration cycle is executed by holding the PB7-pin high during Power-up. The calibration voltage is then applied, and the PB7-pin is set low. This starts calibration, and
once performed, the value of the reference voltage is stored in EEPROM. During normal
operation, the reference value is read from EEPROM, and the input voltage is calculated
using Equation 1.
Configuration
Example
As the resulting output is to be eight bits, the timer should be of at least nine bits to
maintain the resolution. The components should be chosen so that the nominal time
charging the capacitor up to VCC is about 256 timer steps. In that way, inaccuracy in the
component values and temperature changes are allowed, without causing the charging
time to be longer than the maximum timer period, or too short, giving lower resolution.
To achieve sufficient accuracy, a prescaler factor of eight or higher should be used. The
AT90S1200 Timer/Counter0 is of only eight bits, so the ninth bit must be handled in software. The following example illustrates how the component values can be found.
First, decide which crystal frequency to operate at. With a 4 MHz crystal, the clock
period is 250 ns. By setting the prescaler to CK/8, the Timer is incremented every 2 µs.
The maximum timer period with nine bits is 512 x 2 µs = 1,024 µs. From this, we set
2 x TREF to 512 µs.
The charging of a capacitor whit a constant current is described by the equation:
Equation 4
I
∆V = ---- × ∆t
C
We can find the required current when the capacitor size, the time and the voltage difference is known:
Equation 5
∆V × CI = ----------------∆t
The capacitor will be charged up to VCC = 5 V, and with a 220 µF capacitor, the transistor must supply a current of 2.15 mA. The RB value is dependent upon the transistor’s
hFE. For a BC558A pnp transistor, hFE is in the range 125 to 250. This makes this transistor ideal for use, since any hFE value in the specified range can be used. To make
sure the full range in hFE can be used, the average value, 188, is used in the calculations. The resulting base current is 11.4 µA.
The transistor is turned on by applying a “0” on the corresponding pin. At this current
values, the transistor base-emitter voltage is about ÷0.1 V. The base resistor is found to
be:
Equation 6
V CC + V BE
4,9V
R B = -------------------------------- = ------------------- = 430kΩ
IB
11,4µA
The reference voltage is generated by the divider network Rref1 and Rref2. The Rin has to
be much larger than these two, so that the input voltage will not influence with the reference voltage. 100 kΩ for Rin and 1 kΩ for each of Rref1 and Rref2 is suitable.
3
0953C–AVR–02/03
The transistor should be connected to a pin as long away from the comparator inputs as
possible. When a pin is switched, a noise spike appear at the adjacent pins. This will
cause problems when measuring low voltages, as the noise spike might trigger the comparator before the capacitor voltage has reached the measured voltage.
Figure 3 shows measured linearity for a 4 MHz clocked application using the component
values calculated in the above example.
Figure 3. Measured Linearity
LINEARITY
250
O
U
T 200
P
U
150
T
V 100
A
L
50
U
E
0
0
1
2
3
4
5
INPUT VOLTAGE
Implementation
The software consist of several subroutines. The routines “reference” and “convert” handles the charging and timing. After they complete execution, the main program must
perform the calculation needed. This is done using two subroutines that performs division and multiplication, “div17u” and “mul9”. There are also two delays used by the other
routines and the main program. They are used to discharge the capacitor completely
and to generate a delay between each conversion.
“reference” Subroutine – The routine discharges the capacitor, turns on the transistor and charges the capacitor
Measures the Reference until the capacitor voltage is equal to the reference voltage. The time from the beginning
of the charging and until the voltages are equal is measured. The capacitor is then disVoltage
charged again. The charging time is used together with the charging time from the
“convert” routine to calculate the input voltage.
This routine does not have to be called every time a conversion is performed, depending
on variations in ambient temperature. Especially the parameter hFE in the transistor is
quite temperature dependent, so if the ambient temperature is varying, the subroutine
will have to be executed frequently. In the example program, the “reference” routine is
called each time a conversion is performed.
Table 1. “reference” Subroutine Performance Figures
4
Parameter
Value
Code Size
24 words
Execution Cycles
Depends on the reference voltage.
Register Usage
• Low Registers
• High Registers
• Global
:None
:2
:1
AVR401
0953C–AVR–02/03
AVR401
Table 2. “reference” Register Usage
Register
Input
Internal
Output
R17
Tref – Holds the time to reach the reference
voltage.
R18
TH – High part of timer.
R20
temp
Figure 4. Flow Chart for “reference”
Reference
AIN0 as Output
(Discharge Capacitor)
PB2 and PB3 as Outputs
(Turn on Vref)
Delay
Clear Timer Variables
AIN0 as Input
(Capacitor Ready
for Charging)
Turn on Transistor
(Charge Capacitor)
Yes
ACO = 1?
(Vcap > Vref?)
No
Timer > 0x1FF?
No
Yes
Store Tref
Stop Timer
Turn off Transistor
AIN0 as Output
(Discharge Capacitor)
PB2 and PB3 as Inputs
(Turn off Vref)
Return
5
0953C–AVR–02/03
“input” Subroutine –
Measures the Input
Voltage
The routine turns on the transistor and charges the capacitor until the capacitor voltage
is equal to the input voltage. Then capacitor is then discharged. The time needed to do
this is measured and stored in Tin.
There should be a few microseconds delay between two conversion cycles, to ensure
that the capacitor is completely discharged. In the example program, this is done by calling a delay routine.
Table 3. “input” Subroutine Performance Figures
Parameter
Value
Code Size
19 words
Execution Cycles
Depends on the input voltage
Register Usage
• Low Registers
• High Registers
• Global
:2
:None
:1
Table 4. “input” Register Usage
Register
Internal
Output
R14
TinH – High part of the input voltage charge time.
R15
TinL – Low part of the input voltage charge time.
R20
6
Input
temp
AVR401
0953C–AVR–02/03
AVR401
Figure 5. Flow Chart for “input”
Input
AIN0 as Input
(Capacitor Ready
for Charging)
Clear Error Flag
Clear Timer Variables
Turn on Transistor
(Charge Capacitor)
Yes
ACO = 1?
(Vcap > Vin?)
No
Timer > 0x1FF?
No
Yes
Set Error Flag
Store Tin
Stop Timer
Turn off Transistor
AIN0 as Output
(Discharge Capacitor)
Return
7
0953C–AVR–02/03
“T0_int” Interrupt
Service Routine
The only function for this routine is to increment the TH variable, so a 16-bit Timer is created. Only nine bits are used.
Table 5. “T0_int” Interrupt Performance Figures
“mpy9u” 9 × 8 Bit
Multiplication
Parameter
Value
Code Size
2 words
Execution Cycles
9 – including the reti instruction
Register Usage
• Low Registers
• High Registers
• Global
:None
:None
:1
This routine performs a 9 × 8 bit multiplication. The 9-bit multiplier must be stored in the
Carry Flag (MSB) and the “mp9u” Register. The multiplicand is stored in the “mc9u”
Register. The answer is placed in “C:m9uH:m9uL”. The registers used for the result are
the same as those used for the input to the division routine. The routine is based on the
“mpy8u” multiplication routine described in application note AVR 200.
Table 6. “mpy9u” Subroutine Performance Figures
Parameter
Value
Code Size
11 words
Execution Cycles
83
Register Usage
• Low Registers
• High Registers
• Global
• Flags
:3
:None
:1
:C
Table 7. “mpy9u” Register Usage
Register
Input
R0
mc9u – Multiplicand
R1
mp9u – Multiplier
Internal
m9uL – Result Low byte
R2
C-flag
R20
8
Output
m9uH – Result High byte
Multiplier, ninth bit
Result, 17th bit
temp – Used as Loop Counter
AVR401
0953C–AVR–02/03
AVR401
“div17u” 17/16 Bit
Division
This routine performs a 17/16 bit division. The 17-bit dividend must be stored in the
(C:didH:didL) variable, where the Carry Flag is most significant. The divisor is stored in
the (divH:divL) variable. The result is placed in (resH:resL) and the reminder in
(remH:remL). The routine is based on the “div16u” multiplication routine described in
application note AVR 200.
Table 8. “div17u” Subroutine Performance Figures
Parameter
Value
Code Size
18 words
Execution Cycles
209 min, 292 max.
Register Usage
•Low
Low Registers
• High Registers
• Global
• Flags
:6
:None
:1
:C
Table 9. “div17u” Register Usage
Register
Internal
Output
R1
didL – Low part dividend
dresL – Low part result
R2
didH – High part dividend
dresH – High part result
C-flag
Example Program
Input
17th bit of dividend
R3
divL – Low part divisor
R4
divH – High part divisor
R5
remL – Low part reminder
R6
remH – High part reminder
The included example program performs repeated conversions. First, the charging time
for the reference is measured, then for the input voltage. The result is output to Port D
and Port B pin 4 (MSB). The result is inverted before it is output, so active low LEDs can
be connected to show the result. This conversion cycle is repeated in an endless loop.
To perform a calibration the PB7-pin must be initially in high state and the capacitor has
to be discharged. Afterwards the user should apply 2.5 volts at the input before setting
the PB7-pin low. The calibrated Vref is stored in EEPROM, where it is fetched at every
normal Power-up.
9
0953C–AVR–02/03
Performance Figures
Table 10. Overall Performance Figures
Parameter
Value
Code Size
43 words - Conversion routines only (not mpy9u and div17u)
147 words - Complete application note
Register Usage
• Low Registers
• High registers
• Pointers
Interrupt Usage
Timer/Counter 0 Interrupt
Peripheral Usage
Timer/Counter0
Analog Comparator
Port B, pin 0 to 3 and pin 7
Port D, all pins (example program only)
Port B, pin 4 (example program only)
:9
:5
:None
The calibration routine can be skipped if only relative values are measured. The reference voltage is then assumed to be 128, which will also make the calculations easier.
The reference network can be substituted with a voltage reference to achieve even better accuracy. It is then possible to measure variations in VCC by connecting it via a
voltage divider network to the input.
10
AVR401
0953C–AVR–02/03
AVR401
11
0953C–AVR–02/03
Atmel Headquarters
Atmel Operations
Corporate Headquarters
Memory
2325 Orchard Parkway
San Jose, CA 95131
TEL 1(408) 441-0311
FAX 1(408) 487-2600
Europe
Atmel Sarl
Route des Arsenaux 41
Case Postale 80
CH-1705 Fribourg
Switzerland
TEL (41) 26-426-5555
FAX (41) 26-426-5500
Asia
Room 1219
Chinachem Golden Plaza
77 Mody Road Tsimhatsui
East Kowloon
Hong Kong
TEL (852) 2721-9778
FAX (852) 2722-1369
Japan
9F, Tonetsu Shinkawa Bldg.
1-24-8 Shinkawa
Chuo-ku, Tokyo 104-0033
Japan
TEL (81) 3-3523-3551
FAX (81) 3-3523-7581
2325 Orchard Parkway
San Jose, CA 95131
TEL 1(408) 441-0311
FAX 1(408) 436-4314
RF/Automotive
Theresienstrasse 2
Postfach 3535
74025 Heilbronn, Germany
TEL (49) 71-31-67-0
FAX (49) 71-31-67-2340
Microcontrollers
2325 Orchard Parkway
San Jose, CA 95131
TEL 1(408) 441-0311
FAX 1(408) 436-4314
La Chantrerie
BP 70602
44306 Nantes Cedex 3, France
TEL (33) 2-40-18-18-18
FAX (33) 2-40-18-19-60
ASIC/ASSP/Smart Cards
1150 East Cheyenne Mtn. Blvd.
Colorado Springs, CO 80906
TEL 1(719) 576-3300
FAX 1(719) 540-1759
Biometrics/Imaging/Hi-Rel MPU/
High Speed Converters/RF Datacom
Avenue de Rochepleine
BP 123
38521 Saint-Egreve Cedex, France
TEL (33) 4-76-58-30-00
FAX (33) 4-76-58-34-80
Zone Industrielle
13106 Rousset Cedex, France
TEL (33) 4-42-53-60-00
FAX (33) 4-42-53-60-01
1150 East Cheyenne Mtn. Blvd.
Colorado Springs, CO 80906
TEL 1(719) 576-3300
FAX 1(719) 540-1759
Scottish Enterprise Technology Park
Maxwell Building
East Kilbride G75 0QR, Scotland
TEL (44) 1355-803-000
FAX (44) 1355-242-743
e-mail
[email protected]
Web Site
http://www.atmel.com
© Atmel Corporation 2003.
Atmel Corporation makes no warranty for the use of its products, other than those expressly contained in the Company’s standard warranty
which is detailed in Atmel’s Terms and Conditions located on the Company’s web site. The Company assumes no responsibility for any errors
which may appear in this document, reserves the right to change devices or specifications detailed herein at any time without notice, and does
not make any commitment to update the information contained herein. No licenses to patents or other intellectual property of Atmel are granted
by the Company in connection with the sale of Atmel products, expressly or by implication. Atmel’s products are not authorized for use as critical
components in life support devices or systems.
ATMEL ® and AVR ® are the registered trademarks of Atmel.
Other terms and product names may be the trademarks of others.
Printed on recycled paper.
0953C–AVR–02/03
0M