Skip to main content
Knowledgebase
Home
Renesas Electronics America - Knowledgebase

How does a microcontroller's on-chip timer counter operates?

Latest Updated:04/01/2006

Question:

Please explain how a microcontroller's on-chip timer counter operates.

Answer:

(1) Introduction

In recent years, almost all microcontrollers have come to include a timer counter.
Some are designed for special purposes, such as inverter control, but the following describes an ordinary type of timer counter.
The name "timer counter" indicates a combination of two functions: timer and (event) counter.
Basically, the same logic is used to implement both the timer and counter functions, and both functions involve counting signals that have been input.
Simply put, the difference between the timer and counter functions is as follows.

Counter: Counts and indicates the number of signals (events) input at any interval.

Timer: Counts the number of signals input at a constant interval to indicate the elapsed time.

In other words, the timer and counter functions differ in their use objectives and in the signals that are input.


In almost all cases, the clock signal that is input can be selected from either a clock signal divided from the internal system clock or an external input clock signal, and these clock signals can be used with either the timer or the counter.

(2) Differences according to counter operation

Timer counters can be broadly categorized as follows based on their count operations.
- Free-running counter that simply performs a count operation
Once a count operation is started, it just keeps running as a counter.
In some cases, this count operation cannot be stopped.

- Counter that can be stopped during a count and can be set to any count period
When the counter reaches a certain value, a signal can be output, the count value can be cleared, or the counter can be stopped.

Free-running counters have simpler logic and so they are sometimes used in inexpensive microcontrollers.
Although watch timers can be sopped, there is usually no need to stop them during normal operations (since that would cause timing problems), so most operate as a free-running counter.
Nevertheless, various convenient uses can be imagined for a microcontroller that includes a timer that is able to be stopped or cleared at any time.

(3)Additional functions for timer counter

When a timer counter is used only for counting, it has a limited range of applications.
One way to enhance the functionality of a timer counter is by adding one or two registers.
Usually, these added registers are:

- Compare registers
- Capture registers

Sometimes, separate registers are used but usually the same register is switched to serve alternately as a compare register and capture register.

A compare register is used to set values that are compared with the timer counter's count values.
The following operations may occur when a value set to a compare register matches a count value:

- An interrupt is issued
- Output level is changed
- Timer is cleared

When using this type of operation, interrupts can occur or signal levels can change at any time interval based on the precision of the timer counter's clock.


A capture register is used to capture count values from the timer counter when a signal is input from an external source.
This operation is implemented in hardware, so it can capture timer counter values more precisely than when the values are read by software.
 

(When a similar operation is performed by software, an interrupt occurs when a signal is input from an external source and the interrupt service program calls a function that captures the time counter's value.
Generally, there is some variation in the timing by which an interrupt is acknowledged after it occurs.
Some time is also required for the interrupt service program to read the timer counter's value.
Consequently, the captured value varies somewhat from the value that reflects the actual timing of the signal input from an external source.)

 

(4)Uses (operations) of timer counters

(a)Timer counters are mainly used for measurement and output functions, which serve the following uses (operations).

[1] Measurement functions
- External event counter
- Pulse width measurement, time interval measurement

[2] Output functions
- Interval timer
- Square wave output
- Pulse output
- PWM output
- One-shot pulse output
- Carrier generation


(b)Description of individual functions

- External event counter
Counting signals (events) that are input from an external source is one of a timer counter's simplest operations.
The timer counter can be used to count how many signals have been input and its count value can be read periodically by a program.
In the example shown in the figure below, the operation is to count how many people arrive in a 10 minute period.
 

Another possible use is to combine a timer counter with a compare register to report when the number of signals (events) from an external source reaches a specified value (at which point an interrupt occurs).
For example, it can be used to report when 24 apples have been loaded into a box that can hold up to 24 apples.
In the example shown below, a value from 24 to 1 is set to the compare register and an interrupt occurs when the counter value (0 to 23) reaches that number of apples.
When this interrupt occurs, the full box is replaced with an empty box.
 


- Pulse width measurement, time interval measurement
This function measures the interval between externally input signals (events), which is a basic use for capture registers.
The figure below shows an example in which a motor's rpm (revolutions per minute) are being detected.
A photo interrupter is used to detect an empty slot in a disk that is attached to the rotating shaft of a motor.
The photo interrupter's output signal is the externally input signal (event) that functions as the trigger input for the capture operation.
Whenever this trigger is input, the counter value is captured and loaded to the capture register, and then the counter is cleared.
This value can be read from the capture register to determine the rotation time, from which the rpm can be calculated.
 

