Module 5
8051 Timer/Counter
8051 Timer/Counter
• The 8051 has two 16-bit timer/counters. These two
timer/counters can be programmed independently.
• There is a bit in the TMOD SFR that specifies whether it is a
timer or a counter.
• If this bit is set, the timer/counter will work as a counter; and if
this bit is reset, the timer/counter will work as a timer.
8051 Timer/Counter
TIMER MODE
• When a timer/counter is functioning as a timer, the timer
register (TH 1 and/or TL1 for Timer 1 or TH0 and/or TL0 for
Timer 0) is incremented after every machine cycle.
• That is, it will be working at 1/12th of the oscillator frequency
because each machine cycle has got 12 oscillator periods.
• As an example, after starting the spin motor in a washing
machine, the next operation, i.e. the motor shut down, is
performed after a fixed time interval.
8051 Timer/Counter
MODES OF TIMER:
These timers can function in four different modes, namely:
i) mode 0, ii) mode 1,iii) mode 2 and iv) mode 3.
• These modes are achieved by setting certain bits in the TMOD-
register.
• Mode 0 to mode 2 are common for both the timers but not
mode 3
8051 Timer/Counter
i) Mode 0:
• In this mode, TL0and TH0 for Timer 0 (or TL1 and TH1 for Timer
1) are used as a 13-bit register, i.e. all the 8 bits of the TL0 or TL1
are utilized and the five lower most bits of the TH0 or TH1 are
used for counting purposes.
• As the count rolls over from all 1s in the register to all 0s, the
interrupt flag is set.
• This timer interrupt flag is a bit, namely TF0 (for Timer 0) in the
TCON, which is a special function register.
8051 Timer/Counter
ii)Mode 1:
• This mode is similar to mode 0, except that in this, 16 bits, that
is, full TL0 and TH0 for Timer 0 (or TL1 andTH1 for Timer 1) are
used for counting.
• So, the interrupt flag will be set only when the 16 bits go from all
1s to all 0s.
8051 Timer/Counter
iii) Mode 2:
• In this mode, the timer register is 8 bits wide.
• TL0 for Timer 0 (or TL1 for Timer 1) is used for this purpose .
• This mode is also called the auto-reload mode as the timer
generates an interrupt on overflow and after generating the
interrupt, will also reload the present value from TH0 into TL0.
• This present value can be put in the TH0 through software.
8051 Timer/Counter
iv) Mode 3:
• If the Timer 0 is put into mode 3, then it acts as two 8-bit
counters(TL0 and TH0 become two separate counters).
• In this case, all the Timer 0 control bits are used by TL0 itself
and TH0 register is locked into a timer function.
• TH0 is counting machine cycles and has taken over the use of
TR1 and TFl from Timer 1.
8051 Timer/Counter
1. TMOD: Timer Mode Control Registers
8051 Timer/Counter
1. TMOD: Timer Mode Control Registers
• If C/T = 1, the timers function as counters to count the negative
transitions' at T0 or T1 pins.
• If C/T= 0, the timers function as timers, that is, they basically
count the number of machine cycles.
• Gate = 0 means that the timer is controlled by TR1 or TR0 only,
irrespective of INT0’ or INT1’.
• Gate = 1 means that the timer control will depend on INT0’or
INT1’ and also on TR0 or TR1 bits.
8051 Timer/Counter
2. TCON: Timer Control Register
• TF 1:Timer 1 overflow flag. Set by hardware when the
timer/counter overflows. Cleared by hardware when the
processor vectors to the interrupt routine.
• TR1:Timer 1 run control bit. Set/cleared by software to turn the
timer/counter on/off.
8051 Timer/Counter
2. TCON: Timer Control Register
• TF0: Timer 0 overflow flag. Set by hardware when the
timer/counter overflows. Cleared by hardware when the
processor vectors to the interrupt routine.
• TR0: Timer 0 run control bit. Set/cleared by software to turn the
timer/counter on/off.
• IE 1: Interrupt 1 edge flag. Set by hardware when the external
interrupt edge is Detected. Cleared when the interrupt is
processed.
8051 Timer/Counter
2. TCON: Timer Control Register
• IT1: Interrupt 1 type control bit. Set/cleared by software to
specify the falling edge/low level triggered external interrupts.
When IT1 = 1 then INTl’ is falling edge triggered, otherwise when
IT1 = 0 the INT1’ is low-level triggered.
• IE0: Interrupt 0 edge flag. Set by hardware when the external
interrupt edge is detected. Cleared when the interrupt is
processed.
8051 Timer/Counter
2. TCON: Timer Control Register
• IT0: Interrupt 0 type control bit. Set/cleared by software to
specifiy the falling edge/low level triggered external interrupts.
When IT0 = 1 then INT0’ is falling edge triggered, otherwise
when IT0 = 0 the INT0’ is low-level triggered.