Since the dawn of human consciousness, mankind has contemplated and struggled to understand the true nature of time. Wikipedia defines time as “a dimension in which events can be ordered from the past through the present into the future, and also the measure of durations of events and the intervals between them.” Physicists have discovered that time is not continuous but instead has digital like qualities. Time in the universe occurs in discrete chunks which are “atomic” and cannot be broken up into smaller chunks. This time unit is known as Planck time and is on the order of magnitude of 10−43 seconds.
While mankind may not yet fully understand what time is, luckily for most embedded systems a theoretical understanding of the nature of time is not required (unless of course GPS, large distances or velocities are involved which then require an understanding of the effects of General Relativity). Some of these initial thoughts on time seem distant when considering an embedded system but just like the universe, an embedded system has a minimum time quanta that determines the real-time response of the system.
Embedded systems need a reliable method of keeping tracking of time. Even a simple system that only wants to blink an LED every 500 milliseconds needs a way to determine that 500 milliseconds has passed by. More sophisticated systems may need to run tasks every 10 milliseconds or less. One particular system comes to mind that required calculations to be performed every 25 microseconds! If the system didn’t perform the task at exactly that interval then a potentially life threatening situation could have resulted.
It is common practice to keep track of time in an embedded system in two ways, through the use of a timer and the use of a real-time clock. Each of these methods is suited for its own specific application. Timers are a common peripheral included in all microcontrollers and the majority of them on the market today have more than one. A single timer is often setup to act as the system clock. It is setup to increment a tick every 1 millisecond (or some other interval based on the application). This system tick can then be used to schedule tasks at recurring intervals throughout the embedded application.
The real-time clock is usually included in most microcontrollers but they are also available externally. The real-time clock differs from time tracking using a timer in that real-time clocks usually have a resolution of 1 second. Real-time clocks are used to keep track of the date and time on a system. This can be used on a system to schedule long term tasks such as system maintenance or scheduling subsystems to perform certain tasks.
Many embedded systems will use both timers and real-time clocks together in order to provide the system with high resolution time keeping and longer term time keeping. The real-time clock that is internal to an MCU can often be used to wake an MCU from sleep and set alarms. This allows the embedded system to go into a low power state where the timer is stopped. Eventually the real-time clock wakes the system, the timer is restarted and time keeping within the system is maintained.
In the next few posts, the details of how to setup a timer and real-time clock will be explored in addition to application uses. Some of the pitfalls of using timers to keep track of time will be examined so that accurate and bug-free time keeping will be maintained within the system.