An Overview of Timers in Microcontrollers
The basic function of a timer is to measure the passage of time. While the functions and features vary depending on the type of timer, some timers are capable of measuring very short intervals on the order of microseconds, as well as longer periods such as seconds, minutes, hours, days, months, and even years.
By applying timers in different ways, it is also possible to change the level of an output signal at fixed time intervals or to trigger actions using interrupts.
Functions and Characteristics of Various Timers
Timer (Counter)
A timer measures time by counting up or counting down the clock signals input to the timer circuit. Most timers are equipped with a reload function that automatically resets the count value in preparation for an overflow *1 or underflow *1 condition.
In the case of timers embedded in a microcontroller, software can be used to start and stop the timer operation, as well as to read the current timer value. The resolution of time measurement and the maximum measurable time interval are determined by the period of the clock signal used by the timer and the bit width of the timer.
In general, timers are composed of counter circuits. By inputting a stable clock signal with a fixed interval, the timer can measure time accurately. Furthermore, if the clock signal is provided externally, the timer can also be used as a counter to count external input signals.
When used as a timer, a stable clock signal is supplied from the clock generator, enabling accurate time measurement. When used as a counter, external pulse signals input to an I/O port can be counted by connecting the EXCLm pin to the I/O port.
However, in the models currently released by our company, the EXCLm pin is not connected to an I/O port. Therefore, these devices cannot be used as counters. If counter functionality is required, it can be implemented using the enhanced T16B circuit. For details, please refer to the technical manual for each device.
In addition to basic timer functions, some timers are equipped with a capture function that allows instantaneous timer values to be held, as well as a compare function that compares the timer value with a preset value and changes the signal output from a pin based on the comparison result. These functions make it possible to generate PWM (Pulse Width Modulation) waveforms.
By using PWM waveforms, applications such as AC motor speed control and LED brightness control can be implemented.
Real Time Clock (RTC)
A Real Time Clock (RTC) is an extended timer that can count seconds, minutes, hours, months, years, and days of the week. By inputting a clock signal (typically 32,768 Hz), it can be used as a clock and calendar. Most real time clocks also support leap years.
The real time clocks built into our microcontrollers support not only seconds, minutes, hours, months, years, and days of the week, but also a stopwatch function with 1/10-second and 1/100-second resolution. This is because our microcontrollers were originally designed with wristwatch applications in mind.
The stopwatch counter operates independently of the real time clock. Therefore, it is possible to measure time using the stopwatch while the clock function is running simultaneously.
Leap years do not occur strictly every four years; for example, the year 2100 is not a leap year. However, in many real time clocks, leap years are often handled simply by treating every fourth year as a leap year. As a result, the calendar functions of many commercially available clocks are limited to operation up to the year 2099.
Watchdog Timer
A watchdog is, as the name suggests, a "guard dog"-a dog that stays in front of a house and drives away suspicious intruders. In a microcontroller, however, instead of driving away intruders, the watchdog monitors the operation of the microcontroller itself. If abnormal operation is detected, it triggers a reset of the microcontroller or forces an interrupt.
Because it is extremely difficult to directly determine whether a microcontroller is malfunctioning, the watchdog timer does not attempt to detect errors in that way. Instead, it simply checks whether the software running on the microcontroller clears the timer within a specified time limit. If the timer is not cleared within this period, the watchdog timer assumes a malfunction and takes corrective action, such as resetting the system.
The 10-bit counter built into the watchdog timer is continuously incremented by the clock. When this counter overflows *1, the microcontroller determines that a malfunction has occurred and triggers a reset or a forced interrupt (NMI).
To prevent a reset or interrupt from being generated, the software running on the microcontroller must repeatedly clear the 10-bit counter before an overflow occurs.
Software Timer
Although a software timer is not implemented as a hardware circuit, it is a commonly used type of timer and can be implemented on any microcontroller.
When a program is running on a microcontroller, the CPU always performs a sequence of operations: fetching, decoding, and executing instructions. A software timer makes use of the execution time of these CPU instructions. For example, if it is known that a single instruction takes 1 microsecond to execute, then immediately after executing 10 instructions, 10 microseconds will have elapsed, and after executing 100 instructions, 100 microseconds will have elapsed.
Typically, a software loop that runs for a specified number of iterations is created to determine the timer duration. For this reason, measuring time accurately is difficult, but setting a required waiting period is relatively easy. However, during the waiting period created by a software timer, parallel processing by other software tasks is not possible.
In most cases, no processing is required during this waiting period, so NOP (No Operation) instructions are used to generate the delay.
In recent years, pipeline processing *2 has become common as a CPU instruction execution method, and the execution time of each instruction often varies depending on conditions.
Therefore, when implementing a software timer, careful attention must be paid to the execution time of individual instructions.
*1 Overflow / Underflow
In computers and electronic circuits, overflow or underflow refers to a condition in which a numerical value exceeds or falls below the range that can be represented by the allocated number of digits or data size.
*2 Pipeline Processing
In a computer, when an instruction is processed, it must go through several stages, such as fetching, decoding, executing, and writing back results. In non-pipelined processing, the next instruction is not processed until all of these stages for the current instruction have been completed.
In pipeline processing, however, the processing of the next instruction begins without waiting for all stages (fetch, decode, execute, write-back) of the current instruction to complete. These stages are handled in parallel. As a result, pipeline processing improves throughput (the amount of processing performed per unit time) compared to non-pipelined processing.
