Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 60

TIMER PROGRAMMING

By
Dr Debashish Dash
Micro & Nano electronics
Vellore Institute of Technology
TIMER PROGRAMMING
• The 8051 has two timer/counters, they can be
used either as
▫ Timers to generate a time delay
▫ Event counters to count events happening outside
the microcontroller
• Both Timer 0 and Timer 1 are 16 bits wide
▫ Since 8051 has 8-bit architecture, each 16-bits
timer is accessed as two separate registers of low
byte and high byte.
Cont.
• The low byte is called TL0/TL1.
• Similarly the high byte is called TH0/TH1.
• These registers are quite simple to access like
any other register
▫ MOV TL0, #4FH
▫ MOV R5, TH0
Cont.
TMOD (Timer Mode)
• To set various timer operation modes, both
timers 0 and 1 use the same register, called
TMOD.
• TMOD is a 8-bit register.
▫ The lower 4 bits are for Timer 0
▫ The upper 4 bits are for Timer 1
 In each case:
 The lower 2 bits are used to set the timer mode.
 The upper 2 bits to specify the operation.
Cont.
• GATE: Used to run or stop the timer/counter
• C/T: Counter / Timer mode
• M1 & M0: 0 0 13-bit timer mode of operation
0 1 16-bit timer mode of operation
1 0 8-bit auto-reload mode of operation
1 1 Split timer mode of operation
Cont.
• If C/T = 0, it is used as a timer for time delay
generation.
• The clock source for the time delay is the crystal
frequency of 8051.
Ex. 1
• Find out which mode and which timer are
selected for each of the following instructions.
▫ MOV TMOD, #01H
▫ MOV TMOD, #20H
▫ MOV TMOD, #12H
• Solution:
▫ TMOD = 00000001, mode 1 of timer 0 is selected.
▫ TMOD = 00100000, mode 2 of timer 1 is selected.
▫ TMOD = 00010010, mode 2 of timer 0 and mode 1
of timer 1 are selected.
Importance of TMOD
• Timers can be started or stopped by either software or
hardware control
▫ In using software to start and stop the timer where GATE = 0
 The start and stop of the timer are controlled by way of software
by the TR (timer start) bits TR0 and TR1
 The SETB instruction starts it, and it is stopped by the CLR
instruction
 These instructions start and stop the timers as long as GATE = 0 in
the TMOD register.
• The hardware way of starting and stopping the timer by an
external source is achieved by making GATE = 1 in the
TMOD register.
Mode 1 Programming
Operations of mode1:
1. It is a 16-bit timer; therefore, it allows value of
0000 to FFFFH to be loaded into the timer’s
register TL and TH.
2. After TH and TL are loaded with a 16-bit initial
value, the timer must be started.
▫ This is done by SETB TR0 for timer 0 and SETB
TR1 for timer 1.
• After the timer is started, it starts to count up
▫ It counts up until it reaches its limit of FFFFH.
Cont.
Cont.
• When it rolls over from FFFFH to 0000, it sets
high a flag bit called TF (timer flag)
▫ Each timer has its own timer flag:TF0 for timer 0,
and TF1 for timer 1
▫ This timer flag can be monitored.
• When this timer flag is raised, one option would
be to stop the timer with the timer with the
instructions CLR TR0 or CLR TR1, for timer 0
and timer 1, respectively.
Cont.
• After the timer reaches its limit and rolls over, in
order to repeat the process
▫ TH and TL must be reloaded with the original
value, and
▫ TF must be reloaded to 0
Generation of Time Delay
• To generate a time delay
▫ TMOD value register indicating which timer (timer 0 or
timer 1) is to be used and which timer mode (0 or 1) is
selected.
▫ Load registers TL and TH with initial count value.
▫ Start the timer.
▫ Keep monitoring the timer flag (TF) with the
 JNB TFx, target
 Get out of the loop when TF becomes high.
