Accurate Time Keeping in Embedded Systems
In many embedded systems, there is a need to keep accurate time/date. This is often performed using an RTC (Real-Time Clock). However, uncalibrated RTC are not that accurate. For example, ST Microelectronics M41T94 RTC datasheet explains that:
Uncalibrated clock accuracy will not exceed ±35 ppm (parts per million)
oscillator frequency error at 25°C, which equates to about ±1.53 minutes per month. .
In order to improve the accuracy, you’ll need to calibrate the crystal for each board at the factory using a frequency counter, that will greatly improve accuracy, for M41T94:
When the Calibration circuit is properly employed, accuracy improves to better than ±2 ppm at 25°C.
However, calibrating the crystal will further increase the manufacturing costs.
Another way to have a very accurate time keeping, is to use the power supply frequency (50 Hz/60 Hz) which is extremely accurate due to interoperability requirements between electricity providers.
In the diagram above, you can see an overview of such system design based on 220V / 50 Hz mains.
The transformer will generate 5V AC @ 50Hz, then another circuit (in blue), will generate +5V DC to power all board components and a 5V / 50 HZ squared signal that is fed into a MCU interrupt pin. While the board is running, the time is kept based on the 50Hz interrupt (or 60 Hz). 50Hz/60Hz detection might be required in your software if your device is to be used to several countries. If there is a power failure, the board will switch to battery power and keep the time using the crystal or ceramic oscillator (less accurate). With this system design, there is no need to perform crystal calibration at the factory and a RTC is not needed as time keeping is handled by the MCU. The MCU shoud obviously be a low power MCU that may be used to only control the power for a more powerful MPU.
The final choice of the best design depends on your own system requirements.