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 ST7ULTRALITEto 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.
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.
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
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.
----------------------------=
32 f
×
mains
Figure 1.Dichotomous analysis of RCCR value
increase oscillator
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.
Figure 2.Using the timer input capture to measure the mains frequency
Mains
F9h
0h
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:
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.
Overflow
Free-running
Counter
Capture 1Capture 2
timenbover F9h capture2 capture1–+×=
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.
The software works as shown in the following flowchart. The assembly code and a more
detailed flowchart can be found in Section 4.
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 completed.
The average version works as shown in the following flowchart. The assembly code can be found in
Section 4.