

The response to the timer event can be very fast and predictable.ĭesigners just starting with microcontrollers sometimes avoid interrupts because they seem complicated. Notice how the main program does not spend time checking for a timer event. When the ISR is done, the main program resumes. An ISR is sometimes called an “interrupt handler” or “exception handler”.
EETECH SMART TIMER SOFTWARE
The timer sends a hardware signal to an “interrupt controller” which suspends execution of the main program and makes the processor jump to a software function called an “interrupt service routine” or ISR. For example, an interrupt occurs when a down counting timer reaches 0 and reloads the modulus in the main counter. A timer event occurs and triggers an interrupt.

“Main” is the regular program running on the processor. Here is a diagram showing a typical program flow using a periodic interrupt. The solution for these problems is using interrupts. This type of coordination can use a lot of processing time or the response time can vary a lot with a complicated program. Polling is periodically reading status registers to detect timer events or the current value of a counter. The program can poll the timer to get timer information. The main program and a timer are asynchronous which means the timer operates independently of program flow. This is typically done as part of the general configuration of the microcontroller. If the timer is connected to an input or output pin, set up the pin connections.If triggering a DMA operation or other peripheral, set up the trigger conditions.Setting a divide factor for the prescaler.These registers are not shown in the diagram for simplicity and, in any case, they are very different for different timers. Included in the control logic are control and status registers. Be sure to check out the modulus load characteristics of a timer.Ĭontrol logic defines the type of timer and varies widely for different types. Notice how the number of count states is M + 1 or 1,000 because of the “0” state. This is a down counter which reloads the modulus value on the next clock after it reaches 0. Here is a typical counting sequence with M = 999. The count range of the main counter is set by a “modulus” value ( M) stored in a register. Sometimes two 16-bit counters can be combined to form a 32-bit counter, but most timing functions are possible with a prescaler and a 16-bit main counter. The output of the prescaler goes to a main counter which is often 16 bits wide and counts within the range of 0 to 65,535. The dividing factor of the prescaler is usually limited to powers of 2. To increase the count range, the selected clock goes to a “prescaler” which divides the clock before it goes to a main counter. Sometimes an external clock source is an option. There are often several possible clock sources and one is selected with a switch or multiplexer. Let’s start with a look at the common features in many timers.Įvery timer needs a clock source or timebase. Subsequent articles cover other common timers. I describe the SysTick timer in detail as an example. Then, I go into the periodic timer and how it can be used. I start with looking at features in common with most types of timers. The focus is not on a particular family of microcontrollers but describes timer functions in general.
EETECH SMART TIMER SERIES
This article is the first in a series on “timer/counter” functions in microcontrollers.
