4.7 Counters and Timers
Timers T0 and T1
The AT89S8253 has three timers/counters marked as T0, T1 and T2. Timers T0 and T1 completely fall under the 8051 Standard. There are no changes in their operation.
Timer T2
Timer 2 is a 16-bit timer/counter installed only in new versions of the 8051 family. Unlike timers T0 and T1, this timer consists of 4 registers. Two of them, TH2 and TL2, are connected serially in order to form a larger 16-bit timer register. Like timers 0 and 1, it can operate either as a timer or as an event counter. Another two registers, RCAP2H and RCAP2L, are also serially connected and operate as capture registers. They are used to temporarily store the contents of the counter register.
The main adventage of this timer compared to timers 0 and 1 is that all read and swap operations are easily performed using one instruction. Similar to T0 and T1, it has four different modes of operation to be described later in this chapter.
T2CON (Timer/Counter 2 Control Register)

This register contains bits controlling the operation of timer 2.
TF2 bit is automatically set on timer 2 overflow. In order to detect the next overflow, this bit must be cleared from within the program. If bits RCLK and TCLK are set, overflow has no effect on the TF2 bit.
EXF2 bit is automatically set when a capture or a reload is caused by a negative transition on the T2EX pin. It generates an interrupt (if enabled), unless the DCEN bit of the T2CON register is set. The EXF2 bit must be cleared from within the program.
RCLK is receive clock bit which determines which timer is to be used as receive clock for serial port:
- 1 - T2 is used as receive clock for serial port.
- 0 - T1 is used as receive clock for serial port.
TCLK is transmit clock bit which determines which timer is to be used as transmit clock for serial port:
- 1 - T2 is used as transmit clock for serial port.
- 0 - T1 is used as transmit clock for serial port.
EXEN2 is timer 2 external enable bit used to include the T2EX pin in timer 2 operation:
- 1 - Signal on the T2EX pin affects timer 2 operation.
- 0 - Signal on the T2EX pin is ignored.
TR2 is timer 2 run control bit used to enable/disable timer 2:
- 1 - Timer 2 enabled.
- 0 - Timer 2 disabled.
C/T2 is timer/counter 2 select bit used to select pulses to be counted by counter/timer 2:
- 1 - 16-bit register (T2H and T2L) counts pulses on the C/T2 pin (counter).
- 0 - 16-bit register (T2H and T2L) counts pulses from the oscillator (timer).
CP/RL2 is timer 2 capture/reload bit used to define transfer direction:
- 1 - If EXEN=1, pulse on the T2EX pin will cause a number to be transferred from counter to capture register.
- 0 - Under the same condition, signal on the T2EX pin will cause a number to be transferred from capture to counter register.
Timer T2 in Capture mode
If the CP/RL2 bit of the T2CON register is set, timer 2 operates according to the figure below. This is so called
Capture mode in which the value of the counter register (consisting of RCAP2H and RCAP2L) can be “captured” and copied to the capture register (consisting of RCAP2H and RCAP2L), thus not affecting the counting process. This is how it operates:
- First, it is necessary to write a number from which the counting starts to a 16-bit register (TH2+TL2).
- Timer 2 is enabled by setting the TR2 bit of the TCON register. Each coming pulse increments the number stored in the 16-bit register by 1. When both registers are loaded (decimal number 65536), the first next pulse causes an overflow, reset occurs and counting starts from zero.
Settings:
Timer T2 in auto-reload mode
The auto-reload mode configures timer 2 as a 16-bit timer or event counter with automatic reload. It is controlled by the DCEN bit of the T2MOD register. Setting the DCEN bit enables timer 2 to count up or down from the specified value. The T2EX pin controls the counting direction:
T2OE - Enables timer 2 to operate as independent clock generator.
DCEN - When set, it enables counting in either direction- "up" and "down".

As seen in figure above, unlike Capture mode, the contents of the capture register (RCAP2H, RCAP2L) is now copied in the opposite direction upon an overflow occurs, from capture (RCAP2H, RCAP2L) to counter register (TH2, TL2).
Settings of Auto Reload mode are shown in the table below:

All previously mentioned about timer 2 is in force only if the T2MOD register hasn't been changed, i.e. if DCEN = 0. Otherwise, timer/counter is enabled to count in either direction, which depends on the T2EX pin:
T2EX = 0 Timer 2 counts down
T2EX = 1 Timer 2 counts up

On counting up, the whole procedure is similar to the previous mode with one exception referring to the function of the EXF2 bit.
On counting down, an overflow occurs when values stored in the counter and capture registers match. It causes the TF2 bit as well as all bits of registers T2H and T2L to be set while the counter keeps on counting down: 65535, 65534,65533...
In either case, the EXF2 bit is assigned a new function. When an overflow occurs, this bit inverts the signal and cannot be used for generating an interrupt anymore. Instead, it serves as supplementary bit (the 17th bit) of the counter register, making this counter virtually a 17-bit register.
Timer T2 as a baud rate generator
The Timer T2 can be used as a baud rate generator and a clock generator simultaneously. If the RCLK or TCLK bit of the register TCON is set, timer T2 turns into a clock generator, so called Baud Rate generator). This mode is very similar to auto-reload mode. The baud rate is computed using the following formula:

There are a few details to be aware of:
- This formula works only if the internal oscillator is used as a clock generator (in this mode, clock is divided by 2, instead of 12)
- Overflow has no effect on the TF2 bit and does not generate an interrupt.
- Whether the EXEN2 bit is set or not, the T2EX pin logic state has no effect on the timer. It means that the T2EX pin can be used as an external interrupt source in this mode.
- Timer should be disabled (TR2=0) prior to writing or reading from registers TH2 and TL2. Otherwise, an error in serial communication might occur.
Timer T2 as a clock generator
As previously mentioned, timer T2 can also be used as a clock generator. In all previous examples, the P1.0 pin (marked as T2 in figures) is used as an alternative clock generator for this timer, i.e. it acts as an input. Besides, it can also output pulses. By using a 16MHz quartz crystal, the frequency of pulses it generates ranges from 61Hz to 4MHz with a 50% duty-cycle.
To configure this pin as an output, the C/T2 bit of the T2CON register must be cleared, whereas the T2OE bit of the T2MOD register must be set. The TR2 bit enables the timer and the pin outputs rectangular waves the frequency of which ca be calculated using the formula below:
