A timer is a special clock that is used to measure time intervals. It is a device that counts down from a certain time interval and is used to generate a delay. The timer can be used on an internal clock or from an external source. A meter is a device that stores (and sometimes displays) the frequency with which a particular event or process has occurred relative to a clock signal. This counts events that take place outside the microcontroller. In electronics, counters with register circuits, such as a flip-flop, can be implemented quite easily.
Using the timer and the counter
- Periodic event calendar
- Time base for the measurement
The 8051 has two timers, timer 0 and timer 1. They can be used as timers or as event counters. Timer 0 and Timer 1 have a width of 16 bits. The timer is a maximum of 16 bits, the flag is set when the timer overflows.
Timer 0 register
The 16-bit register of timer 0 is accessible as a low or high byte. The low byte register is referred to as TL0 (low byte timer 0) and the high byte register as TH0 (high byte timer 0). These registers are accessible like any other register. For example, MOV instruction TL0, # 4H shifts the value in the low byte of timer # 0.
Timer 1 register
Timer 1’s 16-bit register is available in low and high bytes. The low byte register is called TL1 (lower byte of timer 1) and the high byte register is called TH1 (upper byte of timer 1). These registers are accessible like any other register. For example, MOV instruction TL1, # 4H shifts the value in the low byte of timer 1.
TMOD register (timer mode)
Timer 0 and Timer 1 use the same register to define the different modes of timer operation. This is an 8-bit register in which the lower 4 bits are reserved for timer 0 and the upper 4 bits for timer. In both cases, the lower 2 bits are used to preset the timer mode and the upper 2 bits are used to specify the location.
Gate − When set, the timer only runs while INT(0,1) is high.
C/T − Counter/Timer select bit.
M1 − Mode bit 1.
M0 − Mode bit 0.
Different time modes
Mode 0 (13-bit timer mode)
Timer 1 and Timer 0 in 0 mode function as 8-bit counters (with divisor by 32 divisor). The timer register is configured as a 13-bit register consisting of the 8 bits of TH1 and the lower 5 bits of TL1. The upper 3 bits of TL1 are undetermined and must be ignored. The definition of the execution indicator (TR1) does not clear the register. The timer interrupt indicator TF1 is activated when the counter of all 1s exceeds 0. The operation of mode 0 is the same for timer 0 as for timer 1.
Mode 1 (16-bit timer mode)
Timer mode “1” is a 16-bit timer and is often used. It works in the same way as the 13-bit mode, except that the 16 bits are used. TLx is incremented from 0 to a maximum of 255. Once 255 is reached, TLx is reset to 0 and THx is incremented by one. Since this is a full 16-bit timer, the timer can hold up to 65,536 different values and returns to 0 after 65,536 machine cycles.
Mode 2 (automatic 8-bit recharge)
The two registers of the timer are configured in 8-bit counters (TL1 and TL0) with automatic reloading. Overflow of TL1 (TL0) defines TF1 (TF0) and reloads TL1 (TL0) with the content of Th1 (TH0) specified by the software. When reloading, TH1 (TH0) remains unchanged. The automatic reload mode has the advantage that you can ask the timer to always contain a value between 200 and 255. If you use mode 0 or 1, you must check the code to see the overflow. In this case, reset the timer to 200. In this case, valuable instructions check the value and / or are reloaded. In mode 2, the microcontroller takes care of it. After setting a timer in mode 2, you do not have to worry about whether the timer has overflowed, or reset the value because the microcontroller hardware does it all for you. The automatic reload mode is used to set a common baud rate.
Mode 3 (split timer mode)
The timer mode “3” is called a common timer mode. When timer 0 is set to mode 3, there will be two separate 8-bit timers. The timer 0 is TL0 and the timer 1 is TH0. Both timers count from 0 to 255 and are reset to 0 when exceeded. All bits in Timer 1 are now linked to TH0. When timer 0 is in split mode, the current timer 1 (TH1 and TL1) can be set in modes 0, 1 or 2, but it can not be started / stopped because the corresponding bits are now linked to TH0 , The actual Timer 1 is incremented every cycle of the machine.
Initialization of Timer
Set the timer mode. Imagine a 16-bit timer that runs continuously and is independent of all external pins. Initialize the TMOD-SFR. Use the lowermost 4 bits of the TMOD and take timer 0 into account. Keep both bits, GATE 0 and C/T 0, at 0, as the timer should be independent of the external pins. Since the 16-bit mode is the timer mode 1, delete T0M1 and set T0M0. In fact, the only bit to activate is bit 0 of TMOD. Now execute the following statement
Timer 0 is now in 16-bit timer mode, but the timer does not work. To start the timer in run mode, set the TR0 bit by executing the following statement:
Now, timer 0 will immediately start counting, being incremented once every machine cycle.