About ST
Products
Applications
Support
Buy
News & Events
ST Worldwide
Contact Us
Login
Application Notes
|
Industrial
|
Lighting Control
|
8-bit Microcontrollers
|
ST7 - 8-bit Microcontrollers
Calibrating the RC oscillator of the ST7FLITE0 MCU using the mains
Application Note
Format:
(131 kb)
or
(20 kb)
Last Updated: 28/02/2008
Pages: 15
Related Datasheets
8-bit microcontroller with single voltage Flash memory, data EEPROM, ADC, timers, SPI
Related Information
Source file for calibrating the RC oscillator of the ST7FLITE0 MCU using the mains
Raw Ascii Text
- (
Hide
)
(Unformatted textual content of the document used by search engines)
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 sp ecific 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. Th e 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: f pu c o u n t e d v a l u e = -----------c-------------32 × f m a i n s 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 13 8 h 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 fr equenc y f r e q u e n cy RCCR Register 0h 80h Start Value FFh
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
CALIBRATING THE RC OSCILLATOR OF THE ST7FLITE0 MCU USING THE MAINS
Figure 2. Using the Timer Input Capture to Measure the Mains Frequency Mains
F 9h
Overflow Free-running Co unter
0h C aptur e 1 C aptur e 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: ti me = nb o v e r × F 9 h + c a p tu re 2 c a p t ur e 1 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 stan dard (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 c a lc u la tio n
s m a lle r
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. Th is 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. Th e 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 c a lc u la tio n
Average
s m a lle r
Compare result with reference
greater or equal Decrease RCCR Increase RCCR
no Dichotomization finished? yes Clock is set to 1 MHz
5/15
CALIBRATING THE RC OSCILLATOR OF THE ST7FLITE0 MCU USING THE MAINS
2 POWER SUPPLY AND TIMEBASE DELIVERY CIRCUIT
The following figures show circuits which will provide 5V DC to the ST7LITE0 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 can't be used if there are big current variations. It also inputs 220V/50Hz to the Lite Timer Input Capture pin (LTIC/PA0) protected by resistor R2. The incoming alternating signal on the LTIC input pin is 220V/50Hz. Because of the clamping diod e on the input of the ST7FLITE0, the input signal can be considered as a 0-5V square signa l. Figure 5. Power supply and timebase delivery circuit diagram V DD
ze ner 5.6V C1 220uF/16V
Mains
(220 V/50 Hz) 1N4148 C2 220nF/400V R1 47R/0.5W R2 470k/0.5W
ST7Lite0 GN D LTIC
The maximum current available in the microcontroller depends on the C2 value. Table 1 gives th e maximum average current versus the capacitor value. The average current follows the equation below: I ma x = Vma x 2 f C
6/15
CALIBRATING THE RC OSCILLATOR OF THE ST7FLITE0 MCU USING THE MAINS
In the case above, C2 is equal to 220nF so the available current is limited to 4.9 mA in the ca se 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
CA PACITOR C2 220 nF 330 nF 470 nF 680 nF 1u F MA XIMUM CURRENT 4.9mA 7.3mA 10. 4m A 15mA 22. 1m A
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 LITE0. The following figure shows one example of a filter. This filte r 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. Be aware 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 R1 R2 R3
LTIC
C2 470nF/400V
C2 470nF/250V
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. Table 2. resistors values
resistors R1 R2 R3 50Hz / 220V 6.8K/0.5W 6.8K/0.5W 470K/0.5W 60Hz/110V 5.6K/0.5W 5.6K/0.5W 220K/0.5W
7/15
CALIBRATING THE RC OSCILLATOR OF THE ST7FLITE0 MCU USING THE MAINS
3 CONCLUSION
This system allows you to have a power supply for the microcontroller and an auto adjustable clock set to 1MHz with an accuracy of 1% whatever the external conditions. Th is 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 90 bytes) in its smallest version.
8/15
CALIBRATING THE RC OSCILLATOR OF THE ST7FLITE0 MCU USING THE MAINS
4 SOFTWARE EXAMPLES
A zip file attached to this application note contains the complete software of this calibration m e th o d . 4.1 SINGLE ALTERNANCE This version perform only one count between two edges and changes the value of the RCCR acco rding to this measurement. This can lead to bad tuning if there is noise on the reference signa l. 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 .value equ $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 .capture1 equ $82 .capture2 equ $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 .nbover equ $84 ;this byte contains the number of counter overflows during the measurement ;control register .cr equ $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. .strtstp equ 1 ;this is set to start the count and reset to stop it .overflow equ 2 ;this bit is set when the first capture has occurred. It allows the overflows to be counted .main bset MCCSR, #1 not want to check the clock ld ld ld next clr clr ld rim bset bset btjt clr srl ld ld mul add A, #$80 value, A RCCR, A nbover cr A, LTICR ;output clock enable. You can remove this line if you do
;value containing the value which will be ;add or subs to/from RCCR during the dichotomy ;RCCR is set to the middle of its range of value ;clear the byte containing the number of timer overflow ;clear the byte use as control register for the count ;clear the ICF bit
count
;interrupts enable LTCSR, #7 ;enable input capture interrupt cr,#strtstp ;set the start-stop bit of cr: count can start cr, #strtstp, count; wait for the end of count LTCSR ;lite timer interrupts disable value A,#$F9 X,nbover X,A A, capture2 ;dichotomy value divided by 2 ;these lines calculate this equation: ; ;(nbover*$F9)+ capture2 - capture1 ;
9/15
CALIBRATING THE RC OSCILLATOR OF THE ST7FLITE0 MCU USING THE MAINS
jrnc inc nocarry sub jrnc dec noneg cp jrmi jreq jp compare cp jrmi plus ld add jp ld sub ld btjf loop jp
nocarry X A, capture1 noneg X X, #$01 minus compare plus A, #$38 minus A, RCCR A, value new A, RCCR A, value RCCR,A
;this equation is calculated with 16 bits ; ;MSB are in register X ; ;and LSB in register A. ;if mains frequency is 50Hz the reference value is $138 ;if it is 60Hz the reference is $104.the program first ;compares MSB with $01 and then compare LSB with ;$38 for 50Hz and $04 for 60Hz. if the calculated ;value is smaller than the reference the program jump to ;minus to decrease RCCR else it increase RCCR
;add value if counted value is greater than ref
minus
;subtract value if Y is smaller ;enter the new value in RCCR
new
value, #0, next;stop after 7 rounds loop
4.1.2 input capture interrupt
ld btjt bset number of overflows ld jp finish ld stored in capture2 clr endit1 iret A, LTICR LTCSR, #4, finish LTCSR, #4 capture1, A endit1 capture2, A cr ;load captured value in A ;test if it is first or second capture ;allow timebase interrupt in order to count the ;captured value is stored in capture1
;if it is the second capture, captured value is ;clear cr to end the count
4.1.3 timebase interrupt
ld inc endit2 iret A, LTCSR nbover ;clear TB bit ;increment number of overflows
4.1.4 writing in eeprom To store final value of RCCR in EEPROM, add theses lines after disabling the timer interrupts in the main program.
ld bset ld bset btjt RCCR, A EECSR,#1 $1003,A EECSR,#0 EECSR,#0,wait ;start to enter value in the EEPROM ;load value of the RCCR in EEPROM ;start to write in the EEPROM ;wait for the end of writing in EEPROM
wait
10/15
CALIBRATING THE RC OSCILLATOR OF THE ST7FLITE0 MCU USING THE MAINS
4.1.5 Detailed basic version software flowchart
STORE STARTING VALUE 80H --> VALUE 80H --> RCCR
(NBOVER, CR) ENABLE INPUT CAPTURE INTERRUPT
CLEAR RAM BYTES
EDGE ON LTIC INPUT
NO
YES STORE CAPTURED VALUE IN CAPTURE 1 ENABLE TIMEBASE INTERRUPT TO START TO COUNT TIMER OVERFLOWS
EDGE ON LTIC INPUT
NO
YES STORE CAPTURED VALUE IN CAPTURE 2 DISABLE TIMEBASE AND INPUT CAPTURE IN T E R R U P T S DIVIDE VALUE BY 2 CALCULATION OF: NBOVER X F9 + CAPTURE 2 - CAPTURE 1
GREATER
COMPARE RESULT WITH 138H
SMALLER ADD VALUE TO RCCR SUBTRACT VALUE TO RCCR
NO
IS THE DICHOTOMY FINISHED?
YES RC OSCILLATOR IS TRIMMED TO 1 MHZ
11/15
CALIBRATING THE RC OSCILLATOR OF THE ST7FLITE0 MCU USING THE MAINS
4.2 AVERAGE VERSION Th is version perform the count between two edges four times and changes the value of the RCC R according to the average of these measurements. This method allows to perform a better tune of the RC oscillator. 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 .value equ $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 .capture1 equ $82 .capture2 equ $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 .nbover equ $8A ;this byte contains the number of counter overflows during the measurement ;control register .cr equ $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. .strtstp equ 1 ;this is set to start the count and reset to stop it .overflow equ 2 ;this bit is set when the first capture has occurred. It allows the overflows to be counted .main bset MCCSR, #1 not want to check the clock frequency ld ld ld next clr clr ld A, #$80 value, A RCCR, A nbover cr A, LTICR Y ;interrupts enable LTCSR, #7 ;enable input capture interrupt cr,#strtstp ;set the start-stop bit of cr: count can start cr, #strtstp, count; wait for the end of count LTCSR ;lite timer interrupts disable Y Y,#$4 ;repeat the capture four time to make an average capture value ;dichotomy value divided by 2 ;output clock enable. You can remove this line if you do
;value containing the value which will be ;add or subs to/from RCCR during the dichotomy ;RCCR is set to the middle of its range of value ;clear the byte containing the number of timer overflow ;clear the byte use as control register for the count ;clear the ICF bit
clr capture rim bset bset count btjt clr inc cp jrne srl clr ld ld mul calcul add jrnc inc
Y A,#$F9 ;these lines calculate this equation for the four X,nbover ;measures: X,A ;(nbover*$F9)+ capture2 - capture1 A, (capture2,Y); nocarry ;this equation is calculated with 16 bits X ;
12/15
CALIBRATING THE RC OSCILLATOR OF THE ST7FLITE0 MCU USING THE MAINS
nocarry sub jrnc dec noneg inc cp jrne srl srl jrnc add carry1 srl srl jrnc add carry2 cp jrmi jreq jp compare cp jrmi plus ld add jp ld sub ld btjf loop jp
A, (capture1,Y);MSB are in register X noneg ; X ;and LSB in register A. Y Y,#$4 calcul A X carry1 A,#$80 A X carry2 A,#$80 X, #$01 minus compare plus A, #$38 minus A, RCCR A, value new A, RCCR A, value RCCR,A ;these lines calculate the average of the last four ;measures by dividing their total by 4. It is done by ;two consecutive right shift on the 16 bit result.
;if mains frequency is 50Hz the reference value is $138 ;if it is 60Hz the reference is $104.the program first ;compares MSB with $01 and then compare LSB with ;$38 for 50Hz and $04 for 60Hz. if the calculated ;value is smaller than the reference the program jump to ;minus to decrease RCCR else it increase RCCR
;add value if counted value is greater than ref
minus
;subtract value if Y is smaller ;enter the new value in RCCR
new
value, #0, next;stop after 7 rounds loop
4.2.2 input capture interrupt
ld btjt bset number of overflows ld jp finish ld stored in capture2 clr endit1 iret A, LTICR LTCSR, #4, finish LTCSR, #4 capture1, A endit1 capture2, A cr ;load captured value in A ;test if it is first or second capture ;allow timebase interrupt in order to count the ;captured value is stored in capture1
;if it is the second capture, captured value is ;clear cr to end the count
4.2.3 timebase interrupt
ld inc endit2 iret A, LTCSR nbover ;clear TB bit ;increment number of overflows
13/15
CALIBRATING THE RC OSCILLATOR OF THE ST7FLITE0 MCU USING THE MAINS
4.2.4 Detailed average version software flowchart
STORE STARTING VALUE 80H --> VALUE 80H --> RCCR
(NBOVER, CR) ENABLE INPUT CAPTURE INTERRUPT
CLE AR RAM BYTES
EDGE ON LTIC INPUT
NO
YES STORE CAPTURED VALUE IN CAPTURE 1.X ENABLE TIMEBASE INTERRUPT TO START TO COUNT TIMER OVERFLOWS
EDGE ON LTIC INPUT
NO
YES STORE CAPTURED VALUE IN CAPTURE 2.X DISABLE TIMEBASE AND INPUT CAPTURE INTERRUPTS
NO
FOUR MEASUREMENTS DONE? YES
CALCULATION OF: NBOVER X F9 + 4(CAPTURE2.X - CAPTURE1.X) RESULT DIVIDED BY 4 TO AVERAGE IT DIVIDE VALUE BY 2
EQUAL OR GREATER
COMPARE RESULT WITH 138H
SMALLER ADD VALUE TO RCCR SUBTRACT VALUE TO RCCR
NO
IS THE DICHOTOMY FINISHED?
YES RC OSCILLATOR IS TRIMMED TO 1 MHZ
14/15
CALIBRATING THE RC OSCILLATOR OF THE ST7FLITE0 MCU USING THE MAINS
"THE PRESENT NOTE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH A NOTE AND/OR THE USE MADE BY CUSTOMERS OF THE INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS."
Information furnished is believed to be accurate and reliable. However, STMicroelectronics assumes no responsibility for the consequences of use of such information nor for any infringement of patents or other rights of third parties which may result from its use. No license is granted by implication or otherwise under any patent or patent rights of STMicroelectronics. Specifications mentioned in this publication are subject to change without notice. This publication supersedes and replaces all information previously supplied. STMicroelectronics products are not authorized for use as critical components in life support devices or systems without express written approval of STMicroelectronics. The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners 2004 STMicroelectronics - All rights reserved STMicroelectronics GROUP OF COMPANIES Australia Belgium - Brazil - Canada - China Czech Republic - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan Malaysia - Malta - Morocco - Singapore - Spain - Sweden - Switzerland - United Kingdom - United States www.st.com
15/15
Document Number: 8611