Design Article
How to get the most out of a single timer on an MCU
Ajit Basarur, Shantanu Prasad Prabhudesai, and Ritesh Ramesh Parekh, Ittiam Systems
3/28/2008 1:17 PM EDT
Typical embedded products need to handle a variety of asynchronous activities, such as inputs from various devices (keyboard, touch screen, etc). They also need to handle a variety of synchronous activities, such as real time clock (RTC) and generation of pulse-width modulated (PWM) signals for specific purposes (e.g. brightness control of LCD display), as shown in Fig 1.

1. Typical embedded design.
These activities are often offloaded to the microcontroller so that the main processor can be put in low power mode for the maximum possible amount of time.
With the typical constraints of an embedded system, microcontrollers need to satisfy the requirements of low-cost, low power consumption, small area footprint, execution speed, and the number of functionalities it supports, which ultimately limits the choice of the device. With all these constraints, the selected µC often has a scarcity of peripherals like timers and communication interfaces; hence, intelligent methods of using resources to their best capacity need to be devised.
For example, timers may be used for various functions such as running a real-time clock (RTC), generating pulse-width modulated (PWM) signals for supplying control voltage, remote control decoders, etc. There is a reasonable advantage if a single timer can be made to handle multiple functions, especially when there is a shortage of on-chip timers in the MCU.
This article suggests a method to incorporate the functionality of RTC and PWM waveform on a single timer in a µC. The technique has been implemented and tested on the MSP430F20xx family of microcontrollers, but the concept can be applied to any generic µC.
Configuration
By appropriate configuration, the timer module can be made to interrupt the CPU at regular intervals. Typically, a real-time clock runs at 1 Hz while a PWM signal may have a frequency in the range of a few hertz to several kilohertz.
Even with such varied requirements of interrupting frequencies, the following technique proves to be applicable on a conventional timer (for example, a 16-bit up-counting or down-counting type) or on a watchdog timer configured in interval mode.
The interrupting interval of the timer has to be set to [1 / (f * N)] seconds, where f is the desired frequency of PWM operation and N is the number of PWM levels required for the application.
For instance, if the PWM output is used to control the brightness of an LCD display, N reflects the number of discrete brightness levels which are available when using this technique. In a typical MCU, the interrupt rate is controlled by a scheme as illustrated in Fig 2.

2. Typical clock generation scheme for an MCU.
As shown in Fig 2, the timer module derives its clock from an external source or an alternate internal source, which is divided by a configurable divisor module within the timer to generate the interrupts at the desired rate.
Now consider Fig 3, which illustrates continuous interrupts being generated by the timer at a rate of (f * N) hertz. Hence, the RTC count (seconds, minutes, etc.) should be incremented for every (f * N) counts (i.e. for every one second).

3. PWM output required from MCU.
Similarly, depending on the desired PWM duty cycle, the PWM output level must be toggled at appropriate interrupt counts. This requires that a state machine be maintained within the timer interrupt service routine (ISR), which decides when to toggle the PWM output and also when to update the RTC count. The operation of such a state machine is explained by means of the flow chart shown in Figure 4.

4. Flow chart showing the program flow of the timer algorithm.
(Click this image to view a larger, more detailed version)
The following five parameters need to be defined in the algorithm:
- f: The PWM output frequency. This frequency is constant and should be above a minimum threshold (about 20 Hz) to avoid visible flicker on the LCD display.
- N: The number of discrete PWM duty cycles which are possible to achieve using the algorithm.
- k: The level of PWM duty cycle set by the user to achieve the desired brightness level.
- C: The ISR counter, which indicates how many times program control has been handed over to the Timer ISR.
- temp: A temporary integer.
Where F, N, k, and C are integers and f * N should also be an integer.


Rakshith Amarnath
3/20/2011 5:15 PM EDT
Good going Ajit n Co :)
Liked your idea. Will try to apply this keeping the limitations in mind.
Sign in to Reply
one_armed_bandit
3/28/2011 11:54 PM EDT
There is a much simpler method - still using the same timer with the same discussion on frequency.
Just maintain a counter - if unsigned:
if( counter .ge. thresh )
{ set output high }
if( counter .ge. 100 )
{ counter = 0; set output low }
If a signed value, set the counter to (- thresh) and test for .ge. 0, then for (100 - thresh).
The unsigned is the easiest. This method avoids the modulo - a very expensive operation. Also, obviously set the initial conditions properly.
Might also want to put the whole logic in an if( doing_pwm ) to turn the PWM on and off.
Sign in to Reply
Keroronsk
8/21/2012 5:28 AM EDT
Seems like pictures is broken.
Sign in to Reply
Plavalli
8/22/2012 7:08 PM EDT
Can't get to see the pictures.
Sign in to Reply
nandorocha13
8/28/2012 12:45 PM EDT
Pictures are broken
Sign in to Reply