MC M0DULE 4 Timer Counter
MC M0DULE 4 Timer Counter
1
Two 16-bit timers :-
2
3
Functions performed by Timer/Counter
hardware:-
Calculating time between events(Timer)
Counting events (pulses)
Generating baud rates for serial port
4
5
6
TMOD-Timer Mode Register
Mode selection operation
Select the Timer0 to operate as
timer/Counter
Select the Timer1 to operate as
timer /Counter
TCON – Timer Control register
Start & stop timer 0 & timer 1
Provide status of timer/ counter overflows
Provide status of external interrupts
Configures external interrupts as either low
7
Operating frequency (Timer clock frequency)
=Crystal frequency/12
8
TMOD Register (Byte –addressable -
89h)
Bit 7 Bit6 Bit5 Bit4 Bit 3 Bit2 Bit1
Bit0
Gate C/ T M1 M0 Gate C / T M1
M0
Timer 1 Timer 0
9
Timer-1
Bit 7: Gate
Gate =0 – Timer 1 is internally controlled by
10
Bit 6: C/ T
C/ T = 0 - Timer 1 acts as a timer(increment
every machine cycle)
C/ T = 1 – Timer 1 acts as a counter( count
the pulses on T 1 pin (P3.5) )
11
Bit 5 and Bit 4: M1 and M0
M1 M0 Operation
Mode 0
0 0 13 Bit timer/ counter
(8 bit- TH0 & 5 bit -TL0)
Mode 1
13
Bit 2: C/ T
C/ T = 0 - Timer 0 acts as a timer
C/ T = 1 – Timer 0 acts as a counter ( count
the pulses on T0 pin (P3.4) )
Bit 1 and Bit 0 ----- mode operation is
same as Bit 5 & Bit 4
14
TCON Register (Bit – addressable -88h)
7 6 5 4 3 2 1 0
15
Bit 5: TF0-Timer 0 Overflow Flag
TF0 =0 –there is no Timer 0 overflow
TF0=1- there is Timer 0 overflow
17
Bit 0:IT0-External Interrupt 0 Signal Type
Control bit
18
1) Initialize TMOD for the required Timer (0/ 1) & Mode
(0 /1/2)
2) Load the initial count value in registers TL & TH ( of
Timer 0 or 1)
3) Start the Timer (SETB TR1 or SETB TR0 bit of TCON
register)
4) Wait until the Timer flag is set (TF1 or TF0 of TCON)
5) Stop the timer (TR0 or TR1 is cleared)
6) Clear TF flag
7) Repeat from step 2 for the next delay.
19
(Initial value-1)=Maximum – Time x Crystal
freq
Value delay 12
Maximum value
Mode 0= 13 bit timer = 1FFFh =8191d
Mode 1= 16 bit timer= FFFFh=65535d
Mode 2= 8 bit timer= FFh=255d
Mode 3= 8 bit timer = FFh=255d
20
MODE 1 PROGRAMMING
1)Write an assembly & C –program to generate a
delay of 12 µ sec using Timer 1 in Mode 1 with
XTAL freq=22MHz.
24
a) not considering the overhead due to
instructions
(Initial value-1)=Maximum – Time x Crystal freq
Value delay 12
25
b) Overhead due to instructions
Assembly Level Program Machine
cycles
ORG 0000h
MOV TMOD ,#10H -------2
UP: MOV TL1,#0EAH-----------2
MOV TH1,#0FFH-----------2
SETB TR1 ----------------1
HERE: JNB TF1,HERE -----------22
CLR TR1 ----------------1
CLR TF1 -----------------1
SJMP UP -----------------2
END
26
b) Using Formula ,
T = C X 12d
Crystal Freq
27
1) Generate a square wave of 50% duty cycle
on P2.3 pin. Use Timer 0 in Mode 1 to
generate the delay. Use a initial value of
FF4Eh for the timer & calculate crystal
frequency of square wave generated by
a) not considering the overhead due to
instructions
b) Considering Overhead due to instructions
28
Assembly Level Program
ORG 0000H
MOV TMOD ,#01H------------2
AGAIN : MOV TLO,#4EH---------------2
MOV TH0,# 0FFH--------------2
CPL P2.3--------------------1
LCALL DELAY----------------3
SJMP AGAIN-------------------2
DELAY: SETB TR0--------------------1
WAIT: JNB TF0,WAIT---Timer delay cycles
CLR TR0--------------1
CLR TF0----------------1
RET---------------------2
TOTAL =Timer delay cycles
+17 29
C- Program
#include <reg51.h> {
Void delay (void); TR0 =1;
Sbit squrpin = P2^3; while (TF0==0);
Void main ( ) TR0 =0;
{ TF0=0;
TMOD = 0X01; }
While (1)
{
Squrpin =~squrpin;
TLO= 0X4E;
TH0= 0XFF;
delay ( );
} }
Void delay (void) 30
Duty cycle= Ton
Ton + Toff
= T =0.5 = 50%
2T
a) not considering the overhead due to instruction
Time delay=(FFFFh-FF4Eh +1) x12/11.0592 M
= 193.13µ sec
Frequency, f = 1/ 2T
= 1/2X 193.13µ sec
= 2.589 KHz
31
b) Considering Overhead due to instructions
34
ON Time initial value = 35BH
8-Bit 5-bit
35
Assembly Level Program
MOV TMOD ,#00H
AGAIN: MOV TLO, #1Bh
MOV TH0,#1Ah
SETB P3.4
ACALL DELAY
MOV TL0,#04H
MOV TH0, #54H
CLR P3.4
ACALL DELAY
SJMP AGAIN
DELAY: SETB TR0
WAIT: JNB TF0,WAIT
CLR TR0
CLR TF0
RET
36
C-Program
38
Using formula,
TH0 =A4H
39
Assembly Level Program
MOV TMOD ,#02H
MOV TH0,#A4H
SETB TR0
WAIT: JNB TF0,WAIT
CPL P1.4
CLR TF0
SJMP WAIT
END
40
C –Program
# include <reg 51.h>
Sbit outpin = P1^4;
Void main( )
{
TMOD =0X 02;
TH0= 0XA4;
TR0= 1;
While (1)
{
Outpin = ~ outpin;
While (TF0 = = 0);
TR0 = 0;
TF0 =0;
}
}
41
2) Write a program to toggle all bits of P2
continuously every 50msec .Use Timer 1
mode 2 with XTAL freq=11.0592MHz to
compute delay.
= FFh x 12/11.0592MHz
= 0.276m sec
42
50 m sec = 0.25 msec
200
45
}
}
Void delay 1 ( )
{
Unsigned char i;
for(i=0 ; i<200 ; i++)
{
TR1 =1;
While (TF1 ==0);
TR1 =0;
TF1=0;
}
}
46