Crystal Compensation Look-Up Table Example

To deal with changing temperatures, it is possible to continuously measure the temperature, and apply compensation for this in the RTC ISR. This will help keep the clock more accurate. To avoid expensive floating or fixed point math, the crystal temperature compensation values can be stored in a look-up table.

Crystal manufacturers typically give a plot or an equation to describe magnitude of the drift as a function of temperature. If higher precision is desired compared to a manufacturers specification, a three-point measurement can be done at different temperatures for each PCB. This way it is possible to find the correct curve for each crystal and circuit. If the measurement of the internal temperature sensor is done at the same time, the data points should align well between the crystal drift and the measured temperature.

In the temperature compensation example found in Atmel | START, the parabolic curve comes from the following mathematical function:

Δf/F0=-0.04·(T-25)2

The compensation value used in the RTC ISR is taken directly from the table generated from the above function. The RTC overflows every second, and the error value from the table is added to a variable that keeps track of the total accumulated error in each second. When this accumulated error variable is larger than one period of the 32.768 kHz crystal, it subtracts one or more cycles from the RTC period. When the accumulated error is less than one period, the RTC period value is set to the regular 1-second period value.

As the equation is a parabola, and the values of the table will be symmetric around the top point, it is possible to store only the values from 25 °C up to 105 °C and use the subset of values from of 25 °C to up to 90 °C for temperatures between 25 °C to -40 °C. This will reduce the flash size required.

In the example most calculations are done in the RTC and ADC ISR, and the code being executed is not very time consuming.

In the RTC ISR, the accumulated error is calculated. It consists of the accumulated error not yet compensated for, plus the static crystal error per second, plus the temperature compensation error per second. The temperature compensation is found using a look-up in the ADC ISR. The ADC conversion is started from the RTC ISR.

Note: The RTC period register is written in the RTC ISR. The value that is written will have to be synchronized to the register in the 32.768 kHz domain. The CPU will not have to wait for this to be done, as long as the previously written value to the same register has been synchronized. In this code, this is not a problem. If the register was written outside of the ISR, or if the PER value was very low (1-3 cycles), so that the ISR is executed in quick succession, it could become a problem. Please note that the RTC has a separate synchronization mechanism for each register. Writing to two different registers in the RTC in quick succession will not be a problem.

In the ADC ISR, the ADC result is read out, gain and offset compensation is performed, and the result is converted to kelvins. The result is then used to find the compensation value to use from the temperature compensation look-up table.

The code in the example uses the internal temperature sensor of the device. It is also possible to use a more accurate external temperature sensor. If this is done, the clock accuracy should increase.