Lecture 16
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
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
M1,M0
Used for mode selection. Because the Timers of 8051 can be set in 4-different modes.
Timer
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
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.
TR0=1 TR0=0
Start timer TH0 TL0
Stop timer
TF = 0 TF = 0 TF = 0 TF = 0 TF = 1
TF Monitor TF until TF=1
Steps of Mode 1
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; }