AN2326
Application note
Calibrating the RC oscillator of the
ST7ULTRALITE MCU using the mains
Introduction
The ST7ULTRALITE microcontroller contains an internal RC oscillator which can be trimmed to a specific frequency with the required accuracy. The oscillator frequency has to be calibrated by software using the RCCR register (RC Control Register) and the SICSR register (System Integrity Control/Status Register). The value entered in the RCCR/SICSR registers will switch ON a corresponding number of resistors that will modify the oscillator frequency. Whenever the ST7ULTRALITE microcontroller is reset, the 10-bit value
contained in the RCCR/SICSR registers is restored to its default value (3FFh) i.e. the lower
volatile memory and load it in the RCCR/SICSR registers afterProduct(s)each reset. However, if any of the external conditions (temperature or voltage, for instance) changes too drastically, the
possible frequency, so each time the device is reset, you have to load the calibration value in
the RCCR/SICSR registers. There are predefined calibration values stored in memory (refer
to the” Internal RC Oscillator Adjustment” section in the ST7ULTRALITE datasheet). You
can load one of these values in the RCCR/SICSR registers if one of the operating conditions
matches that in your application. Otherwise, you can define your own value, store it in non
stored value may no longer produce the required accuracy. One solution is to recalculate the RCCR/SICSR register values after each reset, bas d on an external reference.
The purpose of this application note is to pre ent a software solution using the frequency of the European standard mains (220V/50Hz) as a timebase to adjust the internal RC oscillator
of the ST7ULTRALITE to 8 MHz. The same approach can also be used for the US mains |
|
standard (110V/60Hz). |
Obsolete |
- |
The basicProduct(s)software takes le than 200 ms to calibrate the oscillator and uses less than128 bytes of program memory and five bytes of RAM for its simplest version. These RAM bytes
can be freed for other purposes when the calibration is done. Another example using averages is given in this application note. This can be useful with noisy mains.
This applicati n note also contains the diagram of a low cost circuit which converts the
mains into a 5 volt power supply and protects the microcontroller from overcurrent on the Obsoleteinput connected to the mains.
April 2006 |
Rev 1 |
1/20 |
www.st.com
Contents |
AN2326 |
|
|
Contents
1 |
Calibration software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
3 |
|
|
1.1 |
Software principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
3 |
|
1.2 |
Basic version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
4 |
|
1.3 |
Average version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
5 |
2 |
Power supply and timebase delivery circuit . . |
. . . . . . . . . . . . . . . . . . . |
. 7 |
|||
|
2.1 |
Basic circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . |
. . . . . . . . . . . . . . . . . . . . . |
7 |
||
|
2.2 |
Hardware protection . . . . . . . . . . . . . . . . . . . . . |
. . . . . . . . . . . . . . . . . . . . . |
8 |
||
3 |
Conclusion . |
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
Product(s). . . . . . . . . . . . . . . . . . . |
9 |
||
4 |
Software examples |
10 |
||||
. . . . . . . . . . . . . . . . . . |
||||||
|
4.1 |
Single alternance . . . . . . . . . . . . . . . . . . . . . . . |
. . . . . . . . . . . . . . . . . . . . |
10 |
||
|
|
4.1.1 |
Main program . . . . . . . . . . . . . . . . . . . . . . . |
. . . . . . . . . . . . . . . . . . . . . . |
10 |
|
|
|
4.1.2 |
Input capture interrupt . . . . . . . . . . . . . . . . |
. . . . . . . . . . . . . . . . . . . . . . |
11 |
|
|
|
4.1.3 |
Obsolete |
12 |
||
|
|
Timebase interrupt . . . . . . . . . . . . . . . . . . . |
. . . . . . . . . . . . . . . . . . . . . . |
|||
|
|
4.1.4 Writing in non volatile memory for products without Data EEPROM . . . |
12 |
|||
|
|
4.1.5 |
Writing in non volatile memory for products with Data EEPROM . . . . . 13 |
|||
|
|
4.1.6 Detailed basic version software flowchart . |
. . . . . . . . . . . . . . . . . . . . . . |
14 |
||
|
4.2 |
Average version . . . . -. . . . . . . . . . . . . . . . . . . . |
. . . . . . . . . . . . . . . . . . . . |
15 |
||
|
|
Product(s) |
|
15 |
||
|
|
4.2.1 |
Main program . . . . . . . . . . . . . . . . . . . . . . . |
. . . . . . . . . . . . . . . . . . . . . . |
||
|
|
4.2.2 |
Input capture interrupt . . . . . . . . . . . . . . . . |
. . . . . . . . . . . . . . . . . . . . . . |
16 |
|
|
|
4.2.3 |
Timebase interrupt . . . . . . . . . . . . . . . . . . . |
. . . . . . . . . . . . . . . . . . . . . . |
17 |
|
5 |
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . |
. . . . . . . . . . . . . . . . . . |
19 |
|||
Obsolete |
|
|
|
2/20
AN2326 |
Calibration software |
|
|
The software algorithm, described in the following flowchart (see Figure 3.), uses the mains frequency as a timebase. This timebase allows the microcontroller to test if the RC oscillator frequency is above or below the targeted frequency and repeatedly transforms it by dichotomous analysis so that in 10 iterations the RCCR/SICSR registers are set to the optimum value. In order to simplify the calculation, the calibration is done at 1MHz, the 8MHz internal RC is divided by 8 thanks to the AVTCHR register.
As the timer speed depends on the RC oscillator frequency, it is easy to determine if the oscillator is too fast or too slow. The counted value can be obtained by the following equation:
|
fcpu |
countedvalue = ---------------------------- |
|
32 |
× fmains |
Since the frequency of the counter is the frequency of the oscillator divided by 32 (only the 8 MSbits are saved on the 13 bits of the counter), if the oscillator is at 1 MHz, the result of the count between two edges (which have a 10 ms interval), is 138h for the European standard (220V/50Hz). For the US standard (110V/60Hz) the right value is 104h. Since the goal of the
software is to set the RC oscillator frequency to 1 MHz it means obtaining 138h as the result |
||||||||||
of the count. So if the result of the count is greater than 138h, it means that the frequency is |
||||||||||
too high so the program increases the value of RCCR/SICSR registers in order to decrease |
||||||||||
|
|
|
|
|
|
|
Product(s) |
|||
the RC oscillator frequency. And if the result is less than 138h, the RCCR/SICSR register |
||||||||||
value is decreased in order to increase the RC oscillator frequency. |
|
|
||||||||
Figure 1. Dichotomous analysis of RCCR value |
|
|
||||||||
|
|
increase oscillator |
|
decrease oscillator |
|
|
||||
|
|
|
frequency |
|
|
frequency |
|
|
||
RCCR Register |
|
Obsolete |
|
|
|
|||||
|
|
|
|
|
|
|
|
|
|
|
|
- |
|
|
|
|
|
|
|||
0h |
|
80h |
FFh |
|||||||
|
|
|
Start Value |
|
|
The RCCRProduct(s)register is set to 80h initially by the program, then the dichotomization starts by adding or subtracting 40h and after each iteration the result is divided by two, so that after
10 iterations the value of RCCR is set with an accuracy of one bit. Obsolete
3/20
Calibration software |
AN2326 |
|
|
Figure 2. Using the timer input capture to measure the mains frequency
|
Mains |
Overflow |
|
F9h |
|
|
Free-running |
|
Counter |
0h |
|
Capture 1 |
Capture 2 |
To measure the frequency, the software uses the Lite Timer input capture (LTIC) so that on each edge of the mains the value of the free running counter is stored as shown in Figure 2.. Then the microcontroller calculates the elapsed time between the two edges of the mains. This time is given by the following equation:
|
time |
Product(s) |
|
= nbover × F9h + capture2 – capture1 |
|
|
where nbover represents the number of counter overflows during the measurement, capture |
|
|
1 and capture 2 are the values captured on the free running counter when an edge occurs |
|
|
on the mains and F9h is the overflow value of the free running counter. |
|
|
If the RC oscillator frequency is equal to 1 MHz, the result time will be 138h for European |
|
|
|
Obsolete |
|
standard (220V/50Hz) or 104h for US standard mains (110V/60Hz), so these are the |
|
|
reference values. |
|
|
This measurement result is compared to the reference value and, depending on the result of |
|
|
the comparison, the microcontroller adds to or subtracts from the current RCCR/SICSR |
|
|
register values. |
- |
|
|
|
1.2 |
Product(s) |
|
Basic version |
|
|
|
In this version the measurement is done only once for each dichotomization step. This |
|
|
allows the calibration software to be light and fast. It requires only 128 bytes of program |
|
|
memory and 5 bytes of RAM during calibration. The calibration takes less than 200 ms to be |
|
|
completed. |
|
Obsolete |
|
|
|
The software works as shown in the following flowchart. The assembly code and a more |
detailed flowchart can be found in Section 4.
4/20
AN2326 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Calibration software |
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Figure 3. |
Basic software flowchart |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
Initialization of Lite Timer |
|
|
|||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Measurement and |
|
|
||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
calculation |
|
|
|||||||
|
|
|
|
|
|
smaller |
|
|
|
|
Compare |
|
|
|
|
|
|||||||
|
|
|
|
|
|
|
|
|
|
result with |
|
||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reference |
|
||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
greater or equal |
|
|||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
Decrease RCCR |
|
|
|
|
|
Increase RCCR |
|||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
no |
|
|
|
|
|
|
|
|
|
|
|
Product(s) |
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||
|
|
|
|
|
|
|
Dichotomization |
|
|||||||||||||||
|
|
|
|
|
|
|
|
||||||||||||||||
|
|
|
|
|
|
|
|
finished? |
|
|
|
|
|
|
|
||||||||
|
|
|
|
|
|
|
|
|
|
|
|
yes |
|
|
|
|
|
|
|||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||
|
|
|
|
|
|
|
Clock is set to 1 MHz |
|
|||||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This version uses the method described inObsoleteSection 1.1 except it performs four measurements and uses their average for each dichotomization step. It is useful when the mains is noisy. For instance, when a
motor starts it generates a tension pick-and this can be considered as a mains edge.
pleted.
This versionProduct(s)is safer than the basic one but it requires more resources. It uses 136 bytes of program memory and 11 bytes of RAM during calibration. The calibration takes less than 800 ms to be com-
The average version works as shown in the following flowchart. The assembly code can be found in Section 4.
Obsolete
5/20
Calibration software |
AN2326 |
|
|
Figure 4. Average software flowchart
Initialization of Lite Timer
4 measurements and calculation
Average
smaller |
Compare |
|
result with |
||
|
||
|
reference |
|
|
|
|
|
|
finished? |
|
Product(s) |
||
|
|
|
|
|
|
|
|
|
greater or equal |
|
|
|
|
Decrease RCCR |
|
|
Increase RCCR |
|
|||
|
|
|
no |
|
|
|
||||
|
|
|
|
|
Dichotomization |
|
|
|||
|
|
|
|
|
|
|
||||
|
|
|
Obsolete |
|||||||
|
|
|
|
|
|
|
yes |
|
|
|
|
|
|
|
Clock is set to 1 MHz |
|
|
||||
|
|
|
|
|
|
|
|
|
||
- |
|
|
|
|
|
|
|
|||
|
Product(s) |
|
|
|
||||||
Obsolete |
|
|
|
6/20