ST AN1324 Application note

AN1324

APPLICATION NOTE

CALIBRATING THE RC OSCILLATOR OF THE

ST7FLITE0 MCU USING THE MAINS

INTRODUCTION

The ST7FLITE0 microcontroller contains an internal RC oscillator which can be trimmed to a specific frequency with an accuracy of 1%. The oscillator frequency has to be calibrated by software using the RCCR (RC Control Register). The value entered in the RCCR will switch 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 predefined calibration values stored in memory (refer to section 7.1 in the ST7FLITE0 datasheet) You can load one of these values in the RCCR if one of the operating conditions matches that in your application. Otherwise, you can define your own value, store it in EEPROM or any nonvolatile memory and load it in the RCCR register after each reset. However, if any of the external conditions (temperature or voltage, for instance) change too drastically, the stored 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 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 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 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.

Rev. 2

AN1324/0604

1/15

1

CALIBRATING THE RC OSCILLATOR OF THE ST7FLITE0 MCU USING THE MAINS

1 CALIBRATION SOFTWARE

1.1 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 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 oscillator is too fast or too slow. The counted value can be obtained by the following equation:

countedvalue

=

fcpu

× -----fm-----ai----ns

 

32

Since the frequency of the counter is the frequency of the oscillator divided by 32, 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 in order to decrease the RC oscillator frequency. And if the result is less than 138h, the RCCR 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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0h

80h

FFh

 

 

Start Value

 

 

 

 

 

 

 

 

 

 

 

 

 

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 7 iterations the value of RCCR is set with an accuracy of one bit.

2/15

2

ST AN1324 Application note

CALIBRATING THE RC OSCILLATOR OF THE ST7FLITE0 MCU USING THE MAINS

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 = 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 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 light and fast. It requires only 90 bytes of program memory 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 detailed flowchart can be found in Section 4.

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

Dichotomization finished?

yes

Clock is set to 1 MHz

4/15

CALIBRATING THE RC OSCILLATOR OF THE ST7FLITE0 MCU USING THE MAINS

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 560 ms to be completed.

The average version works as shown in the following flowchart. The assembly code can be found in Section 4.

Figure 4. Average software flowchart

Initialization of Lite Timer

4 measurements and calculation

Average

 

 

 

 

 

 

 

 

 

 

 

 

smaller

Compare

 

 

result with

 

 

 

 

 

 

 

 

 

 

reference

 

 

 

 

 

 

 

 

greater or equal

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Decrease RCCR

 

 

Increase RCCR

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

no

Dichotomization finished?

yes

Clock is set to 1 MHz

5/15

Loading...
+ 10 hidden pages