Timers and Counters in 8051
Timers and Counters in 8051
There are two 16-bit timers and counters in 8051 microcontroller: timer 0 and timer 1. Both
timers consist of 16-bit register in which the lower byte is stored in TL and the higher byte is
stored in TH. Timer can be used as a counter as well as for timing operation that depends on
the source of clock pulses to counters.
Counters and Timers in 8051 microcontroller contain two special function registers: TMOD
(Timer Mode Register) and TCON (Timer Control Register), which are used for activating and
configuring timers and counters.
Timer Mode Control (TMOD): TMOD is an 8-bit register used for selecting timer or counter and
mode of timers. Lower 4-bits are used for control operation of timer 0 or counter0, and
remaining 4-bits are used for control operation of timer1 or counter1.This register is present in
SFR register, the address for SFR register is 89th.
TCON is another register used to control operations of counter and timers in microcontrollers.
It is an 8-bit register wherein four upper bits are responsible for timers and counters and lower
bits are responsible for interrupts.
TF1: The TF1 stands for ‘timer1’ flag bit. Whenever calculating the time-delay in timer1, the TH1
and TL1 reaches to the maximum value that is “FFFF” automatically.
EX: while (TF1==1) Whenever the TF1=1, then clear the flag bit and stop the timer.
TR1: The TR1 stands for timer1 start or stop bit. This timer starting can be through software
instruction or through hardware method.
EX: gate=0 (start timer 1 through software instruction)
TR1=1; (Start timer)
TF0: The TF0 stands for ‘timer0’ flag-bit. Whenever calculating the time delay in timer1, the TH0
and TL0 reaches to a maximum value that is ‘FFFF’, automatically.
EX: while (TF0==1)
Whenever the TF0=1, then clear the flag bit and stop the timer.
TR0: The TR0 stands for ‘timer0’ start or stop bit; this timer starting can be through software
instruction or through hardware method.
EX: gate=0 (start timer 1 through software instruction)
TR0=1; (Start timer)