Using Timers and Counters to Create Efficient MCU-based Designs


Timers and counters are perhaps the most pervasive peripherals in MCU designs. Just about any application can use a timer or counter to improve performance, reduce power, or simplify the design by replacing repetitive- or looping-CPU operations with a simple timer or counter interrupt. You may not have used some of the more recent timer/counter units, however, and some of the advanced features now available can improve your design even more. This article will quickly review some of the new timer/counter features you can use to improve your designs with illustrative examples from popular MCU families with a particular emphasis on features for autonomous operation and motor control.

Timer and counter modes: from simple to advanced

Timers and counters started out with some very simple modes of operation to replace common-program loops for counting external events, for timing internal and external operations and for collecting various statistics on key MCU operations. Some of the most familiar counting modes are illustrated by the features in the Maxim MAXQ612 MCU and some examples are shown in Figure 1 below. The table at the bottom of the figure shows three common autonomous operation modes for Timer B, the MAXQ612 timer/counter block: Auto-reload, capture, and pp/down counting. The figure in the upper right shows the block diagram for the auto-reload mode. The clock input for Timer B can come from a clock prescaler that can divide the system clock with eight different settings, or from an external pin. A control bit, TRB, enables or disables Timer B operation. The timer-value register (TVB) counts up when clocked and when it reaches the value stored in the Timer B reload register (TBR) and interrupt is generated and TVB is reset to zero. This provides a simple method for creating delays without using up valuable CPU cycles to create a delay. An external pin can be used to optionally reset TVB to zero as well, making this mode useful for creating a timeout if an expected input does not show up in the expected amount of time.

Figure 1: Maxim MAXQ612 MCU example counter/timer modes. (Courtesy of Maxim)

The block diagram on the upper right of Figure 1 illustrates the operation of the capture mode. In this mode the clocking prescaler and enable/disable functions are the same as in the auto-reload mode. The TBV register counts up and resets to zero when it overflows and generates an optional interrupt. On the falling edge of external-pin TBB, the value in the TBV register is loaded into the capture register, TBR and an interrupt, EXFB, can be generated. This mode is useful for counting the number of clocks between the rising edges of the external signal to determine signal frequency or signal delays. The timer thus frees the CPU from doing cycle-intensive counting operations so it can be more efficient for the tasks it is really needed for.

Several other common timer/counter operations are supported by the MAXQ612 such as up/down auto-reload, where an external pin controls the direction of the counter. This mode is helpful for decoding a variety of pulse-width-modulation signals, such as those used in electromechanical sensors.  A clock-output mode can be used to create a simple output clock using the system clock, a prescaler, and the terminal count of Timer B. Finally, a pulse-width-modulation (PWM) output mode can generate the edge-aligned signals used in common PWM applications, like those for motor control.

PWM counter/timer functions for motor control

Some of the most advanced timer/counter features are used for PWM applications for motor control. These counters implement as much of the motor-related PWM functions as possible using dedicated hardware to free the processor for doing higher-level functions. The basic operation of a motor-control PWM counter/timer are common between most manufacturer implementations and those found in the NXP LPC 17xx PWM timer, which is optimized for three-phase AC and DC motor-control applications, provides a good example. As shown in Figure 2, the functions of a motor-control PWM block can appear rather complex until you realize there are three copies of a basic PWM timer channel; one on the left, one in the middle, and one on the right. Having three channels make it possible to control three phase motors using a single timer/counter for a very efficient implementation. Each channel controls a pair of outputs that, in turn, can control something off-chip, like one set of coils in a motor. Each channel includes a Timer/Counter (TC) register that is incremented by a processor clock (timer mode) or by an input pin (counter mode).

Figure 2: NXP LPC 17xx PWM timer/counter. (Courtesy of NXP)

Each channel has a limit register that is compared to the TC value, and when a match occurs the TC is “reset” in one of two ways. In the edge-aligned mode the TC is reset to 0, while in centered mode a match switches the TC to decrement on each processor clock or input-pin transition until it reaches 0, at which time it starts counting up again.