- Interval timer
Interval timer
This function causes an interrupt to occur at a regular interval, and is one of the simplest operations for a timer.
This operation uses a compare register.
An interrupt occurs whenever the timer counter's value matches the value set to the compare register, after which the timer counter is cleared and starts counting again from zero.
Interrupts can be set to occur at a regular interval by repeating this operation.
 


- Square wave output
The basic operation for square wave output is the same as the interval timer operation.
The only difference is that square waves are output.
In the example shown below, an interrupt occurs whenever the timer counter's value matches the value set to the compare register, after which the timer output is inverted and the operation is repeated.

 

- Pulse output
Unlike for the square wave output function, for this pulse output function not only cycle time but also the duty ratio can be changed.
Two compare registers are used for this function: one is used to specify the cycle time and the other is used to specify the duty ratio.
These two compare registers are used differently.
The time counter can be cleared when a match occurs with the compare register that is used to set the cycle time, but when a match occurs with the compare register that is used to set the duty ratio, the output changes but the timer counter is not cleared.
An interrupt request is issued whenever the value in either of these registers matches the timer counter value.
A pulse output operation example is shown in the following figure.
This function can be used to output square waves, similar to the PWM output function described next.
With pulse output, timing problems may occur when changing values in the compare register in order to change the duty ratio.
When the timing is not correct, two matches (or no matches at all) may occur in the same cycle with the compare register used to set the duty ratio, causing an unexpected inversion of timer output.

 


- PWM output
Although this type of operation generates the same waveforms as the pulse output operation, its main purpose is to change the duty ratio.
Often, the cycle time for PWM output remains fixed, in which case the time is determined by the timer counter's bit count.
In such cases, a compare register is used to specify the duty ratio.
Unlike pulse output, PWM output has a clear purpose, so there are no constraints on the compare register write timing when changing the duty ratio.
The figure below shows the operation timing.
Here, the timing is for the initial startup operation.
At startup, the output is inactive (at low level in the figure) during the first cycle.
When the timer counter overflows, the output becomes active, and it becomes inactive again when a match with the compare register occurs.
PWM signals are output by repeating this operation.

 


The following figure shows an operation example in which values in the compare register are overwritten in order to change the duty ratio.
Although "m" is overwritten by "n" during the cycle time, the result is not reflected in the operation until the next overflow occurs.

 


Although the cycle time can be changed for some timer counters, in such cases the end of the cycle time is when a match occurs with the compare register used to set the cycle time, and not when the timer counter overflows.

- One-shot pulse output
In contrast to the consecutive repetition of the output function's operation as described in the examples above, this function outputs only one pulse.
This function uses two compare registers.
One of the registers is used to specify the period between when a trigger occurs and when a pulse is output, and the other is used to specify the pulse's width.
The timing chart below describes this operation.
When a trigger signal is input from an external source, the timer counter is cleared and starts counting again from zero.
When the timer counter value matches the value (m) in the compare register used to specify the output timing, a pulse is output via the timer output.
When the timer counter value matches the value (n) in the compare register used to specify the pulse width, output of this pulse is terminated.

 

- Carrier generation
This function is used to generate signals for infrared remote control operations, and it requires the combined use of two timer counters.
Since this is an application of timer counters rather than a timer counter operation per se, it differs fundamentally from other functions.
An infrared remote control operates by emitting an ON/OFF sequence of infrared rays at a carrier frequency.
Consequently, one timer counter is used to generate a signal that matches this carrier frequency (via pulse output operation).
The other timer counter counts the length (time) of the infrared remote control's signal (interval timer operation).
The interval timer's output signal that indicates when the specified time has elapsed is used to sample the signal to be output by remote control.
A carrier is output when the sampled result is "1" and is not output when it is "0".
The timing chart below describes this operation.
In this example, the carrier signal is already being generated by one of the timer counters.
The value of the compare register used for the interval timer operation is overwritten when a match interrupt occurs, and the settings are made in the sequence of k (OFF time), m (ON time), and then n (OFF time).
[1]   The output data value (1) is sampled when k is matched.
[2]   The output of a carrier signal starts from the timer output.
[3]   The interrupt servicing that occurs after a match writes "m" to the compare register and "0" to the output data.
[4]   The output data value (0) is sampled when m is matched.
[5]   The carrier signal output is stopped.
[6]   The interrupt servicing that occurs after a match updates the compare register and output data value.
This operation is performed by repeating the above operations.
 

Suitable Products