LECTURE 16
TIMERS/COUNTERS
CHAPTER 9
Timers /Counters
The 8051 has 2 timers/counters:
Timer/Counter 0
Timer/Counter 1
They can be used as
1. The Timer :Used as a time delay generator.
The clock source is the internal crystal frequency of the
8051.
2. An event Counter.
Input pin to count the number of events
These clock pulses represent the number of people passing
through an entrance, or the number of wheel rotations, or
any other event that can be converted to pulses.
Registers Used in Timer/Counter
8051 has two 16-bit Timer registers ,Timer 0 & Timer 1.
As 8051 has 8-bit architecture , each 16-bits timer is accessed
as two separate registers of low byte and high byte
Timer 0: TH0, TL0
Timer 1: TH1, TL1
TMOD Register
Both Timer 0 &Timer 1 use the same Mode register TMOD (8 Bits)
It is an-8-bit register .The lower 4-bits are meant for Timer 0 & the upper 4-bits
are meant for Timer 1
It is not bit addressable.
It is used similar to any other register of 8051 . For ex:
MOV TMOD,#21H
TMOD Register
M1,M0
Used for mode selection. Because the Timers of 8051 can be set in 4-different modes.
Timer
Set the initial value of registers
Start the timer and then the 8051 counts up.
When the registers equal to 0 and the 8051 sets a bit (Timer Flag) to
denote time out 8051
P2 P1
Set
Timer 0 TH0
TL0
TMOD Register
TMOD Register
Every timer has a mean of starting and stopping.
GATE=0
Internal control
The start and stop of the timer are controlled by way of
software.
Set/clear the TR for start/stop timer.
SETB TR0
CLR TR0
GATE=1
External control
The hardware way of starting and stopping the timer by
software and an external source.
Timer/counter is enabled only while the INT pin is high and
the TR control pin is set (TR).
TMOD Register
Examples
Indicate which mode and which timer are selected for each of the
following.
(a) MOV TMOD, #01H
(b) (b) MOV TMOD, #20H
(c) (c) MOV TMOD, #12H
Find the timer’s clock frequency and its period for various 8051-
based system, with the crystal frequency 11.0592 MHz when C/T
bit of TMOD is 0.
Find the value for TMOD if we want to program timer 0 in mode 2,
use 8051 XTAL for the clock source, and use instructions to start and
stop the timer.
Working of Timer (Mode 1)
For this , let us consider timer 0 as an example.
16-bit timer (TH0 and TL0)
TH0-TL0 is incremented continuously when TR0 is set to 1. And the
8051 stops to increment TH0-TL0 when TR0 is cleared.
The timer works with the internal system clock. In other words, the
timer counts up each machine cycle.
When the timer (TH0-TL0) reaches its maximum of FFFFH, it rolls
over to 0000, and TF0 is raised.
Programmer should check TF0 and stop the timer 0.
Steps of Mode 1
1. Choose mode 1 timer 0
MOV TMOD,#01H
2. Set the original value to TH0 and TL0.
MOV TH0,#0FFH
MOV TL0,#0FCH
3. Start the timer.
SETB TR0
Steps of Mode 1
4. The 8051 starts to count up by incrementing the TH0-
TL0.
TH0-TL0= FFFCH,FFFDH,FFFEH,FFFFH,0000H
TR0=1 TR0=0
Start timer TH0 TL0
Stop timer
FFFC FFFD FFFE FFFF 0000
TF = 0 TF = 0 TF = 0 TF = 0 TF = 1
TF Monitor TF until TF=1
Steps of Mode 1
5. When TH0-TL0 rolls over from FFFFH to 0000, the
8051 set TF0=1.
TH0-TL0= FFFE H, FFFF H, 0000 H (Now
TF0=1)
6. Keep monitoring the timer flag (TF) to see if it is
raised.
AGAIN: JNB TF0, AGAIN
7. Clear TR0 to stop the process.
CLR TR0
8. Clear the TF flag for the next round.
CLR TF0
Write a Program to toggle LEDs connected to Port 1 by
inserting some delay
Org 0h MOV P1,#0FFH
START:
MOV TMOD,#01H
MOV P1,#00H MOV TL0,#0FCH
MOV TH0,#0FFH
MOV TMOD,#01H SETB TR0
MOV TL0,#0FCH AGAIN1: JNB TF0,AGAIN1
MOV TH0,#0FFH CLR TR0
SETB TR0 CLR TF0
AGAIN: JNB TF0,AGAIN
CLR TR0 Sjmp START
CLR TF0 end
Write a Program to toggle LEDs connected to Port 1 by
inserting some delay
Org 0h Delay:
START: MOV TMOD,#01H
MOV P1,#00H MOV TL0,#0FCH
ACALL Delay MOV TH0,#0FFH
MOV P1,#0FFH SETB TR0
ACALL Delay AGAIN: JNB TF0,AGAIN
SJMP START CLR TR0
CLR TF0
RET
end
Write a Program to toggle LEDs connected to Port 1 by
inserting some delay
#include <reg51.h>
void Delay();
Org 0h
void main()
START:
{
MOV P1,#00H
while(1)
ACALL Delay
{
MOV P1,#0FFH
P1=0x00;
ACALL Delay
Delay();
SJMP START
P1=0xFF;
Delay:
Delay();
MOV TMOD,#01H
}
MOV TL0,#0FCH
}
MOV TH0,#0FFH
void Delay()
SETB TR0
{ TMOD=0x01;
AGAIN: JNB TF0,AGAIN
TL0=0XFC;
CLR TR0
TH0=0xFF;
CLR TF0
TR0=1;
RET
While(TF0==0);
end
TR0=0; TF0=0; }