Each channel also includes a match register that holds a smaller value than the limit register. In edge-aligned mode the channel’s outputs are switched whenever the TC matches either the match or limit register, while in center-aligned mode they are switched only when it matches the match register. Thus, the limit register controls the period of the outputs, while the match register controls how much of each period the outputs spend in each state. Having a small value in the limit register minimizes ripple if the output is integrated into a voltage, and allows the motor-control PWM timer to control devices that operate at high speed.

All these channel hardware elements work together to control two outputs, A and B, which can drive a pair of transistors to switch a control point between two power rails. Most of the time the two outputs have opposite polarity, but a dead-time feature can be enabled (on a per-channel basis) to delay both signals’ transitions from the passive to active state so that the transistors are never on simultaneously. The states of each output pair can be thought of as high, low, and floating or up, down, and center-off. Each channel’s mapping from active and passive to high and low is programmable and each can perform edge-aligned and center-aligned pulse-width modulation. Figure 3 shows two examples of output configurations. The one on the left is center aligned without any dead time. The one on the right has dead time (DT) inserted to insure both outputs are not active at the same time.

Figure 3: NXP LPC17xx motor-control PWM timer/counter example output configurations. (Courtesy of NXP)

The motor-control PWM timer also includes several interrupt sources to make it easy to inform the processor that a higher-level motor-control function is required. These interrupts are organized on a channel basis and can indicate when a TC matches the match register, when TC matches the limit register, when the channel captures the value of the TC to its capture register or when the abort input goes active. The LPC17xx also has several supporting peripherals that simplify higher-level control functions including a quadrature-encoder interface, additional PWM blocks, interrupt timer, and watch-dog timer. This wide variety of specialized timing functions is indicative of how important timing functions have become for MCU-based designs.

Other specialized timing functions

On many modern MCUs timing and counting functions have become increasingly specialized as manufacturers target specific application segments. As an example, the Freescale Kinetis K10 family (such as the MK10DN512ZVLQ10) has a variety of timing and counting-oriented peripherals with specialized functions. These peripherals include: a programmable delay block useful for controlling ADC and DAC operations to free the processor from managing these low-level processes; a flexible-timing module that provides multiple channels of timing, counting, input capture, and output compare to support power management and control for lighting and electric motors; a periodic interrupt timer that can autonomously manage peripheral interrupts and DMA transfers; a very-low-power timer that can operate even when the MCU is in the lowest power state to provide a simple periodic “wake-up” event;  and a real-time clock that keeps accurate time and can even operate off a battery when the MCU is completely powered off, making it convenient source of system operation and lifetime data.

The K10 family also provides specialized clocking and timing functions that are dedicated with a specific block so other timing resources are not consumed. For example, the carrier modulator transmitter block, used to create the protocols used in a variety of signal encoding schemes such as those in infra-red communications, has its own dedicated timing and counting functions, much like a pulse-width-modulation counter, to manage the changes in pulse width associated with frequency-shift-key-encoding schemes. This trend to dedicate specialized timing and counting functions is expected to continue as MCUs become more application and market-segment specific.

Development kits speed time to market

As MCUs become more application specific, manufacturers are creating more application-oriented development kits and reference designs. Motor-control applications are perhaps one of the most common examples of application-specific kits. As just one example, Renesas provides a complete motor-control development kit, the YMCRPRX62T shown in Figure 4, which even includes an example motor. The kit comes with all the software and example design you need to evaluate the Renesas RX62T MCU in several motor-control designs. A PC-hosted demonstration graphic-user interface shows the motor speed, voltage, and current while allowing the user to adjust parameters and algorithms to directly view the various results to help tune motor operation for optimal results in a specific design. Many other manufacturers also have motor-control evaluation kits with some similar features to the Renesas RX62T. Look for the kit that best matches your target application and development environment to leverage the significant amount of work manufacturers “bundle” to help you accelerate your next motor-control design.

Figure 4: Renesas Motor Control Development Kit for RX62T MCU Family. (Courtesy of Renesas)

Summary

Timers and counters are the most familiar peripherals for an MCU, but using them to the fullest extent can make sure you save power, improve performance and simplify your design. This article has discussed some new and advanced features that are making it possible to take advantage of these common elements in uncommon ways.

For more information about the parts discussed here, use the links provided to access product pages on the Hotenda website.

Supplier