View detail for AVR400: Low Cost A/D Converter using Analog Comp. on tinyAVR and megaAVR devices

View detail for AVR400: Low Cost A/D Converter using Analog Comp. on tinyAVR and megaAVR devices
AVR400: Low Cost A/D Converter
Features
•
•
•
•
•
•
Interrupt Driven
Code Size: 23 Words
Low Use of External Components
Resolution: 6 Bits
Measurement Range: 0 - 2 V
Runs on Any AVR Device with 8-bit Timer/Counter and Analog Comparator
Introduction
8-bit
Microcontroller
Application
Note
This application note targets cost and space critical applications that need an ADC. It
describes how to make a simple ADC with only two external components, a resistor
and a capacitor. The design enables a very compact and inexpensive application.
Theory of Operation
Nearly all AVR microcontrollers feature an Analog Comparator which makes it easy to
implement an ADC. The signal to be measured is connected to the inverted input, and
a reference signal is connected to the non-inverting input. The reference signal is generated by charging a capacitor through a resistor. When the capacitor is being
charged, the voltage across it will follow an exponential curve. If the voltage range to
be measured is limited to 2/5*VCC, the exponential curve is a good approximation to a
straight line. The voltage of the applied signal, UIN is found by measuring the time it
takes for the voltage across the capacitor to rise above the applied voltage. By using
one pin on port B to control the charging and discharging of the capacitor, only three
port pins are used. A schematic diagram is found in Figure 1.
Figure 1. Circuit Diagram
5V
RESET
VCC
PD0
PB7(SCK)
PD1
PB6(MISOI)
XTAL2
PB5(MOSI)
XTAL1
PB4
PD2(INT0)
PB3
PD3
PB2
PD4(T0)
PB1(AIN1)
PD5
PB0(AIN0)
GND
PD6
AT90S1200
R
UIN
C
Rev. 0942B–AVR–05/02
1
The time constant of the R/C network must be tuned so that the following equation is
satisfied:
2
1002
512
---------- = – RC ln  1 – --- ⇒ RC = ------------
f
f
5
Component values for some typical Oscillator frequencies are shown in Table 1. If the
time constant varies from this, it will cause errors in the result. This makes it necessary
to use components with high accuracy in the RC-network. The voltage curve for the
capacitor is shown together with a straight line in Figure 2. As the supply voltage is used
as a reference, it must be stable during the conversion.
Figure 2. A/D Converter Linearity
Table 1. R/C Network Coomponent Values
XTAL (MHz)
1
2
4
6
8
10
12
14
16
R (kΩ)
100
33
30
30
27
100
56
47
160
C (nF)
10
15
8.2
5.6
4.7
1
1.5
1.5
0.39
To ensure proper operation, the capacitor must be discharged for at least 200 µs
between conversions. If the capacitor is not properly discharged, it will not be possible to
measure low values. If the voltage input is larger than 2/5 VCC, the converter will return
the maximum value. This is accomplished by loading an offset value into the
Timer/Counter0 Register before conversion starts. The timer will give an Overflow Interrupt after 512 cycles (64*8). This is the time it takes for the voltage across the capacitor
to reach 2/5 VCC. If the voltage is within the operating range, an Analog Comparator
interrupt will occur. Offset is subtracted from the measured value.
Implementation
2
The ADC uses Timer/Counter0 and the Analog Comparator interrupts. This frees the
MCU resources during conversion.
AVR400
0942B–AVR–05/02
AVR400
Subroutine
“convert_init” – ADC
Initialization
This subroutine is called to initialize the ADC. This must be done before the ADC is
used. By calling this subroutine, the Comparator and Timer interrupts are enabled, and
the control pin is set as output. Then the “SEI” instruction, which enables global interrupts, should be called to enable the A/D converter. By calling “CLI”, the A/D converter is
disabled.
Table 2. “convert_init” Subroutine Performance Figures
Parameter
Value
Code Size
6 words
Execution Cycles
10, including the RET instruction
Register Usage
Low Registers
High Registers
Pointers
:None
:1
:None
Table 3. “convert_init” Register Usage
Register
R16
Input
Internal
Output
"result" – Scratch Register
Figure 3. “convert_init” Flow Chart
SET COMPARATOR AND INTERRUPT
ENABLE TIMER INTERRUPT
SET CONTROL PIN OUTPUT
FINISHED - A/D INITIALIZED
“AD_convert”
Subroutine – Start an
A/D Conversion
This routine is used to start an A/D conversion. It pre-loads the Counter with 256 - 64
and starts counting up at the frequency XTAL/8. The Conversion Complete Flag (the Tflag in the Status Register) is cleared, and the charging of the capacitor is started.
Table 4. “AD_convert” Subroutine Performance Figures
Parameter
Value
Code Size
7 words
Execution Cycle
10 (including RET)
Register Usage
Low Registers
High Registers
Pointers
Status Flags
:None
:1
:None
:1
3
0942B–AVR–05/02
Table 5. “AD_convert” Register usage
Register
Input
R16
Internal
Output
"result" – Scratch Register
SREG
T-flag – This flag is used to indicate that a
conversion is in progress.
Figure 4. ADC Conversion Flow Chart
LOAD OFFSET INTO COUNTER
CLEAR CONVERSION COMPLETE FLAG
START COUNTER, CLOCK/8
START CHARGING
FINISHED - CONVERSION STARTED
“ANA_COMP” –
Interrupt Handling
Routine
This routine is executed when a conversion is complete. It loads the Timer/Counter0
value, stops the timer and sets the Conversion Complete Flag (T-flag in SREG). The offset is then subtracted from the timer value. It is necessary to subtract one more than the
offset, because the interrupt handling takes a minimum of seven cycles.
Table 6. “ANA_COMP” Subroutine Performance Figures
Parameter
Value
Code Size
7 words
Execution Cycle
11 (including RETI)
Register Usage
Interrupt Usage
Low Registers
High Registers
Pointers
Status Flags
:None
:2
:None
:1
Timer/Counter0 and Analog Comparator Interrupt
Table 7. “ANA_COMP” Register usage
Register
Internal
Output
R16
"result" – Stores the Timer value
"result" – Contains the result from
the A/D conversion
R17
"temp" – Scratch Register
SREG
4
Input
T-Flag – This flag is used to indicate
that the conversion is finished
AVR400
0942B–AVR–05/02
AVR400
Figure 5. "ANA_COMP" Flowchart
LOAD TIMER VALUE
STOP TIMER
REMOVE OFFSET FROM RESULT
SET CONVERSION COMPLETE FLAG
FINISHED - CONVERSION COMPLETE
Example Program
The example program that is included in this application note performs successive conversions, and presents the data as binary values on port B.
Table 8. Overall Performance Figures
Parameter
Value
Code Size
23 words – A/D converter routines only
37 words – Complete with test program
Register Usage
Low Registers
High Registers
Pointers
Status Flags
Interrupt Usage
Timer/Counter0 Overflow Interrupt
Analog Comparator Interrupt
Peripheral Usage
Timer/Counter0
Analog Comparator (port B pin0 and pin1)
Port B pin2
Port D (example program only)
:None
:2
:None
:1
5
0942B–AVR–05/02
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 2002.
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.
0942B–AVR–05/02
0M
Was this manual useful for you? yes no
Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Download PDF

advertisement