Hardware manual

Common Features Description
Atmel 8051 Microcontrollers Hardware Manual 2-39
4316A–8051–01/04
2.12 Timer 2 Timer 2 is a 16-bit timer/counter which is present in most of the Atmel 8051 microcon-
trollers.The count is maintained by two 8-bit timer registers, TH2 and TL2, that are
cascade connected. Like Timers 0 and 1, it can operate either as a timer or as an event
counter.
It is controlled by the T2CON register (See Table 2-11) and the T2MOD register (See
Table 2-12). Timer 2 operation is similar to Timer 0 and Timer 1. C/T2
selects F
OSC
/6
(timer operation) or external pin T2 (counter operation) as timer register input. Setting
TR2 allows TL2 to be incremented by the selected input.
It has three operating modes: ‘capture’, ‘autoload’ and ‘baud rate generator’, which are
selected by bits in T2CON as shown in Table 2-10.
In the capture mode there are two options which are selected by bit EXEN2 in T2CON. If
EXEN2 = 0, then Timer 2 is a 16-bit timer or counter which upon overflowing sets bit
TF2, the Timer 2 overflow bit, which can be used to generate an interrupt. If EXEN2 = 1,
then Timer 2 still does the above, but with the added feature that a 1-to-0 transition at
external input T2EX causes the current value in the Timer 2 registers, TL2 and TH2, to
be captured into registers RCAP2L and RCAP2H, respectively. RCAP2L and RCAP2H
are new Special Function Registers in the 80C52, 83C154 and 83C154D. In addition,
the transition at T2EX causes bit EXF2 in T2CON to be set, and EXF2, like TF2, can
generate an interrupt.
The capture mode is illustrated in Figure 2-16.
Figure 2-16. Timer 2 in Capture Mode
In the auto-reload mode there are again two options, which are selected by bit EXEN2 in
T2CON. If EXEN2 = 0, then when Timer 2 rolls over it not only sets TF2 but also causes
the Timer 2 registers to be reloaded with the 16-bit value in registers RCAP2L and
RCAP2H, which are preset by software. If EXEN2 = 1, then Timer 2 still does the above,
Table 2-10. Timer 2 Operating Modes
RCLK + TCLK CP/RL2 TR2 Mode
0 0 1 16-bit auto-reload
0 1 1 16-bit capture
1 X 1 baud rate generator
XX0(off)
Periph
Clock
/ 6