0% found this document useful (0 votes)
16 views46 pages

MC M0DULE 4 Timer Counter

Uploaded by

Shashank Patgar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views46 pages

MC M0DULE 4 Timer Counter

Uploaded by

Shashank Patgar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 46

8051 Timers and Serial Port

1
Two 16-bit timers :-

Timer 0(16-bit) and Timer 1(16-bit)

TH0 TLO TH1 TL1


(8-bit) (8-bit) (8-bit) (8-bit)
(8C) (8A) (8D)
( 8B)

2
3
Functions performed by Timer/Counter
hardware:-
 Calculating time between events(Timer)
 Counting events (pulses)
 Generating baud rates for serial port

Two Special Function Registers are used to


control the timers/counter operation.
 TMOD(89h) & TCON(88h)

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

level triggered or falling edge triggered

7
Operating frequency (Timer clock frequency)
=Crystal frequency/12

=11.0592MHz = 921,600 times/sec


12
For Counting 50,000 times the no of seconds
required = 1 sec x 50,000
921,600
= 0.0543 seconds
For 0.05 seconds , the number of counts
possible
= 0.05 x 921600 = 46,080 Counts

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

software by TR1(Timer Run Control bit in TCON).


SETB TR1 and CLR TR1 instructions are
used
 No external hardware needed for start/stop

the timer 1.(regardless of the state of


INT 1 Pin).

Gate =1- Hardware way of starting /stopping


Timer 1 by external source. ( INT 1 (P3.3) =1)

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

Mode 2 0 1 16 Bit timer/counter


(8 bit-TH0 & 8 bit –TL0)

Mode 3 Auto reload mode


1 0
Baud rate Calculation
(TH0 holds a value that is to be
12
Timer-0
Bit 3: Gate
Gate =0 – Timer 0 is internally controlled by

software by TR0 (Timer Run Control bit in


TCON).
SETB TR0 and CLR TR0 instructions are
used
 No external hardware needed for start/stop

the timer 0. (regardless of the state of


INT 0 Pin).
Gate =1- Hardware way of starting /stopping
Timer 0 by external source. (INT 0(P3.2) =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

TF1 TR1 TF0 TR0 IE 1 IT 1 IE 0 IT 0

Bit 7 : TF1-Timer 1 overflow flag


TF1 =0 –There is no overflow in Timer 1
TF1 =1 – There is overflow in Timer 1
Bit 6: TR1- Timer 1 Run Control bit
TR1 = 1 – Turn ON the Timer1 H/W
TR1=0 - Turn OFF the Timer1 H/W

15
Bit 5: TF0-Timer 0 Overflow Flag
TF0 =0 –there is no Timer 0 overflow
TF0=1- there is Timer 0 overflow

Bit 4: TR0- Timer 0 Run Control bit


TR0 =1 – Turn ON the Timer 0 H/W
TR0=0 - Turn OFF the Timer 0 H/W

Bit 3: IE 1-External Interrupt 1 Edge Flag


IE1 =1–Unmasking (enabling)the Timer 1
interrupt INT1
IE 1=0 – Masking(disabling) the Timer 1
interrupt INT1
16
Bit 2:IT1-External Interrupt 1 Signal Type
Control bit
IT1 =1 – There is an external interrupt
request for Timer 1
IT1=0 –There is no external interrupt request
for Timer1

Bit 1: IE 0-External Interrupt 0 Edge Flag


IE0 =1–Unmasking (enabling)the Timer 0
interrupt INT0
IE 0=0 – Masking(disabling) the Timer 0
interrupt INT0

17
Bit 0:IT0-External Interrupt 0 Signal Type
Control bit

IT0 =1–There is an external interrupt request


for Timer 0

IT0=0 –There is no external interrupt request


for Timer0

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.

TMOD Value =10h for Timer 1 in Mode 1

(Initial value-1)=Maximum – Time x Crystal freq


Value delay 12

= FFFFh - 12 µ sec x 22M


12
Initial Value = 65514d = FFEA h
21
Initial value loaded into Timer registers
TH1 = FFh (MSB) ; TL1= EAh (LSB)
Assembly Level Program
ORG 0000h
MOV TMOD ,#10H ; Timer1 with Mode1
UP: MOV TL1,#0EAH; Load initial values
MOV TH1,#0FFH
SETB TR1 ; Start Timer 1
HERE: JNB TF1,HERE ; Wait till TF1=0
CLR TR1 ; Stop Timer
CLR TF1 ; Clear Timer flag
SJMP UP ; Repeat the procedure
END
22
C-Program
# include <reg51.h>
Void main( )
{
TMOD = 0X10;
While (1)
{
TL1 =0XEA;
TH1=0XFF;
TR1=1;
While (TF1 ==0);
TR1 = 0;
TF1=0;
}
}
23
2) Write assembly & C program to find the
delay generated by Timer 1 in mode 1 with
XTAL freq= 11.0592MHz with initial value
FFEA h by
a) not considering the overhead due to
instructions
b) Overhead due to instructions

