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.
The following figures show circuits which will provide 5V DC to the ST7ULTRALITE and
protect the input capture from overcurrent. If no power supply is needed, the only
component to keep is the resistor on the LTIC input, which is mainly to protect from
overcurrent.
2.1 Basic circuit
This circuit contains a capacitive power supply which converts the 220V/50Hz of the mains,
as well as the 110V/60Hz of the US mains, into 5V DC.
Warning:be aware that this kind of power supply cannot be used if there are
big current variations.
It also inputs 220V/50Hz to the Lite Timer Input Capture pin (LTIC/PA2) protected by resistor
R2.
The incoming alternating signal on the LTIC input pin is 220V/50Hz. Because of the
clamping diode on the input of the ST7ULTRALITE, the input signal can be considered as a
0-5V square signal.
Figure 5.Power supply and timebase delivery circuit diagram
V
DD
C1
220µF/16V
ST7ULTRALITE
GND
LTIC
Mains
(220V/50Hz)
R1 47R/0.5W
zener
5.6V
1N4148
C2
C2
220nF/400V
R2 470k/0.5W
The maximum current available in the microcontroller depends on the C2 value. Table 1
gives the maximum average current versus the capacitor value. The average current follows
the equation below:
ImaxVmax 2 f C⋅⋅⋅=
In the case above, C2 is equal to 220nF so the available current is limited to 4.9 mA in the
case of a European mains. To have the same current levels in the case of the US mains
(110V/60Hz), C2 must be multiplied by two. A 440nF capacitor will limit the current to 4.9
mA.
For the US standard, R2 must be divided by two in order not to limit the current too much on
the LTIC input. A 220kΩ resistor is enough in this case.
Table 1.Maximum MCU current
Capacitor C2Maximum Current
220nF4.9mA
330nF7.3mA
470nF10.4mA
680nF15mA
1µF22.1mA
2.2 Hardware protection
To prevent bad measurements due to noisy mains, a filter can be added between the mains
and the input capture of the ST7ULTRALITE. The following figure shows one example of a
filter. This filter is a pass band centered on the mains frequency in order to reject all
frequency which could be understood by the microcontroller as a mains edge.
Please note that this is just a second order filter and that this may not be enough if the mains
is really noisy. Any kind of filter can be added on the LTIC.
Figure 6.Band pass filter
mains
C1
470nF/400V
The pass band filter above must be tuned to mains frequency. The value of the resistors for
this filter is given in the table below.
This system allows you to have a power supply for the microcontroller and an auto
adjustable clock set to 1, 2, 4 or 8MHz, selected by prescaler, with the required accuracy
whatever the external conditions.
This solution also offers the advantage of being less expensive than a solution with a
transformer and requires less space.
It requires a small amount of space in program memory (less than 128 bytes) in its smallest
version.
A zip file attached to this application note contains the complete software of this calibration
method.
4.1 Single alternance
This version performs only one count between two edges and changes the value of the
RCCR/SICSR registers according to this measurement. This can lead to bad tuning if there
is noise on the reference signal.
4.1.1 Main program
;All the bytes from locations 80h to 85h are used by this software to store values or as control
registers but they can be reused safely after the clock has been set.
;dichotomy value
.valueequ$81;this byte contains the value which will be added or
subtracted to/from the RCCR last value at the end of each round
;capture values
.capture1equ$82
.capture2equ$83;these two bytes contain the two values of the counter captured on
the edge of the mains, they are used to calculate the time elapsed between the two edges
;number of overflows
.nboverequ$84;this byte contains the number of counter overflows during the
measurement
;control register
.crequ$85;this byte is used as a control register for the measurement. Its
bits allow or not the interrupts and show which step of the count is the current one.
.strtstpequ1;this is set to start the count and reset to stop it
.lsb_RCCRequ7;this bit is set when the first capture has occurred. It allows
the overflows to be counted
;address to program in NVM (optional)
.E2ADDR equ $86
.RAM equ $88
.main
ld A, #$63
ld AVDTHCR, A ; set internal clock to fRC/8 meaning 1MHz targeted
clr SICSR ;
ldA, #$80 ;value containing the value which will be
ldvalue, A;add or subs to/from RCCR during the dichotomy
ldRCCR, A;RCCR is set to the middle of its range of value
clrcr;clear the byte use as control register for the count
next
clr nbover;clear the byte containing the number of timer overflow
ldA, LTICR;clear the ICF bit
count
btjt cr, #strtstp, count; wait for the end of count
rim;interrupts enable
bsetLTCSR,
bsetcr,#strtstp;set the start-stop bit of cr: count can start
clrLTCSR;lite timer interrupts disable
srlvalue;dichotomy value divided by 2
ldA,#$F9;these lines calculate this equation:
ldX,nbover;
mulX,A;(nbover*$F9)+ capture2 - capture1
addA, capture2;
jrncnocarry;this equation is calculated with 16 bits
incX;
clear the 2 lowest significant bits located in this register
noneg
cpX, #$01;if mains frequency is 50Hz the reference value is $138
compare cpA, #$38;value is smaller than the reference the program jump to
plus
plus_lsb
btjt value, #5, p_lsb0
btjt value, #4, end
ld A, value
ld SICSR, A ;add value if counted value is greater than ref
jra next
p_lsb0
minus
btjt cr,#lsb_RCCR,minus_lsb
ldA, RCCR
jra new
minus_lsb
btjt value,#5, m_lsb0
btjt value, #4, clr_lsb0
bres RCCR,#0 ;reset the LSb of RCCRH
ld A, value
ld SICSR, A ;substract value if counted value is smaller tha n ref
jra next
m_lsb0
clr_lsb0
new
bset cr, #lsb_RCCR
ld A, #$80
ld value, A
jra next
.end_calibration ; end of the calibration
; at this point the Fcpu is set to 1MHz
; the last operation is to set the clock to the targeted value by setting the prescaler register
jrncnoneg;
decX;and LSB in register A.
jrmiminus;if it is 60Hz the reference is $104.the program first
jreqcompare;compares MSB with $01 and then compare LSB with
jpplus;$38 for 50Hz and $04 for 60Hz. if the calculated
jrmiminus;minus to decrease RCCR else it increase RCCR
btjt cr,#lsb_RCCR,plus_lsb
ldA, RCCR
addA, value;add value if counted value is greater than ref
jranew
bset SICSR,#5;add value if counted value is greater than ref
jra next
subA, value ;subtract value if Y
bres SICSR,#6
bset SICSR,#5 ;subtract value if Y is smaller
jra next
bres SICSR,#5 ;reset the LSb of RCCRL
jra end
ldRCCR,A;enter the new value in RCCR
btjfvalue, #0,
ld A, #$03; set clock to fRC meaning 8MHz
; ld A, #$23 ; set prescaler to fRC/2
; ld A, #$43 ; set prescaler
; ld A, #$63 ; set prescaler to fRC/8
ld AVDTHCR,A; in the three cases AVD is set off
next;stop after 7 rounds
is smaller
to fRC/4
4.1.2 Input capture interrupt
ldA, LTICR;load captured value in A
btjtLTCSR, #4, finish;test if it is first or second capture
overflows
finish ldcapture2, A;if it is the second capture, captured value is stored in
capture2
bsetLTCSR, #4;allow timebase interrupt in order to count the number of
ldcapture1, A;captured value is stored in capture1
jpendit1
4.1.4 Writing in non volatile memory for products without Data EEPROM
To store the final value of RCCR/SICSR registers in non volatile memory, the following lines must be
added after the timer interrupts have been disabled in the main program. As Data EEPROM is not embedded in the ST7ULTRALITE, the storage is done in program memory, so the sector 0 size must be
0.5k (selected by option byte). The programmation routine must be executed from RAM, so it is first
loaded in RAM and then called. Both RCCR and SICSR registers are saved.
; Content of RCCR and SICSR is saved at FC00h and FC01h
; < RESET_FCSR >
LD A,#$56 ; Enter RASS keys to unlock FCSR register
LD FCSR,A
LD A,#$AE
LD FCSR,A
LD A,#$FC ; High address
LD X,A ;
clr Y ; define FC00 as destination address
LD {E2ADDR},X ; Address high to be programmed (0081h) is in X
LD {E2ADDR+1},Y ; Address low to be programmed (0082h) is in Y
CALL RAM ; Call the programming driver located into RAM
JP USER_APPLICATION
; ----------------------------------------------------------------------------; ROUTINE: XemulE2_ByteProg
; DESCRIPTION: Emulated data EEPROM byte
; BEFORE: A = data to be programmmed
; X:Y = address where it has to be programmed [FC00h..FDFFh]
; -----------------------------------------------------------------------------
.EEPROM_Prog
BTJT FCSR,#PGM,EEPROM_Prog; Wait end of programming (~5ms)
RET
4.1.5 Writing in non volatile memory for products with Data EEPROM
To store final value of RCCR/SICSR registers in EEPROM, add theses lines after disabling the timer interrupts in the main program.
ldRCCR, A
bsetEECSR,#1;start to enter value in the EEPROM
ld$1003,A;load value of the RCCR in EEPROM
ldA, SICSR
ld$1004,A;load value of the SICSR in EEPROM
waitbtjtEECSR,#0,wait;wait for the end of writing in EEPROM
This version performs the count between two edges four times and changes the value of the
RCCR/SICSR registers according to the average of these measurements. This method
allows to perform a better tune of the RC oscillator especially in noisy environment.
4.2.1 Main program
;All the bytes from locations 80h to 8Bh are used by this software to store values or as control
registers but they can be reused safely after the clock has been set.
;dichotomy value
.valueequ$81;this byte contains the value which will be added or
subtracted to/from the RCCR last value at the end of each round
;capture values
.capture1equ$82
.capture2equ$86;these bytes contain the values of the counter captured on the
edge of the mains, they are used to calculate the time elapsed between the two edges
;number of overflows
.nboverequ$8A;this byte contains the number of counter overflows during the
measurement
;control
.crequ$8B;this byte is used as a control register for the measurement. Its
bits allow or not the interrupts and show which step of the count is the current one.
.strtstpequ1;this is set to start the count and reset to stop it
.lsb_RCCRequ7;this bit is set when the first capture has occurred. It allows
the overflows to be counted
;address to program in NVM (optional)
.E2ADDRequ $86
.RAM equ $88
.main
next
clr nbover;clear the byte containing the number of timer overflow
ldA, LTICR;clear the ICF bit
capture
rim;interrupts enable
count
calcul
add A, (capture2,Y);
nocarry
sub A, (capture1,Y) ;MSB are in register X
noneg incY
register
ld A, #$63
ld AVDTHCR, A ; set internal clock to fRC/8 meaning 1MHz targeted
ldA, #$80 ;value containing the value which will
ldvalue, A;add or subs to/from RCCR during the dichotomy
ldRCCR, A;RCCR is set to the middle of its range of value
clrcr;clear the byte use as control register for the count
clrY
bsetLTCSR, #7;enable input capture interrupt
bsetcr,#strtstp;set the start-stop bit of cr: count can start
btjt cr, #strtstp, count; wait for the end of count
clrLTCSR;lite timer interrupts disable
incY
cpY,#4 ;repeat the capture four time to make an average
jrnecapture
srlvalue;dichotomy value divided by 2
clrY
ldA,#$F9;these lines calculate this equation for the four
ldX,nbover;measures:
mulX,A ;(nbover*$F9)+ capture2 - capture1
jrncnocarry;this equation is calculated with 16 bits
incX;
srlA ;these lines calculate the average of the last four
srlX ;measures by dividing their total by 4. It is done by
jrnccarry1;two consecutive right shift on the 16 bit result.
carry1
srl A
carry2
cpX, #$01;if mains frequency is 50Hz the reference value is $138
compare
cpA, #$38;value is smaller than the reference the program jump to
plus
plus_lsb
btjt value, #5, p_lsb0
btjt value, #4, end
ld A, value
ld SICSR, A ;add value if counted value is greater than ref
jra next
p_lsb0
minus
btjt cr,#lsb_RCCR,minus_lsb
ldA, RCCR
jra new
minus_lsb
btjt value,#5, m_lsb0
btjt value, #4, clr_lsb0
bres RCCR,#0 ;reset the LSb of RCCRH
ld A, value
ld SICSR, A ;substract value if counted value is smaller
jra next
m_lsb0
clr_lsb0
newldRCCR,A;enter the new value in RCCR
next_near
.end_calibration ; end of the calibration
; at this point the Fcpu is set to 1MHz
; the last operation is to set the clock to
addA,#$80
srlX
jrnccarry2
addA,#$80
jrmiminus ;if it is 60Hz the reference is $104.the program first
jreqcompare;compares MSB with $01 and then compare LSB with
jpplus;$38 for 50Hz and $04 for 60Hz. if the calculated
jrmiminus ;minus to decrease RCCR else it increase RCCR
btjt cr,#lsb_RCCR,plus_lsb
ldA, RCCR
addA, value;add value if counted value is greater than ref
jranew
bset SICSR,#5;add value if counted value is greater than ref
jra next
subA, value ;subtract value if Y is smaller
than ref
bres SICSR,#6
bset SICSR,#5 ;subtract value if Y is smaller
jp next
bres SICSR,#5 ;reset the LSb of RCCRL
jra end
btjfvalue, #0, next_near;stop after 7 rounds
bset cr, #lsb_RCCR
ld A, #$80
ld value, A
jp next
ld A, #$03; set clock to fRC meaning 8MHz
; ld A, #$23 ; set prescaler to fRC/2
; ld A, #$43 ; set prescaler to fRC/4
; ld A, #$63 ; set prescaler to fRC/8
ld AVDTHCR,A; in the three cases AVD is set off
the targeted value by setting the prescaler register
4.2.2 Input capture interrupt
ldA, LTICR;load captured value in A
btjtLTCSR, #4, finish;test if it is first or second capture
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the
right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any
time, without notice.
All ST products are sold pursuant to ST’s terms and conditions of sale.
Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no
liability whatsoever relating to the choice, selection or use of the ST products and services described herein.
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this
document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products
or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such
third party products or services or any intellectual property contained therein.
UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED
WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS
OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZE REPRESENTATIVE OF ST, ST PRODUCTS ARE NOT DESIGNED,
AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING APPLICATIONS,
NOR IN PRODUCTS OR SYSTEMS, WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY, DEATH, OR
SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE.
Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void
any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any
liability of ST.
ST and the ST logo are trademarks or registered trademarks of ST in various countries.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.