▫ Stop the timer.
▫ Clear the TF flag for the next round.
▫ Go back to step 2 to load TH and TL again.
Ex. 2
• Create a square wave of 50% duty cycle on the
P1.5 bit. Use Timer 0 to generate the time delay.
Answer
• MOV TMOD,#01 ;Timer 0 , mode 1
• HERE: MOV TL0,#0F2H
• MOV TH0, #0FFH
• CPL P1.5
• ACALL DELAY
• SJMP HERE
• DELAY:
▫ SETB TR0 ;START THE TIMER 0
▫ AGAIN: JNB TF0,AGAIN ;MONITOR TIMER FLAG 0 UNTIL IT ROLLS
OVER
▫ CLR TR0
▫ CLR TF0
▫ RET
Steps:
• TMOD is loaded
• FFF2H is loaded in TH0 and TL0
• P1.5 is toggled for the high and low portions of the
pulse.
• The delay subroutine using the timer is called
• Timer 0 counts up with the passing of each clock,
which is provided by crystal osc.
• As the timer counts up, it goes through FFF3, FFF4,
FFF5, FFF6, FFF7, FFF8, FFF9, FFFA, FFFB and so
on until FFFFH.
Cont.
• One more clock rolls it to 0, raising the timer
flag (TF0 = 1).
• At that point, the JNB instruction falls
• Timer0 is stopped by CLR instruction.
Ex.3
• Calculate the amount of time delay for the
previous question?
• The number of counts for the roll over is FFFFH –
FFF2H = 0DH (13 decimal).
• However, we add one to 13 because of the extra clock
• needed when it rolls over from FFFF to 0 and raise
the TF flag.
• This gives 14 × 1.085us = 15.19us for half the pulse.
For the entire period it is T = 2 × 15.19us = 30.38us
as the time delay generated by the timer.
Procedure to calculate Delay
Ex-4
Calculate the frequency of the square wave generated on pin P1.5?
Ans:
• HERE: MOV TL0,#0F2H 2
• MOV TH0, #0FFH 2
• CPL P1.5 1
• ACALL DELAY 2
• SJMP HERE 2
• DELAY:
▫ SETB TR0 1
▫ AGAIN: JNB TF0,AGAIN 14
▫ CLR TR01
▫ CLR TF0 1
▫ RET 2
Answer
• T = 2*28*1.085uS = 60.76uS
• F = 1/T = 16458.2Hz
Ex.5
Find the delay generated by the timer 0 using both decimal and
hexadecimal method. Don’t include overhead instruction.
CLR P2.3
MOV TMOD,#01
HERE: MOV TL0,#3EH
MOV TH0, #0B8H
SETB P2.3
SETB TR0
AGAIN: JNB TF0, AGAIN
CLR TR0
CLR TF0
CLR P2.3
Answer
• (FFFFH – B83E + 1) = 47C2H = 18370 in
decimal and 18370*1.085uS = 19.93145mS
• B83EH = 47166. So 65536 – 47166 = 18370. So
all total 19.93145 mS as the width of the pulse.
What will be the largest TH and TL value is
possible so that maximum amount of delay can be
produced?
Answer
• To get the maximum delay value, we have to make the timer count
whole over 65536.
• For this, we need to make TH and TL value should start with 0.
CLR P2.3 ;Clear P2.3
MOV TMOD,#01 ;Timer 0, 16-bitmode
HERE: MOV TL0,#0 ;TL0=0, the low byte
MOV TH0,#0 ;TH0=0, the high byte
SETB P2.3 ;SET high P2.3
SETB TR0 ;Start timer 0
AGAIN: JNB TF0,AGAIN ;Monitor timer flag 0
CLR TR0 ;Stop the timer 0
CLR TF0 ;Clear timer 0 flag
CLR P2.3
Cont.
• Making TH and TL both zero means that the
timer will count from 0000 to FFFF, and then
roll over to raise the TF flag.
• As a result, it goes through a total Of 65536
states.
• Therefore, we have delay =(65536 - 0) × 1.085 us
= 71.1065ms.
Ex.6
If a square wave on P1.5 continuously generated
using timer 1 using time delay. Find the frequency
of the square wave if XTAL = 11.0592 MHz. Don’t
consider overhead condition due to instructions in
the loop.
Answer
MOV TMOD,#10 ;Timer 1, mod 1 (16-bit mode)
AGAIN:MOV TL1,#34H ;Low byte of timer `
MOV TH1, #76H ;High byte of timer 1
SETB TR1 ;Start the timer 1
BACK:JNB TF1, BACK
CLR TR1
CPL P1.5
CLR TF1
SJMP AGAIN
Analysis
• Since FFFFH – 7634H = 89CBH + 1 = 89CCH
and it is equivalent to 35276 counts and 35276 *
1.085 uS = 38.274 mS for half of the square
wave.
• Total frequency is 13.06Hz.
Calculation for the Value to be Loaded in
TL & TH
• Assume frequency is 11.0592MHz.
▫ Divide the desired time delay by 1.085uS
▫ Perform 65536 – n, where ‘n’ is the decimal value
we got in step – 1.
▫ Convert the result of step-2 to hex, where yyxx is
the initial hex value to be loaded into timer’s
register.
▫ Set TL = xx and TH = yy
Ex.7
• Assuming that the frequency is 11.0592 MHz.
What value do we need to load the timer’s
register if we want to have a time delay of 5mS?
Show the program for timer 0 to create a pulse
width of 5mS on P2.3.
Answer
• Crystal frequency is 11.0592MHz. The counter
counts up every 1.085uS.
• This means that out of many 1.085uS intervals,
we must make a 5mS pulse.
• To get that, 5mS/1.085uS = 4608 clocks.
• Then 65536 – 4608 = EE00H, we have TH = EE
and TL = 00
Cont.
CLR p2.3 ;Clear P2.3
MOV TMOD, #01 ;Timer 0, 16-bit mode
HERE: MOV TL0,#0 ;Low byte
MOV TH0, #0EEH ;High byte
SETB P2.3
SETB TR0
AGAIN: JNB TF0, AGAIN
CLR TR0
CLR TF0
Ex.8
• Assuming frequency is 11.0592MHz, write a
program to generate a square wave of 2khz
frequency on pin P1.5.
Solution
• T = 1/f = 1/2KHz = 500uS, the period of square
wave.
• ½ of it for the high and low portion of the pulse
is 250uS.
• 250uS/1.085uS = 230 and 65536 – 230 = 65306
which is hex is FF1AH.
• TL = 1AH and TH = FFH, all in hex.
Answer
MOV TMOD, #01 ;Timer 0, 16 bit mode
AGAIN: MOV TL1, #1AH
MOV TH1, #0FFH
SETB TR1
BACK:JNB TF1,BACK
CLR TR1
CLR P1.5
CLR TF1
SJMP AGAIN
Ex.9
• Assume frequency is 11.0592MHz, write a
program to generate a square wave of 50KHz on
pin P2.3.
Answer
• T = 1/50 = 20uS, the period of square wave.
• ½ of it for the high and low portion of the pulse
is 10mS.
• 10mS/1.085uS = 9216, then 65536 – 9216 =
56320 in decimal and in hex it is DC00H.
• So TL = 00 and TH = DCH
Cont.
MOV TMOD,#10H
AGAIN:MOV TL1, #00
MOV TH1, #0DCH
SETB TR1
BACK:JNB TF1,BACK
CLR TR1
CLR P2.3
SJMP AGAIN
Ex. 10
• Calculate the amount of delay in seconds. Neglect any
overhead occurs in the program.
MOV TMOD, #10H
MOV R3,#200
AGAIN: MOV TL1, #08H
MOV TH1,#01H
SETB TR1
BACK: JNB TF1, BACK
CLR TR1
CLR TF1
DJNZ R3, AGAIN
Answer
TH – TL = 0108H = 264 in decimal. So 65536 –
264 = 65272. Now 65272 * 1.085uS = 70.820mS,
and for 200 counter, multiply 200*70.820mS =
14.16 seconds
MODE 2 PROGRAMMING
• It is an 8-bit auto reload timer, therefore, it allows only
values of 00 to FFH to be loaded in to the timer’s register
TH.
• After loading the value to TH, the 8051 gives a copy of it to
TL
▫ Then the timer must be started
▫ This is done by the instruction SETB TR0 for timer 0 and SETB
TR1 for timer 1
• After the timer started, it starts to count up by incrementing
the TL register
▫ It counts up until it reaches its limit of FFH
▫ When it rolls over from FFH to 00, it sets high the TF.
Cont.
• When the TL register rolls from FFH to 0 and TF
is set to 1, TL is reloaded automatically with the
original value kept by TH register
▫ To repeat the process, we must clear TF and let it
go without any intervention by the programmer to
reload the original value
▫ This makes mode 2 an auto-reload in contrast to
mode 1
To Generate Time Delay
• Load the TMOD indicating which timer (timer 0 or
timer 1) is to be used, and the timer mode is to be
selected.
• Load the TH register with the initial count value
• Start timer
• Keep monitoring the timer flag (TF) with the
instruction JNB TFx, target instruction to see
whether it is raised. Goes out if TF is high.
• Clear the TF flag
• Go back to step 4, since mode 2 is auto reload.
Ex. 11
• Assuming frequency is 11.0592MHz, find the
frequency of the square wave generated on pin P1.0
for the given program.
MOV TMOD, #20H
MOV TH1, #5
SETB TR1
BACK: JNB TF1, BACK
CPL P1.0
CLR TF1
SJMP BACK
Answer
• 256-05 = 251*1.085uS = 272.33uS is the high
portion of the pulse. Since it is a 50% duty cycle
square wave , the period T is twice that, as a
result T = 2*272.33uS = 544.67uS and the
frequency is 1.835KHz.
Ex.12
• Find the frequency of a square wave generated on pin P1.0.
MOV TMOD,#02H
MOV TH0, #0
AGAIN:MOV R5, #250
ACALL DELAY
CPL P1.0
SSJMP AGAIN
DELAY: SETB TR0
BACK: JNB TF0,BACK
CLR TR0
CLR TF0
DJNZ R5, DELAY
RET
Answer
• T = 2(250*256*1.085uS)=138.88mS and
frequency is 72Hz.
Ex:13
• Assuming that programmer working in mode 2
of timer, find the Hex value which need to be
loaded in TH for the given instructions.
• MOV TH1, #-200
• MOV TH0, #-60
• MOV TH1,#-3
• MOV TH1, #-12
• MOV TH0, #-48
Ans
Decimal 2’s Complement (TH value)
-200 38H
-60 C4H
-48 D0H
-12 F4H
-3 FDH
Counter Programming
• Timers can also be used as counters.
• Counters used to count the events happening
outside the 8051
• When it is used as a counter, it is a pulse outside
of the 8051 that increments the TH, TL registers.
• The C/T bit in the TMOD register decides the
source of the clock for the timer.
• If C/T = 1, the timer is used as a counter and gets
its pulses from outside the 8051
Cont.
Ex.14
• Assuming that clock pulses are fed into T1, write
a ALP for counter 1 in mode 2 to count the
pulses and display the state of the TL1 count on
P2, which connected with 8 LEDs.
Answer
MOV TMOD, #01100000B; counter 1, mode 2, C/T = 1
MOV TH1,#0 ;Clear TH1
SETB P3.5 ;Make T1 as input
AGAIN: SETB TR1
BACK: MOV A,TL1
MOV P2,A
JNB TF1,BACK
CLR TR1
CLR TF1
SJMP AGAIN
TCON SFR
• TCON stands for Timer Control register which is
a 8-bit register.
Cont.
• TCON is a bit addressable register.
Ex.15
• Assume that a 1-Hz frequency pulse is connected
to input pin 3.4. Write a program to display
counter 0 on an LCD. Set the initial value of TH0
to -60.
Solution
• To display the TL count on an LCD, we must convert 8-bit binary data to
ASCII.
ACALL LCD_SET_UP
MOV TMOD, #00000110B
MOV TH0, #-60 ;Counting 60 pulses
SETB P3.4
AGAIN:SETB TR0
MOV A, TL0
ACALL CONV
ACALL DISPLAY
JNB TF0,BACK
CLR TR0
CLR TF0
SJMP AGAIN
Cont.
• Converting 8-bit binary to ASCII, upon return, R4,R3, R2 have ASCII data (R2 is LSD)
CONV: MOV B,#10
DIV AB
MOV R2,B
MV B,#10
DIV AB
ORL A,#30H
MOV R4,A
MOV A,B
ORL A,30H
MOV R3,A
MOV A,R2
ORL A,#30H
MOV R2,A
END
Cont.
• By using 60 Hz we can generate seconds, minutes,
hours.

• Note that on the first round, it starts from 0, since


on RESET, TLO = 0. To solve this problem, load
TLO with -60 at the beginning of the program.

You might also like