Timers

Introduction

In the world of embedded systems, a system often has to interact with events occuring in the time domain. The purpose of a timer is to facilitate decision-making in this regard – Be it counting up, counting down, timing an event, or using inturrupts.

Timers at a Low Level

At the lowest level, a timer is a section of dedicated internal circuity within a microcontroller responsible for counting. When enabled, a register begins to count up by 1 during each clock cycle. While simple at it’s core, timers have numerous applications. Some key terms and characteristics to know are:

  • Tick / Cycle – One ‘event’ within the microcontroller. The receprical of the frequency of the MCU gives the length of one tick. In older MCUs, one instruction / line of assembly was executed per tick, however most modern MCUs are more coplicated. Conceptually, 1 tick represents the ‘delay’ between the outside world and the MCU. Humans ticks effectively take 0 seconds, which allows us to respond incredibly fast to the outside world. This is not always the case in embedded systems.

  • Osciliators – Osciliators are external discrete external components that are given a voltage and product a hard-coded frequency. The most common form of oscilators is a crystal quartz oscilator. These devices are used to provide an external ‘clock’ source for a microcontroller, in the event that the interal clock is not sufficient for the given application.

  • Timer Resolution – Building on the concepts above, the timer resolution is simply the minimum amount of time measureable by the MCU. It equals 1/frequency = timer_resolution in seconds. Most of the time, timer resolution will not be an important factor to consider in designs.

  • Prescaler – As the frequency of an MCU increases, while the resolution of the timer increases, the total length of time it can track before overflowing decreases. For an n-bit timer, the total time tracked before overflowing would be 2^n * timer_resolution. Enabling the timer prescaler will artifically increase the timer resolution (make the timer less precise), in order to track more time before overflowing. For example, a prescaler of 256 will only increment the timer register once every 256 clock cycles.

Every tick, the timer is incremented by 1, up until the maxiumum value for the timer (E.G a 16-bit timer would have a maxiumum value of 2^16)

Timer Applications

Watchdog Timers – One of the many safeguards implemented on embedded systems are watchdog timers. At the basic level, a watchdog timer specifies the amount of time a specific operation is supposed to take. If an operation supasses the watchdog timer, an event is triggered (such as a system reset) Some software/hardware artictures have watchdog timers built-in on certain routines, others can be added by the user.

Systick Timers – In the world of RTOS, some low-level operating system concepts need to be implemented using the hardware avaliable on a microncontroller. For many varations of the ARM Cotrex artitecture there is a special timer used by RTOS' for handling inturrupts and context switching called the systick timer. It is always periodic and decreasing, but generally should not be interacted with by the user.

PWM – Timers are the basis for PWM signal generation. Most systems support a compare mode, which will trigger an inturrupt when the timer equals a non-max value. Additionally, a max value can be set by hand for most timers. Combining these two features enables one to generate a PWM signal with arbitrary frequency (overflow timer value) and period (compared value).

Timer (top) compared to PWM signal (bottom)