147
AT8xC5122/23
4202E–SCR–06/06
Timers/Counters T he A T8xC5 122D i mpl eme nts two ge neral- purpose , 16 -bit Ti mers/ Coun ters. A lthou gh
they are identified as Timer 0, Timer 1, y ou can independently configure each to operate
in a vari ety o f modes as a Timer or as an e vent Count er. When operating as a T im er, a
Time r/Cou nter ru ns for a prog ramm ed len gth of ti me, then i ssues an interru pt req uest.
W hen ope rating as a Counter, a Timer/Cou nter cou nts ne gative tran sitions on an exter-
nal pin. After a preset number of counts , the Counter issues an interrupt request.
Th e Time r re gisters and ass ociat ed co ntrol regi sters are imp lem ente d as ad dr essa ble
Sp ecial Fun ction Regist ers (SFRs ). Two o f the S FRs pro vide prog ramm able contro l of
the Timers as follows:
• Timer/Counter mode control register (TMO D) and Ti mer/Counter co ntrol register
(TCON) control respectively Timer 0 and Timer 1.
The various operat ing modes of each Timer/Counter are described belo w.
Timer/Counter
Operations For example, a basic operation is Timer registers THx and TLx (x= 0, 1) connected in
casc ade to fo rm a 16-bi t Timer. S etting the r un control b it (TRx) in th e TCON register
(se e Table 88 on pa ge 152) turn s the T imer on by allowin g the sel ected inp ut to incre -
me nt TLx. Whe n TLx overflows, it increments THx and when THx o verflows i t se ts the
Timer overflow flag (TFx) in th e T CON register. Setting the TRx does not clear the THx
and TLx Ti mer registers. Tim er registers can be acc essed t o obt ain the current count or
to e nter preset values. The y c an be read at any time but the T R x bit m us t be c le ared to
preset their values, otherwise the behavior of the Timer/Counter is u npredictable.
The C/ Tx# control bit selects T imer operation or Cou nter operation by s electing the
divided-down system clock or the external pin Tx as the source for the counted signal.
The TRx bit m us t be cleared when changing the operating mode, otherwise the behavior
of the Timer/Coun ter is unpredict able.
For Timer operation (C/Tx#= 0), the Timer register counts the divided-down system
clock. The Timer register is incremented once every peripheral cycle.
Exceptions are the Timer 2 Baud Rate and Clock-Out mode s in which the Timer register
is increment ed by the system clock divided by two.
For Counter operat ion (C/Tx #= 1), the Timer regi ster counts the negative transitions on
the Tx external input pin. The external input is sampled during every S5P2 state. The
Pr ogram m er’s G uide des crib es the no tat ion for th e st ates in a perip hera l cy cle. W he n
the sample is high in one cycle and low in the next one, the Counter is incremented. The
new c oun t val ue app ears in the re gister d uring t he n ext S3P 1 st ate af ter the transit ion
has be en detected. Since it takes 12 states (24 osc illator periods) to recognize a nega-
tive transition, the maximum count rate is 1 /24 of the oscillato r f requency. There are no
restrictions on the duty cycle of the ext ernal input signal, but to ensure t hat a giv en level
is sampled at least once before it changes, it should be held for at leas t one full periph-
eral cycle .
Timer 0 Timer 0 functions as either a Timer or an event Counter in four operating modes.
Figure 90 th rough Figure 96 show the logic configuration of each mode.
Timer 0 is controlled by the f our lower bits of the TM OD register (see T able 89 on page
153) and bits 0, 1, 4 and 5 of the TCON register (see Table 88 on page 152). The TMOD
register selects the method of Timer gating (GATE0), Timer or Counter operation
(T/ C0#) and the op erating m ode (M 10 and M 00). Th e TCON regis te r provide s Time r 0
control functions: overflow flag (TF0), run control bit (TR0), interrupt flag (IE 0) and inter-
rupt type control bit (IT 0).