Tuning Real-Time-Clock (RTC) Accuracy to Compensate for Temperature Changes
Abstract: A real-time clock (RTC) is a critical component for applications that rely on analysis of data acquired in real time. RTC accuracy of better than 5ppm is essential for credible data. While several environmental factors can affect RTC accuracy, temperature fluctuation is the most common cause of RTC inaccuracy. This application note shows how to use a MAXQ3100 microcontroller to calculate a trim factor that can be used to compensate for RTC inaccuracy caused by temperature changes.
Introduction
The real-time clock (RTC) is a key component for applications relying on data in real time. An RTC, for example, is the key to determining how much energy is consumed per house in an electricity meter application. The RTC has multiple functions in a gas-flow computer application. It is a key component for measuring the production of gas; it accurately logs the time in which different events occurred within the gas-flow computer in order to compensate for those values in the end result. Many software applications that gather data in real time rely heavily on the RTC's accuracy for complex data analysis. Clearly, the RTC and its accuracy are vital components for applications relying on real-time values.
Most standard applications relying on an RTC require accuracy of better than 5ppm (parts per million). The RTC accuracy is directly dependent on the frequency of its crystal time base. Environmental changes in temperature, humidity, pressure, and vibration reduce the accuracy of the crystal's resonant frequency, but among these factors temperature fluctuation is the most critical and influential element.
How can the accuracy of an RTC be measured and tuned to compensate for environmental temperature changes? This application note demonstrates how to use the MAXQ3100 microcontroller and its on-chip temperature sensor to calculate a trim factor used in a digital trim function to compensate for fluctuating RTC accuracy from temperature changes.
Measuring RTC Inaccuracy over Temperature
The MAXQ3100 uses a tuning-fork crystal with a nominal frequency of 32.768kHz. As stated above, environmental temperature changes can affect the resonance frequency of the MAXQ3100's crystal. A frequency counter was used to measure and record how long it took the microcontroller to generate a 1Hz output ranging from -40°C and +80°C (Figure 1). Data show that the crystal frequency has a parabolic relationship to temperature (Figure 2). The more the temperature deviates from the ideal +23°C (room temperature), the more the crystal frequency slows, which in turn causes the RTC to run slower.
Figure 1. Data for a 1Hz output on the MAXQ3100 were collected for the temperature range of -40°C to +80°C.
Figure 2. Data from Figure 1 show that the more the temperature deviates from the ideal +23°C (room temperature), the more the crystal frequency slows. (NOTE: to calculate the crystal's frequency use the following formula: frequency = (1/period) × 32.768
In time-critical applications, the inaccuracy of the RTC cannot be tolerated. So how can these temperature variations be compensated?
Calculating to Compensate for Temperature Fluctuations
The MAXQ3100 contains an on-chip temperature sensor and a digital trim function to compensate the RTC's oscillator. The temperature sensor reads temperature in real time on the chip, and the trim function allows the insertion and removal of pulses every 10 seconds to compensate for the RTC's fast or slow operation, respectively. To determine the number of pulses to insert/remove, one must develop and apply a second-order equation for temperature, which can be accomplished by the following steps.
Calculate the number of seconds that a pulse actually adds, since the trim factor inserts/removes pulses at a 10-second interval:
pulse = (1/32768)/10
seconds = 3.052µs
Determine the number of pulses to add, based on the period required to generate a 1Hz output (Table 1).
n = (Period - 1)/pulse
// pulse ~ 0.000003052
Table 1. Number of Additional Pulses Calculated from Temperature and Time*
Temperature (°C)
Period
No. of Pulses to Add
-35
1.000120504
39.48675072
-30
1.000102766
33.67436288
-25
1.00008535
27.967488
-20
1.000070136
22.98216448
-15
1.000056025
18.358272
-10
1.000045206
14.81310208
-5
1.000034355
11.2574464
0
1.000025078
8.21755904
+5
1.000017937
5.87759616
+10
1.00001248
4.0894464
+15
1.00000806
2.6411008
+20
1.00000519
1.7006592
+25
1.000003799
1.24485632
+30
1.00000448
1.4680064
+35
1.000007354
2.40975872
+40
1.00001105
3.620864
+45
1.000016831
5.51518208
+50
1.000025447
8.33847296
+55
1.000034671
11.36099328
+60
1.000049247
16.13725696
+65
1.000060162
19.71388416
+70
1.000077059
25.25069312
+75
1.000095825
31.399936
+80
1.000117828
38.60987904
*Data are based on the period recorded in Figure 1.
Plot the data collected on a graph using Excel.
Finally, generate a second-order equation by adding a trend line and displaying the equation on the screen.
Figure 3. Chart shows the number of pulses to add for fluctuations in temperature.
Figure 3 demonstrates the number of pulses to add relative to temperature. (Note: to eliminate the first-order value without affecting the outcome of the equation, simply subtract 25 from the temperature reading.)
The second-ordered equation derived is:
n = 0.0113x² + 0.0272x + 1.2768
This second-order equation defines the parabolic relationship between temperature and pulses. Since the temperature value is known from the on-chip temperature sensor, one can determine the number of pulses needed to compensate for the delta frequency of the RTC crystal.
Example Scenario
if x = -60
n = ?
n = 0.0113x² + 0.0272x + 1.2768
n = 0.0113(-60²) + 0.0272(-60) + 1.2768
n = 0.0113(-60²) + 0.0272(-60) + 1.2768
n = (0.0113 × 3600) - 1.632 + 1.2768
n = 40.3248
To validate the equation, calculate the following:
Once the number of pulses to insert/remove is determined, the value of the RTRM register can be set. The digital TRIM function automatically adds or removes the number of pulses specified in the RTRM register.
If the computation for the second-order equation is correct, the trim factor derived from your equation will compensate for the delta period/delta frequency due to temperature. The comparison graph between compensated and uncompensated temperature will look similar to Figure 4 and Figure 5.
Figure 4. Data shows the time difference between temperatures compensated and uncompensated by a trim factor.
Figure 5. . Data shows the difference in frequency between temperature compensated and uncompensated by a trim factor.
In conclusion, the MAXQ3100's on-chip temperature sensor and the digital trim function allow easy measurement and compensation for RTC accuracy variations due to environmental temperature changes. Using temperature compensation, you can adjust the RTC to run with better than 5ppm (parts per million) accuracy over the full -40°C to +80°C range.
Appendix A. Sample Code
//*******************************************************************//
// Function: calculate_trim_factor
// Description: calculate trim factor based on temperature input
// Returns: trim factor
// Destroys: N/A
// Notes: N/A
//*******************************************************************//
int calculate_trim_factor (int temperature)
{
// 2nd order equation derived from experiment Temperature vs. Pulses per 10 sec
// y = 0.0113x² + 0.0272x + 1.2768
return ((0.0113 × (temperature × temperature)) + (0.0272 × temperature) + 1.2768) + 0.5;
}
//*******************************************************************//
// Function: set_trim_factor
// Description: subroutine to get temperature and do trim calculation
// Returns: N/A
// Destroys: N/A
// Notes: RTRM will be assigned value returned from
// calculate_trim_factor
//*******************************************************************//
void set_trim_factor (void)
{
while (1)
{
if ((TPCFG & START) != START) // make sure temperature conversion is not running
{
TPCFG |= START; // set bit to start temperature conversion
while (1)
{
if ((TPCFG & TPIF) == TPIF) // wait for temperature conversion to complete
{
wait_for_rtc();
RCNT |= 0x8000; // write enable real time clock
wait_for_rtc();
RTRM = calculate_trim_factor(((TEMPR & 0x1FFF) >> 4) - 25); // set trim factor
wait_for_rtc();
RCNT &= ~0x8000; // turn off write access
TPCFG &= ~TPIF; // reset interrupt flag
break;
}
}
break;
}
}
}
//*******************************************************************//
// Function: main
// Description: main sub for application
// Returns: N/A
// Destroys: N/A
// Notes: subroutine in charge of peripheral initialization,
// handling interrupts, display lcd data, and determining
// when it is time to go grab on chip temperature
// to determine trim factor
//*******************************************************************//
void main (void)
{
unsigned long time = 0;
unsigned char flg = 0;
initialize_lcd(); // initialize lcd
initialize_clock(); // initialize real time clock
TPCFG |= 0x40; // set temperature resolution and enable temp. interrupt
EIE0 |= EX7; // enable external interrupt (SW2)
EIES0 |= IT7;
clear_lcd(); // clear LCD segments
while (1)
{
if ((EIF0 & IE7) != 0) // check external interrupt
{
flg += 1; // toggle lcd data display
if (flg == 3)
flg = 0;
EIF0 &= ~IE7; // reset interrupt
}
time = ((unsigned long) RTSH) << 16 | RTSL;
switch (flg)
{
case 0:
display_time(time); // display time on lcd
break;
case 1:
if ((TPCFG & START) != START) // make sure temperature conversion is not running
{
TPCFG |= START; // set bit to start temperature conversion
while (1)
{
if ((TPCFG & TPIF) == TPIF) // wait for temperature conversion to complete
{
display_val(TEMPR); // display temperature value
TPCFG &= ~TPIF; // reset interrupt flag
break;
}
}
}
break;
default:
display_val(RTRM); // display trim factor
break;
}
// go grab the temperature and set trim value on the top of every 5 minutes intervals
if (((((time % 3600)/60) % 5) == 0) && ((((time % 3600) % 60) % 60) == 0))
{
set_trim_factor();
}
}
}
의견을 보내주세요! 위 내용이 도움이 되셨나요? 여러분의 의견을 기다립니다 — Maxim은 보내주신 정정이나 제안사항을 반영하고 있습니다.
이 페이지를 평가하고 의견을 보내주십시오.