ST AN1324 Application note

AN1324
APPLICATION NOTE
CALIBR ATING THE RC OSCIL LATOR OF THE
ST7F LITE 0 M C U US ING T HE MAI NS

INTRODUCTION

The ST7FLITE0 microcontroller contains an internal RC oscillator which can be trimmed to a specific freq uency wi th an accura cy of 1%. The oscilla tor frequen cy has to be calibrated by softwa re u sing the R CCR ( RC Con trol Regi ster ). T he val ue e nte red in t he RCCR will swi tch on a corresponding number of resistors that will modify the oscillator frequency. Whenever the ST7FLITE0 microcontroller is reset, the RCCR is restored to its default value (FFh), so each time the device is reset, you have to load the calibration value in the RCCR. There are prede­fined calibration values stored in memory (refer to section 7.1 in the ST7FLITE0 datasheet) You can loa d on e o f th ese v alu es i n th e RCCR if on e of t he op erating c on dit ions match es t h at in your application. Otherwise, you can define your own value, store it in EEPROM or any non­volatile memory and load it in the RCCR register after each reset. However, if any of the ex­ternal conditions (temperature or voltage, for instance) change too drastically, the s tored value may no longer produce the required 1% accuracy. One solution is to recalculate the RCCR value after each reset, based on an external reference.
The purpose of this app lication no te is to pres ent a sof tware s olution using the freque ncy o f the European standard mains (220V/50Hz) as a timebase to adj ust the internal RC oscillator of the ST7FLITE0 to 1 MHz (1%). The same approach can also be used for the US mains standard (110V/60Hz).
The basic software takes less than 160 ms to calibrate the oscillator and uses less than 90 bytes of progra m memo ry and five byte s of RAM for it s simplest vers ion. The se RAM bytes can be freed for other pur poses when the calibration i s 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 con­nected to the mains.
Rev. 2
AN1324/0604 1/15
1
CALIBRATING THE RC OSCILLATOR OF THE ST7FLITE0 MCU USING THE MAINS

1 CALIBRA TION SOFTWARE

1.1 SOFTWARE PR INCIPLE

The softwar e algorith m, descri bed in the fo llowing fl owcha rt (see Figu re 3), uses the mains frequency as a timebase. This timebase allows the m icrocontr oller to test i f the RC os cillator frequency is above or below 1 MHz and repeatedly transforms it by dichotomous analysis so that in 7 iterations the RCCR is set to the optimum value.
As the timer speed depends on the RC oscillator frequency, it is easy to determine if the oscil ­lator is too fast or too slow. The counted value can be obtained by the following equation:
f
count edval ue
Since the frequency of the counter is the frequency of the oscillator divided by 32, if the oscil­lator 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 obtainin g 13 8h as the result of the c oun t. S o if the resu lt o f the c ount is gr eater t han 138h, it means that the frequency is too high so the program increases the value of RCCR in order to decrease the RC oscillator frequency. And if th e result is less than 138h, the RCCR is decreased in order to increase the RC oscillator frequency.
Figure 1. Dichotomous Analysis of RCCR Value
-------------------------- -=
32 f
×
cpu
mains
increase oscillator
frequency
decrease oscillator
frequency
RCCR Register
80h
FFh0h
Start Value
The RCCR register is set to 80h initially by the program, then the dichotomization starts by adding or subtracting 40h and after eac h iterati on the result is di vided by two, so that after 7 it ­erations the value of RCCR is set with an accuracy of one bit.
2/15
2
CALIBRATING THE RC OSCILLATOR OF THE ST7FLITE0 MCU USING THE MAINS
Figure 2. Using the Timer Input Capture to Measure the Mains Frequency
Mains
F9h
Overflow
Free-running Counter
0h
Capture 1 Capture 2
To measure the frequency, the softwar e 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 frequ ency 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 refer­ence 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 value.
3/15
CALIBRATING THE RC OSCILLATOR OF THE ST7FLITE0 MCU USING THE MAINS

1.2 BASIC VERSION

In this version the measurement is done only once for each dichotomization step. This allows the calibration software to be l ight and fast. It requires only 90 bytes of program mem o ry and 5 bytes of RAM during calibration. The calibration takes less than 160 ms to be completed
The software works as shown in the following flowchart. The assembly code and a more de­tailed flowchart can be found in Section 4.
Figure 3. Basic software flowchart
Initialization of Lite Timer
Measurement and
calculation
smaller
no
Dichotomization
finished?
Clock is set to 1 MHz
Compare result with
reference
greater or equal
Increase RCCRDecrease RCCR
yes
4/15
CALIBRATING THE RC OSCILLATOR OF THE ST7FLITE0 MCU USING THE MAINS

1.3 AVERAGE VERS ION

This version uses the method described in Section 1.1 except it performs four measurements and uses their average for each di chotomization step. It i s useful w hen the mains is noi sy. F or 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 res ources. It uses 13 6 bytes of program memory and 11 bytes of RAM during calibration. The calibration takes less than 560 ms to be completed.
The average version w orks as shown in the following flowc hart. The assem bly code can be found in Section 4.
Figure 4. Average software flowchart
Initialization of Lite Timer
no
smaller
Dichotomization
finished?
yes
4 measurements and
calculation
Average
Compare result with
reference
greater or equal
Increase RCCRDecrease RCCR
Clock is set to 1 MHz
5/15
Loading...
+ 10 hidden pages