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
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
volatile memory and load it in the RCCR/SICSR registers after each reset. However, if any
of the external conditions (temperature or voltage, for instance) changes too drastically, the
stored value may no longer produce the required accuracy. One solution is to recalculate the
RCCR/SICSR register values after each reset, based on an external reference.
The purpose of this application note is to present 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).
The basic software takes less 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.
O
so
b
te
le
This application 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
input connected to the mains.
r
P
uc
od
s)
t(
bs
-O
et
l
o
P
e
od
r
s)
t(
uc
April 2006
Rev 1
1/20
www.st.com
Contents
AN2326
Contents
1
Calibration software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1
1.2
1.3
Software principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Basic version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Average version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2
Power supply and timebase delivery circuit . . . . . . . . . . . . . . . . . . . . . . 7
2.1
2.2
Basic circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Hardware protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3
4
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Software examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.1
Single alternance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.1.1
4.1.2
4.1.3
4.1.4
4.1.5
4.1.6
Main program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Input capture interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Timebase interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Writing in non volatile memory for products without Data EEPROM . . . 12
Writing in non volatile memory for products with Data EEPROM . . . . . 13
Detailed basic version software flowchart . . . . . . . . . . . . . . . . . . . . . . . 14
4.2
Average version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.2.1
4.2.2
4.2.3
5
O
so
b
te
le
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
r
P
uc
od
Main program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Input capture interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Timebase interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
s)
t(
bs
-O
et
l
o
P
e
od
r
s)
t(
uc
2/20
AN2326
Calibration software
1
1.1
Calibration software
Software principle
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:
f
cpu
-
countedvalue
= ---------------------------
32
×
f
mains
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
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
The RCCR 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.
O
so
b
te
le
r
P
uc
od
s)
t(
0h
bs
-O
et
l
o
P
e
od
r
s)
t(
uc
80h
Start Value
FFh
3/20
Calibration software
Figure 2.
Using the timer input capture to measure the mains frequency
Mains
AN2326
F9h
Overflow
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
=
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
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
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.
O
so
b
te
le
The software works as shown in the following flowchart. The assembly code and a more
detailed flowchart can be found in Section 4.
r
P
uc
od
s)
t(
bs
-O
et
l
o
P
e
od
r
s)
t(
uc
4/20
AN2326
Figure 3.
Basic software flowchart
Initialization of Lite Timer
Calibration software
Measurement and
calculation
smaller
Compare
result with
reference
greater or equal
Decrease RCCR
Increase RCCR
no
Dichotomization
finished?
yes
Clock is set to 1 MHz
1.3
Average version
This version uses the method described in Section 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.
This version 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-
pleted.
The average version works as shown in the following flowchart. The assembly code can be found in
Section 4.
O
so
b
te
le
r
P
uc
od
s)
t(
bs
-O
et
l
o
P
e
od
r
s)
t(
uc
5/20