“Timers and counters are probably the most common peripherals in MCU designs. Almost any application can use a timer or counter to improve performance, reduce power consumption, or simplify a design by replacing repetitive or looping CPU operations with simple timer or counter interrupts. However, you may not have used some of the newer timer/counter units, and some advanced features are now available to further improve your design. This article will quickly review some of the new timer/counter features that can be used to improve designs with illustrative examples from popular MCU families, with a special emphasis on features for autonomous operation and motor control.
Author: Warren Miller
Timers and counters are probably the most ubiquitous peripherals in MCU designs. Almost any application can use a timer or counter to improve performance, reduce power consumption, or simplify a design by replacing repetitive or looping CPU operations with simple timer or counter interrupts. However, you may not have used some of the newer timer/counter units, and some advanced features are now available to further improve your design. This article will quickly review some of the new timer/counter features that can be used to improve designs with illustrative examples from popular MCU families, with a special emphasis on features for autonomous operation and motor control.
Timer and Counter Modes: From Simple to Advanced
Timers and counters start with some very simple modes of operation to replace general program loops used to count external events, time internal and external operations, and collect various statistics on key MCU operations. The features in the Maxim MAXQ612 illustrate some of the most familiar count-mode MCUs and some examples are shown in Figure 1 below. The table at the bottom of the figure shows the three common autonomous modes of operation for Timer B, the MAXQ612 timer/counter module: auto-reload, capture, and pp/count down. The upper right image shows the block diagram of the auto-reload mode. The clock input to TImer B can come from a clock prescaler that divides the system clock with eight different settings, or from an external pin. Control bit TRB enables or disables Timer B operation. The Timer Value Register (TVB) counts up when the timer reaches the value stored in the Timer B Reload Register (TBR) and generates an interrupt and resets TVB to zero. This provides an easy way to create latency without using precious CPU cycles to create latency.
Figure 1: Maxim MAXQ612 MCU example counter/timer mode. (Courtesy of Maxim)
The block diagram in the upper right corner of Figure 1 illustrates the operation of the capture mode. In this mode, the clock prescaler and enable/disable functions are the same as in auto-reload mode. The TBV register counts up and resets to zero on overflow and 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 can be used to count the number of clocks between rising edges of an external signal to determine signal frequency or signal delay. Thus, the timer frees the CPU from performing cycle-intensive counting operations, so it can do what it really needs to be done more efficiently.
The MAXQ612 supports several other common timer/counter operations, such as up/down auto-reload, and an external pin to control the direction of the counter. This mode is useful for decoding various pulse width modulated signals, such as those used in electromechanical sensors. The clock output mode can be used to create a simple output clock using the system clock, prescaler, and timer B’s terminal count. Finally, the Pulse Width Modulation (PWM) output mode can generate edge-aligned signals for common PWM applications such as those used in motor control.
PWM counter/timer function for motor control
Some state-of-the-art timer/counter functions are used for motor control PWM applications. These counters implement as many motor-related PWM functions as possible using dedicated hardware to free up the processor for higher-level functions. The basic operation of a motor control PWM counter/timer is common to most manufacturer implementations, and those in the NXP LPC 17xx PWM timers provide a good example for three-phase AC and DC motors The control application has been optimized. As shown in Figure 2, the functionality of the motor control PWM module may seem quite complicated until you realize that there are three copies of the basic PWM timer channel; one on the left, one in the middle, and one on the right. Having three channels makes it possible to use a single timer/counter to control a three-phase motor, making for a very efficient implementation. Each channel controls a pair of outputs, which in turn control something off-chip, such as a set of coils in a motor. Each channel contains a timer/counter (TC) register that is incremented by the processor clock (timer mode) or 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 edge-aligned mode, TC resets to 0, while in center mode, a match switches TC to decrement on every processor clock or input pin transition until it reaches 0, at which point 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 outputs of the channels toggle whenever the TC matches the match or limit register, while in center-aligned mode they toggle only when the match register is matched. Therefore, the limit register controls the period of the output, while the match register controls how much of each period the output spends in each state. If the output is integrated into the voltage, a small value in the limit register minimizes ripple and allows the motor control PWM timer to control high-speed devices.
All of these channel hardware elements together control two outputs, A and B, which can drive a pair of transistors to switch the control point between the two power rails. Most of the time, the two outputs have opposite polarities, but a dead-time feature can be enabled (on a per-channel basis) to delay the transition of both signals from passive to active state so that the transistors never turn on at the same time . The state of each output pair can be thought of as high, low and floating or up, down and center off. The mapping from active and passive to high and low is programmable for each channel, and each channel can perform edge-aligned and center-aligned PWM. Figure 3 shows two examples of output configurations. The one on the left is center-aligned without any dead time.
Figure 3: NXP LPC17xx Motor Control PWM Timer/Counter Example Output Configuration. (Courtesy of NXP)
The motor control PWM timer also includes several interrupt sources to inform the processor that higher level motor control functions are required. These interrupts are organized by channel and can indicate when the TC matches the match register, when the TC matches the limit register, when the channel captures the value of the TC into its capture register, or when the abort input is active. The LPC17xx also has several supporting peripherals to simplify advanced control functions, including a quadrature encoder interface, additional PWM modules, interrupt timers and watchdog timers. This wide variety of dedicated timing functions shows how important timing functions have become for MCU-based designs.
Other specialized timekeeping functions
On many modern MCUs, timing and counting functions are becoming more specialized as manufacturers target specific application areas. For example, Freescale’s KineTIs K10 family, such as the MK10DN512ZVLQ10, has a variety of timing and counting peripherals with specialized functions. These peripherals include: Programmable delay blocks that control ADC and DAC operations, freeing the processor from managing these low-level processes; flexible timing blocks that provide multiple timing, count, input capture, and output compare channels to support lighting and motor power management and control; a periodic interrupt timer that can autonomously manage peripheral interrupts and DMA transfers; a very low-power timer that runs even when the MCU is in its lowest power state to provide simple Periodic “wake-up” events; real-time clock, keeps accurate time, and can be powered by battery even when the MCU is completely powered down for easy access to system operation and lifetime data.
The K10 series also provides dedicated clock and timing functions dedicated to specific modules, so no other timing resources are consumed. For example, carrier modulator transmitter modules used to create protocols used in various signal encoding schemes such as those in infrared communications have their own dedicated timing and counting functions, much like pulse width modulation counters, management and frequency shift keys. Coding scheme dependent pulse width variation. This trend of dedicated 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 probably one of the most common examples of a specific application suite. As an example, Renesas offers a complete motor control development kit, the YMCRPRX62T shown in Figure 4, which even includes an example motor. This kit comes with all the software and example designs you need to evaluate the Renesas RX62T MCU in multiple motor control designs. The PC-hosted demo GUI displays motor speed, voltage, and current, while allowing the user to adjust parameters and algorithms to directly view various results to help tune motor operation for optimal results in a particular design. Motor control evaluation kits with similar functionality to the Renesas RX62T are also available from many other manufacturers. Find the kit that best matches your target application and development environment to help accelerate your next motor control design by taking advantage of the vast amount of work “bundled” by the manufacturer.
Timers and counters are the most familiar peripherals to MCUs, but taking full advantage of them can ensure you save power, improve performance, and simplify your designs. This article discusses some new and advanced features that make it possible to exploit these common elements in uncommon ways.
The Links: LQ150X1LG91 KCS057QV1BL-G21