(Referring to the earlier program)

24
a) not considering the overhead due to
instructions
(Initial value-1)=Maximum – Time x Crystal freq
Value delay 12

Time delay = Max.value –Initial value +1 x12


Cry.freq

Time delay=(FFFFh-FFEAh +1) x 12/11.0592 M


= 23.87µ sec

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

C- no. of machine cycles

Total delay including overhead


= 33 x 12/11.0592M Hz
= 35. 805µ sec

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

Timer delay cycles = Maximum – Initial +1


value value
= FFFFh -FF4Eh +1
= 178 decimal
Total time delay =178+17 =195 Machine
cycles
T = 195 X 12/11.0592MHz
= 211.575 µ sec
f= 1/2T = 1/ 2X 211.57µ sec = 2.36KHz
32
33
1) Generate a square wave with an ON time
=4msec & OFF time= 3msec on pin P3.4.
Assume Crystal freq=22MHz ,use Timer 0 in
mode 0

NOTE : ON time & OFF time are different


hence initial value has to be calculated
separately.

34
 ON Time initial value = 35BH

35B h= 00011 0101 1011

8-Bit 5-bit

TH0 = 1A h , TL0 = 1Bh


 OFF Time initial value = A84h

A84h = 01010 100 00100

TL0= 04H , TH0 =54H


 TMOD register = 00h

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

#include <reg 51.h> delay ( );


Void delay (void); }
Sbit outpin = P3^4; }
Void main ( ) void delay ( void)
{
TMOD =0X00; {
While(1) TR0=1;
{
TLO=0X1B; while (TF0= =0);
TH0=0X1A; TR0 =0;
Outpin=1; TF0 = 0;
Delay ( ); }
TLO =0X 04;
TH0= 0X 54;
Outpin = 0;
37
1) Write a program to generate square
wave of freq 10KHz on P1.4 . Use Timer 0 in
mode 2 with XTAL freq=22MHz.
Given : f= 10KHz , XTAL =22MHz

T = 1/f= 1/10K = 0.1msec


T = Ton + Toff = 0.1 msec
Ton =Toff = 0.1msec/2 = 0.05m sec
=required delay

38
 Using formula,

Initial value = 164 decimal = A4h

TH0 =A4H

Note: In the program initial value is loaded


only once into TH register for MODE -2.

TMOD Register value= 02h

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.

Maximum delay = Max.value X 12


of Mode 2 Crystal freq

= FFh x 12/11.0592MHz
= 0.276m sec

42
50 m sec = 0.25 msec
200

0.25 msec < 0.276 msec


First generate a smaller delay ( 0.25msec)
then repeat the Timer for 200 times to get
the required delay
Therefore Counter =200
Initial value using formula ,

Initial value = 1Ah


 TMOD register = 20h
43
Assembly Level Program
MOV TMOD ,#20H WAIT2: JNB TF1,WAIT2
MOV TH1 ,#1AH CLR TF1
AGAIN: MOV P2,#00H DJNZ R0,WAIT1
SETB TR1 CLR TR1
MOV R0, #200 SJMP AGAIN
WAIT: JNB TF1,WAIT
CLR TF1
DJNZ R0,WAIT
CLR TR1
MOV P2,#0FFH
MOV R0,#200
SETB TR1
44
 C-Program
# include<reg 51.h>
Void delay 1(void);
Void main( )
{
TMOD =0X20;
TH1 =0X1A;
P2=0X58; //Some value on port2 to be toggled
While (1)
{
P2 = ~P2;
delay1 ( );

45
}
}
Void delay 1 ( )
{
Unsigned char i;
for(i=0 ; i<200 ; i++)
{
TR1 =1;
While (TF1 ==0);
TR1 =0;
TF1=0;
}
}
46

You might also like