Runtime Calibration and Compensation of RC Oscillators
Runtime Calibration and Compensation of RC Oscillators
of RC oscillators
8-bit
Features Microcontrollers
• Use of the Oscillator Sampling Interface in calibration.
• Slow RC oscillator frequency prediction.
• Ultra Low Power RC oscillator frequency measurement.
• Fast RC frequency calibration. Application Note
1 Introduction
Due to frequency drift over temperature, the clock sources in AVR should be
calibrated at operating temperature or runtime if system temperature changes over
time, to achieve the best possible accuracy.
Several of the new battery-monitoring devices, e.g. ATmega16HVA, have a very
accurate coulomb counting ADC, and thus need a precise time reference to
achieve the best possible results. They also have protection circuitry, and thus
need a low-power clock source to minimize power consumption. The oscillators,
signature bytes, internal temperature sensor and the Oscillator Sampling Interface
provide the ways to achieve this.
The oscillators in these devices are the Slow RC, Ultra Low Power RC and Fast
RC Oscillator. The Slow RC oscillator has a very predictable behavior over
temperature, and can thus be used for runtime calibration of the other oscillators. In
some devices it is also used for the Coulomb Counting ADC. The Ultra Low Power
oscillator has very low power consumption and enables the lowest possible system
power consumption, thus making it ideal for battery protection. In some devices it is
also used for the Coulomb Counting ADC. The last oscillator is the Fast RC
oscillator that is routed to parts of the system concerned with the AVR core, e.g.
core, flash, eeprom and timers.
Rev. 8053A-AVR-10/08
2 Theory of operation
Calibration of the clock sources in the AVR parts is done by either adjusting the
frequency directly or measuring the actual frequency and compensating for it. This
application note describes the properties and use of the various oscillators in battery
monitoring devices, and how calibration is performed. The new battery-monitoring
devices, e.g. ATmega16HVA have a module called Oscillator Sampling Interface that
is used to simplify measurements of clock periods and calibration.
2 AVR351
8053A-AVR-10/08
AVR351
the Coulomb Counting ADC (CC-ADC) in some devices. Its frequency drifts with
temperature and process, and needs to be compensated for. The recommended way
is comparing the actual period with the calculated Slow RC oscillator reference using
the fast RC to measure both periods and the calculate the ULP RC period from the
Slow RC period.
The nominal period at a high temperature (usually 70°C or 85°C) is also stored in the
signature row. This value gives a rough estimate of the ULP oscillator frequency and
can be used without further compensation for less demanding modules, e.g. to
determine battery protection timing.
2.3.1 Measuring Ultra Low Power oscillator period against Slow RC oscillator
Equation 2-2 shows the formula for measuring and calculating the ULP RC period
with the Slow RC as reference.
Equation 2-2. Calculation of ULP RC Oscillator clock period.
number of CPU cycles in n prescaled ULP RC periods
TULP RC ( μs ) = TSlow RC ⋅
number of CPU cycles in n prescaled Slow RC periods
The default FOSCCAL value is selected such that it is in the lower half of a segment.
It is therefore sufficient to use that default segment and the one below to calibrate the
oscillator frequency to 8MHz over the whole temperature range. To avoid a large
frequency change when shifting between the two segments, a FOSC SEGMENT
3
8053A-AVR-10/08
value is also stored in the signature row. This is the first FOSCCAL value giving a
lower frequency than the lowest value in the default segment, and should be used
when calibrating the Fast RC oscillator.
2.4.1 Runtime calibration of Fast RC against Slow RC oscillator using the Oscillator sampling interface
Equation 2-3. Fast RC oscillator period.
128 ⋅ n
TFast RC ( μs ) = TSlow RC ⋅
number of CPU cycles in n prescaled Slow RC periods
n = number of prescaled Slow RC periods
3 Implementation
This application note includes source code with routines for frequency calculation,
measurement and calibration. It can be downloaded as a zip-archive from Atmel Web.
The source contains code consisting of both functions for calibration and supporting
routines. The code has source documentation (see section Source documentation),
while this section documents the code at a higher level.
4 AVR351
8053A-AVR-10/08
AVR351
row is provided. For more details on the VADC, temperature measurements and the
voltage reference please see application note AVR353.
5
8053A-AVR-10/08
Figure 3-1. Flowchart for the Fast RC oscillator calibration.
Calibrate
Fast RC oscillator
Error = Fast RC
cycles - Target
FOSCCAL++ FOSCCAL--
No No
Return FAILED
Yes No No Yes
Error < 0 Error > 0
FOSCCAL =
previous
FOSCCAL
Return SUCCESS
4 Source documentation
Please see readme.html provided with the code for complete source documentation.
Complete complier information, settings, and device information are also provided
there.
6 AVR351
8053A-AVR-10/08
Disclaimer
Headquarters International
Atmel Corporation Atmel Asia Atmel Europe Atmel Japan
2325 Orchard Parkway Room 1219 Le Krebs 9F, Tonetsu Shinkawa Bldg.
San Jose, CA 95131 Chinachem Golden Plaza 8, Rue Jean-Pierre Timbaud 1-24-8 Shinkawa
USA 77 Mody Road Tsimshatsui BP 309 Chuo-ku, Tokyo 104-0033
Tel: 1(408) 441-0311 East Kowloon 78054 Saint-Quentin-en- Japan
Fax: 1(408) 487-2600 Hong Kong Yvelines Cedex Tel: (81) 3-3523-3551
Tel: (852) 2721-9778 France Fax: (81) 3-3523-7581
Fax: (852) 2722-1369 Tel: (33) 1-30-60-70-00
Fax: (33) 1-30-60-71-11
Product Contact
Literature Request
www.atmel.com/literature
Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any
intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN ATMEL’S TERMS AND
CONDITIONS OF SALE LOCATED ON ATMEL’S WEB SITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED
OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS,
BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the
contents of this document and reserves the right to make changes to specifications and product descriptions at any time without notice. Atmel does not make any
commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in,
automotive applications. Atmel’s products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life.
© 2008 Atmel Corporation. All rights reserved. Atmel®, logo and combinations thereof, AVR® and others, are the registered trademarks or
trademarks of Atmel Corporation or its subsidiaries. Other terms and product names may be trademarks of others.
8053A-AVR-10/08