Internet Explorer is not a supported browser for TI.com. For the best experience, please use a different browser.

VIDEO SERIES

Precision labs series: Timers and control

This series explains the timing and control features found on microcontrollers.

Video Player is loading.
Current Time 0:00
Duration 0:00
Loaded: 0%
Stream Type LIVE
Remaining Time 0:00
 
1x
  • Chapters
  • descriptions off, selected
  • captions off, selected

      Presenter(s)

      download

      Hi, everyone. My name is Shreya Mandal, and I'm a Product Marketing Engineer for MSP430 MCUs. Today I'll go over an introduction of what timers are inside of a microcontroller, as well as some timer basics. This video serves as an introduction, and future videos will cover different timer modes and modules in further detail. Let's get started.

      Do you have MCU projects in mind that need to keep track of the actual time of day or even the data? What about monitoring the exact speed of something, such as a motor or a drive? Maybe wanting to generate or sample sound and audio signals or a low power application that only wakes up periodically.

      All of these applications will require the use of timers. Timers are used for measuring time between events. This could be for input such as sampling analog signals, like audio or sensor data, or for generating outputs for control or display applications. Even fading in LEDs usually done with a timer. In this training, we will introduce basic functionality of MCU timers.

      At its core, a timer is simply a counter that counts from a starting value to a maximum value. The count can increment or decrement depending on the timer's mode. Timers have an input clock source, which ultimately drives how fast or slow the timer counts. Knowing the amount of time between each count and the exact number of counts is how it is able to actually measure time.

      Timers are periodic, meaning that once they reach a fixed value, they'll loop and continue to repeat their counting pattern. They can also be started or stopped, usually by an enable or start bit. Timers are also independent, which means they can continue to count in the background without any interrupt of the CPU, so the CPU can continue to execute other code.

      Timers can count in a few different modes. Most timers typically start counting at zero and count up to their maximum value. This is generally referred to as up mode and can be seen in the top right. The maximum count value depends on the number of bits that makes up a timer's counter.

      Some timers, such as the general purpose timers and C2000 digital signal processors, do the opposite, though, starting at their maximum value and decrementing to zero. This is typically referred as down mode and can be seen in the bottom right. In the middle, there's an example of a timer in up and down mode, which as the name suggests, count up to the maximum and then counts back down to zero. All three modes are equally useful, and many timers can support all three.

      Here's a block diagram of a basic timer. In an MCU, the timer will usually have the option to select between multiple clock sources with an input multiplexer. These clock sources could be internal or external. Next, there's generally a clock divider or pre-scalar stage. As mentioned, knowing the exact timer clock speed is the first critical step of measuring time with the timer.

      The timer clock is fed into the actual timer counter, which in this example is a 16-bit counter. There will be generally some sort control block to configure the time behavior and the timer period register for adjusting the timer period. Finally, the timer counter is typically configured to generate an interrupt back to the CPU, another peripheral, or even an external GPIO when it reaches a certain point.

      Here you can see the timer counter incrementing over time. Each cycle of the timer clock will increment the timer's counter. At some point, the counter will reach its maximum value and overlook back to the start. This overflow generates the most basic interrupt generated by the timer, this interrupt event where some action occurs in software or hardware.

      After the timer reaches its maximum value and overflows, it will continue to count periodically as long as the timer is actively running. One thing to note is that it usually takes one additional timer clock cycle to overflow the timer and make it jump from the maximum value back to the start, so the total period between overflow interrupts is actually the timer's period plus one.

      Here's an application example to help explain these principles. In this example, the input clock is a 2 megahertz signal, but is divided by 2 in the clock divider. The first equation shows that the timer clock is an input clock divided by the clock divider. So the timer clock is actually 1 megahertz. Next, the timer period register is set to hex FFFF, which is the 16-bit timers max period. Hex FFFF is equal to a max count of 65,535.

      Now that we the timer clock frequency and the timer counter's period, we can calculate the frequency between overflow interrupts. As seen in the second equation, if we divide the timer clock by the timer's period plus 1, we will get the interrupt frequency. In this example, it's approximately 15.26 Hertz.

      An additional feature that many general purpose timers will have is the addition of capture compared registers. The timer period register sets the period in which the timer overflows, but the capture compare register can trigger an interrupt at an arbitrary timer counter value. The capture compare register may also be connected to input and output modules to provide enhanced functionality.

      For example, when configured with the input module, the register could capture the timer value when an event or edge happens on an input signal. Examples of using the output module could be an external signal toggle or maybe to trigger an ADC conversation.

      As just mentioned, using a timer with a compared register will allow multiple interrupts to be generated per timer period. In this example, one compare register is set just over the halfway point for the timer period and will trigger its own compare interrupt. If there are additional compare registers, it may be possible to have several more interrupts per a timer period.

      These interrupts can be used for almost anything, but typically they are used for functions that should happen in relationship with the overflow interrupt. The simplest example is probably toggling an LED, saying you want to turn on the LED every second, but only want it on 4.2 seconds. You could set your timer period to one second and use the overflow interrupt to turn on the LED, then set your compare interrupt 4.2 seconds later to turn the LED back off and just let this repeat endlessly.

      This example builds upon the previous application example. Input clock is still 2 megahertz and clock divider is still divided by 2, giving a timer clock of 1 megahertz. The timer period is also still 0xFFFF. Instead of calculating the timer interrupt frequency, instead equation two here's calculating the timer interrupt period, which is the period divided by the timer clock.

      So the timer interrupt period is 65.5 milliseconds. The computer register's loaded with hex 8500, which is when the compare interrupt will trigger. Since the timer is in the up mode, it'll restart at zero after the overflow, so in equation three we calculate the time between the timer overflow and the compare interrupt. In this case, it comes out to 34 milliseconds.

      These timer periods are easier to comprehend when we put them back on this timer counter graphic. As calculated on the previous example, the overall timer period is 65.5 milliseconds. And at the end of this time period, the overflow interrupt will be generated. The comparer interrupt is 34 generated milliseconds after the overflow.

      In this graphic, you can see that by changing the compare register the compare interrupt can easily move earlier or later throughout the timer period. This is really useful when you need to interrupts or two triggers in exact relation from each other.

      In our upcoming timer module videos, we will focus further on three main use cases of timers, using them in compare mode, and using them in capture mode, and special purpose timers, such as watchdog timers and RTCs. We'll go over at a high level how each use case works, though specific register names will vary by microprocessor. Consult the MCU's data sheet for details on how to enable and configure each timer mode or its equivalent module.

      Thank you for watching our timers introduction video. For more information, please visit the additional timer videos or timer labs in the description below.

      View series

      Precision labs series: Timers and control