ST AN1530 Application note

AN1530
APPLICATION NOTE
ACCURATE TIMEBASE FOR LOW-COST
ST7 APPLICATIONS WITH INTERNAL RC OSCILLATOR
by Microcontroller Division Applications
INTRODUCTION
The ST7 microcontroller con tains an internal RC oscillator, which may vary due to internal component variation depending upon the surrounding conditions. This can lead to the wrong calculation of the timing for the different peripherals like Timers, SCI etc. Timing calculation for the peripheral can be done based on the internal RC frequency given in the datasheet, but due to the variations in internal RC frequency from component to component, this leads to wrong timing results in the application.
The purpose of this application note is to present a software solution for accurate timing by cal­ibrating the inter na l perip heral pa ram eters ag ain st the v ariation of th e i nterna l RC osc illator. This note focuses on ST7 MCUs with a non-calibrated RC. A 50 H z, 5 V source is applied as a reference frequency to find the real internal RC oscillator frequency. The deviation of the in­ternal frequency w ith respect to the data sheet frequ ency is applied to c orrect the peripher al parameter values to obtain timing accuracy. This solution provides an innovative way to com­pensate the internal RC oscillator variation and to develop low cost applications.
Software is developed with using the ST7 software library (available free on the ST web site). Software shows , h ow t o cal ibrate P WM signal o f TIME R A to produ ce a 5 K Hz fr equen cy. It also gives an idea of how to calibrate the baud rate of the SCI (here it is calibrated for 9600 baud). An externally calibrated source (ex. Function generator) is used to apply 50 Hz and 5 V as a reference.
AN1530/0702 1/12
1
ACCURATE TIMEBASE FOR LOW-COST ST7 APPLICATIONS WITH INTERNAL RC...
1 SOFTWARE SOL UTION
1.1 MEASUREMENT PRINCIPLE
To measure the frequency of the internal RC, software uses the input capture pin of 16-bit TIMER A. A calibrated sour ce is applied with 50Hz on the capture pin. Every falling ed ge on this pin generates an interrupt (at 20ms time duration).
Softwar e has take n the int erna l RC fre que nc y o f t ypi cally 4 M Hz a s p er t he d atash eet . The TIMER clo ck is selec ted as fc pu/2 (1 MH z). Co unter over flow is gen erated after 65.532 ms (0xFFFF). This internal RC frequency should be chosen between the datasheet. max. and min. RC frequency values. Capture1 and Capture2 are stored as shown in Figure 1.
Figure 1. Timer Input Capture to measure the Reference frequency.
Calibrated source for 5 Vpp, 50 Hz
FFFFh
Free running
counter of TIMER A
0000h
Here two possibilities can occur during the calculation of the capture elapsed time. If Capture1 and Capture2 are on the single counter (as shown in Figure 1), time is calculated simply by
Capture Counter = Capture2 - Capture1
Second possibility is that counter overflow generates between the measurement of the Capture1 and Capture2. This leads to a more complex calculation given by
Capture Counter= ((0xffff - capture1) + capture2)
This Measured Capture Counter is compared with the Ideal Capture Counter. Ideal Capture Counter is calculated manually and here for the fcpu = 2 MHz, it gives 0x4E20. With this infor­mation Internal RC deviation is found.
Capture1 Capture2
2/12
2
ACCURATE TIMEBASE FOR LOW-COST ST7 APPLICATIONS WITH INTERNAL RC...
InternalRCDeviationFraction
MeasuredCaptureCounter
-------------------------------------------------------------------------------=
CalculatedCaptureCounter
This fraction can be used to calibrate the other peripheral parameters to add precision to the the timing calculations.
If a Mains Supply is used as an external source, this solution works for an European Supply (50 Hz)). If a US supply (60 Hz) is used, the calculated Capture Counter will be 0x411B to find the fraction for internal RC deviation.
A software routine is developed to find this fraction and utilize it to calibrate the peripheral pa­rameters. this routine is included in the application. The basic software takes less than 30ms to find the fraction, which gives the deviation of r eal RC frequenc y with referen ce to the da­tasheet frequency. This application incl udes a glitch filter algorithm and also takes the average of 8 samples for finding the fraction. The basic algorithm and the average + glitch algorithms are descr ibed in th e next c hap ters. T he sof tware t ak es l ess than 325 ms wi th t he av era ging and glitch algorithms.
This routine ex ecut ion t ime inc ludes the 8 ca pture e lapse d tim es , w aiting t im es fo r th e cap ­tures and the loop calculations i nside the routine. T he routine execution time is affected mostly by the capture waiting times. This routine will run in the background of the main application, so MCU can run other tasks during thi s 30ms (bas ic software algorithm) or 325ms (for average and glitch algorithm) period.
3/12
ACCURATE TIMEBASE FOR LOW-COST ST7 APPLICATIONS WITH INTERNAL RC...
1.2 BASIC ALGORITHM
This algorithm measures the deviation of internal RC oscillator frequency in terms of Fraction.
This will take 30ms to find the fraction. Software is developed in “C” language with the help of the ST7 software library. It needs 9 bytes of RAM.
The software works as shown in the following flow chart for a single sample.
Figure 2. Basic Algorithm Flowchart
START
Initialisation of TIMER A capture
Measurement of the Capture1 and Capture2 done
Is
counter overflow
generated
?
no
Capture Counter= Capture2 - Capture1
Internal RC deviation fraction = Measured Capture Counter / Calculated (Ideal) Capture Counter
Capture Counter= ( ( 0xffff - Capture1 ) + Capture2 )
yes
4/12
Loading...
+ 8 hidden pages