Real Time Clocks (RTCs) in Microcontroller Timers

Real Time Clocks (RTCs) in Microcontroller Timers

Overview of Real-time Clocks

The real-time clock’s basic function is to produce intervals of one second and maintain a continuous count.

You can see a representation of this in the diagram below.

Real Time Clocks (RTCs) in Microcontroller Timers

Figure 1. This timing diagram depicts the basic function of an RTC

Also shown is a program function, A, reading a seconds counter and scheduling an event, B, to occur three seconds in the future. This action is called an alarm. Notice the seconds counter runs continuously and does not stop and start. Two primary requirements of an RTC are accuracy and continuous operation.

The next diagram shows common hardware features of an RTC.

Real Time Clocks (RTCs) in Microcontroller Timers

Figure 2. Real-time clock hardware features

An RTC often has its own internal oscillator with an external crystal and an option to use an external frequency reference. All clock sources run at 32,768 Hz. An external clock source allows the use of a very accurate and stable device such as a TCXO (temperature-compensated crystal oscillator).

A clock source is selected with a multiplexer and input to a prescaler which divides the clock by a factor of 32,768 (2^15) to produce a one-second clock.

A basic RTC has a seconds counter which is usually 32 bits or more. Some RTCs have specialized counters to keep track of the time of day and the calendar date.

A basic RTC without time and date counters uses software for this purpose. A common option is a 1 Hz square wave from an output pin. An RTC will have several possible events to generate a processor interrupt.

An RTC often has a dedicated power pin to allow operation when the rest of the microcontroller is powered down. This power pin is typically connected to a battery or separate power supply.

RTC Accuracy and Frequency Compensation

The accuracy of an RTC depends on the 32,768 Hz clock source. In a well-designed crystal oscillator, the major source of error is the crystal. An external TCXO can be used for highly accurate timing, or special frequency compensation techniques are used with less expensive crystals and the internal oscillator. There are three major sources of error from a crystal.

  1. Initial circuit and crystal tolerance
  2. Crystal drift with temperature
  3. Crystal aging

The graph below shows several concepts related to RTC accuracy.

Real Time Clocks (RTCs) in Microcontroller Timers

Figure 3. Graph showing error measurement with temperature changing

The dark blue trace on this graph shows a typical initial tolerance and the change with temperature. The pink trace shows just the temperature error. The key to compensating for temperature is the fact that the behavior of a crystal is well known and predicted with a quadratic equation. If the initial error is measured after the circuit board is manufactured and the temperature is known, it is possible to compensate for the largest error sources.

The yellow band is a reasonable target for accuracy after careful compensation. Keep in mind 1 ppm over a year is about 30 seconds. Crystal aging is difficult to compensate for. Fortunately, aging is usually only a few ppm per year.

How to Change RTC Timing

Here are two ways to change the timing of an RTC as part of a system to compensate for errors.

The first diagram (Figure 4) depicts the number of oscillator cycles counted by the prescaler for each period of the seconds counter.

The first two seconds are the usual 32,768 cycles. The software uses temperature readings and the initial error to determine that the oscillator is running a little fast and 32,768 cycles is actually a period of 0.99990 seconds. To compensate for this small error, the software tells the RTC to change the modulus of the prescaler to 32,781 for every fourth second to add some time.

Real Time Clocks (RTCs) in Microcontroller Timers

Figure 4. A representation of oscillator cycles counted by a prescaler

This technique has the advantage of a small change in the period from second to second. However, the technique requires an adjustable prescaler and additional registers to hold the special prescale count and the number of seconds between application of the special count.

What if the RTC does not have a special prescaler to adjust the timing? This diagram shows another method.

Real Time Clocks (RTCs) in Microcontroller Timers

Figure 5. The same situation as shown in Figure 4, but without a prescaler

In this case, the number in the boxes is the seconds counter. The count shown is 100251 followed by 100252. The software has been continuously calculating the adjustment and keeping track of the RTC seconds count. When the error accumulates to exactly one second, the software adds or subtracts a second to adjust for the accumulated error.

A disadvantage of this technique is the change from second to second is large when an adjustment is done. This technique has the advantage of compatibility with any RTC.

Security in RTCs

Security is an interesting requirement. There are applications where time is used for billing customers for using a service or consuming a resource. There is an extensive body of practice around preventing or detecting hacks of RTCs. Techniques range from intrusion detection for enclosures to special features within the microcontroller.

The RTC on a microcontroller that I am currently using has special registers to allow software to permanently lock critical registers. Once locked, they cannot be changed and are protected from hacking or out-of-control code. Changing the time requires a complete reset of the microcontroller.

Time and Date

Some RTCs have hardware counters to maintain the time of day and calendar date. This requires counters for minutes, hours, days, months, years, and consideration for leap years. Time of day and calendar dates can also be kept by software.

电话:0755-23068369