68
4182O–CAN–09/08
Timers/Counters The AT89C51CC03 implements two general-purpose, 16-bit Timers/Counters. Such are
identi fied as Tim er 0 and Time r 1, an d can be in depende ntly confi gured to operate in a
variety of modes as a Timer or an event Counter. When operating as a Timer, the
Timer/Counter runs for a programmed length of time, then issues an interrupt request.
When oper ating as a Coun ter, the Timer /Counter counts n egative transit ions on an
external pin. After a preset number of counts, the Counter issues an interrupt request.
The v arious o perating m odes of each Time r/Counter are des cribed in the foll owing
sections.
Timer/Counter
Operations A basic operation is Timer registers THx and TLx (x = 0, 1) connected in cascade to
form a 16-bit Timer. Setting the run control bit (TRx) in TCON register (see Figure 30)
turns the Timer on by allowing the selected input to increment TLx. When TLx overflows
it incre ments THx; when THx overflows it sets th e Timer overfl ow flag (TFx) i n TCON
register. Setting the TRx does not clear the THx and TLx Timer registers. Timer regis-
ters can be acc essed to obtai n the cur rent count or to enter pres et value s. They ca n be
read at any time but TRx bit must be cleared to preset their values, otherwise the behav-
ior of the Timer/Counter is unpredictable.
The C/Tx# control bit selects Timer oper ation or Counter operation by selecting the
divided-down peripher al clock or external pin Tx as the source for the counted signal.
TRx bit must be cleared when changing the mode of operation, otherwise the behavior
of the Timer/Counter is unpredictable.
For T imer oper atio n ( C/Tx# = 0 ), t he Ti mer regi ster co unts the divid ed-d own periph era l
clock. The Timer register is incremented once every peripheral cycle (6 peripheral clock
peri ods). The Tim er clo ck ra te is FPER/6, i.e. F OSC/12 in standard mode or FOSC/6 i n X2
mode.
For Count er oper ati on (C/T x # = 1), the T imer reg ister cou nts t he neg ative tran si tions on
the Tx external input pin. The external input is sampled every peripheral cycles. When
the sample is high in one cycle and low in the next one, the Counter is incremented.
Since it takes 2 cycles (12 peripheral clock periods) to recognize a negative transition,
the maximum count rate is FPER/12, i.e. FOSC/24 in standard mode or FOSC/12 in X 2
mode. There are no restrictions on the duty cycle of the external input signal, but to
ensure that a given level is sampled at least once before it changes, it should be held for
at least one full peripheral cycle.
Timer 0 Timer 0 functions as either a Timer or event Counter in four modes of operation.
Figure 35 to Figure 38 show the logical configuration of each mode.
Timer 0 is control led by th e four lower bits of T MOD regi ster (s ee Figur e 31) and bits 0,
1, 4 and 5 of TCON register (see Figure 30). TMOD register selects the method of Timer
gating (GATE0), Timer or Counter operation (T/C0#) and mode of operation (M10 and
M00). TCON register provides Timer 0 control functions: overflow flag (TF0), run control
bit (TR0), interrupt flag (IE0) and interrupt type control bit (IT0).
For norm al Tim er ope ration ( GATE 0 = 0), settin g TR0 allows TL0 to be inc reme nted by
the selected input. Setting GATE0 and TR0 allows external pin INT0# to control Timer
operation.
Timer 0 overflow (count rolls over from all 1s to all 0s) sets TF0 flag generating an inter-
rupt reque st.
It is important to stop Timer/Counter before changing mode.