Timers are peripheral devices that can be used to set the interval between events. In embedded systems design, the ability to precisely set the timing between events is extremely common. Timers are used to in multi-threaded operating systems to determine how long a task is active before swapping to a new task. Timers can be used to pulse width modulate (PWM) an LED to save power. Timers can also be used to determine the sampling rate of an analog signal. Almost any embedded software project will take advantage of timers.
A timer is essentially a finite state machine that increments or decrements a register once per clock cycle. All timers have a register that contains the current value of the time. Timers also have a reload register. The reload register is used to set the period it takes for the timer expire. Most timers in a modern microprocessor will have more than two registers that dictate the behavior of the timer, but we can use these two registers to examine how timers allow us to measure a precise duration of time.
Let’s assume that a basic timer decrements the COUNT register once per clock cycle. In order to set an arbitrary period of time, the programmer is allowed to set the RELOAD register with an arbitrary value. The timer will then decrement until the CURRENT register equals zero. In order to determine the amount of time that has passed between the initial load and the timer reaching zero, we need to know the frequency at which the clock decrements. The frequency at which the clock decrements is simply the clock frequency driving the clock peripheral.
As an example, lets say that the clock peripheral was running at 50MHz. This means that the clock decrements once every 20nS. If the RELOAD register was set to a value of 100,000, we can determine the period of the timer to be 20nS • 100,000 = 0.002S or two milliseconds.
In addition to the CURRENT and RELOAD register, timers have several other characteristics to take into account. Below is a short list of characteristics that a timer might have.
A timer can be a count up or count down timer. A count down timer counts down from the value in the RELOAD register until it reaches 0. A count up timer will initialize CURRENT to zero and count up until the CURRENT register reaches the value in the RELOAD register.
A Timer that is free running will begin the count sequence at its maximum value and then count down to zero. Once the CURRENT register equals zero, it will reload the CURRENT register with the maximum count value and count down again. This process will repeat until the timer is explicitly turned off by software.
A Timer that is periodic, will begin the count sequence over again once the programmed period of time expires. In the case of a count down time, once the CURRENT register equals zero, it will reload the CURRENT register with the value contained in the RELOAD register. This process will repeat until the timer is explicitly turned off by software.
A one-shot timer will count up/down for for a single period of time. Once the timer expires, the timer will stop counting and wait for software to re-arm it.
Prescalars act as a method to slow down how quickly a clock will increment/decrement the CURRENT register. Instead of modifying the CURRENT register every clock cycle, a prescalar allows you to set the number of clock cycles the timer waits before modifying the count register.
If we used the same example as above ( 50MHz clock, a load value of 100,000) and add a prescalar of 3, the total amount of time it will take for one period is ( 20nS • 100,000 • 4) = 8mS ( A prescalar of 0 means that the timer will count on every core clock cycle. A prescalar of 1 means that he timer will count on every other core clock cycle)
Real Time Clock
A real time clock is a specialized timer that is used to determine the date and time that an event has occurred. An application might know that two events are separated by 5 seconds, but at what time of the day? Basic timers allow an application to determine the interval between two events, but that period of time may have no relationship to what time of the day did the event happen.
The purpose of a real time clock is to provide date and time information to a system. Once the date/time are set in a system, the real time clock will continue to run as long as power is supplied. The real time clock is then used to place a time stamp on important events for later analysis. The real time clock can also be used to schedule periodic events where the frequency of the event span hours or days.
A watchdog timer is a specialized timer that is meant to ensure that an embedded system is functioning properly. It is designed as a fail safe mechanism to handle situations that the software was not designed to encounter.
A watchdog timer is configured in a similar manner to a general purpose timer. The only difference is that if the watchdog timer expires, then the microprocessor will reboot or suspend activity indefinitely.
The reason that the watchdog timer acts as a fail safe method is that the embedded application is written in such a way that during normal operation, the application will periodically reset, or pet, the watchdog timer. As long as software is functioning correctly, the watchdog will never expire and the application continues on.
If the normal flow of the application goes awry ( say we end up in the fault handler because we follow a NULL pointer), the watchdog timer can reset the microprocessor with the hope that the situation that caused the fault will not occur again.
One of the decisions that an embedded software designer needs to decide is what to do if the watchdog timer expires. Should the board be reset and hope that all is well after the reset? Or should the application enter into a fail safe mode where any the system tries to disable any devices that may cause harm to the system or an individual? Each embedded system is designed with different functional and safety requirements. It is normally those requirements that will dictate what action should be taken if the watchdog timer expires.