Module 3 - Timer
Module 3 - Timer
Programming in
Assembly and C
Dr. Lokesh M
Dept. of EEE, NIEIT
To generate a time delay, using timer in mode 1, following are the steps:
1. Load the TMOD value register indicating which timer (timer 0 or timer 1) is to be used and which
timer mode (0 or 1) is selected.
2. Load registers TL and TH with initial count value.
3. Start the timer.
4. Keep monitoring the timer flag (TF) with the JNB TFx, target instruction to see if it is raised. Get out
of the loop when TF becomes high.
5. Stop the timer.
6. Clear the TF flag for the next round.
7. Go back to Step 2 to load TH and TL again.
In the following program, we create a square wave of 50% duty cycle (with equal portions high and
low) on the P1.5 bit. Timer 0 is used to generate the time delay. Analyze the program. Also calculate the
delay generated. Assume XTAL=11.0592MHz.
In the above program notice the following
MOV TMOD,#01 ;Timer 0, mode 1(16-bit mode)
step.
HERE: MOV TL0,#0F2H ;TL0=F2H, the low byte 1. TMOD is loaded.
2. FFF2H is loaded into TH0-TL0.
MOV TH0,#0FFH ;TH0=FFH, the high byte
3. P1.5 is toggled for the high and low
CPL P1.5 ;toggle P1.5 portions of the pulse.
4. The DELAY subroutine using the timer
ACALL DELAY
is called.
SJMP HERE 5. In the DELAY subroutine, timer 0 is
started by the SETB TR0 instruction.
DELAY:
6. Timer 0 counts up with the passing of
SETB TR0 ;start the timer 0 each clock, which is provided by the
crystal
AGAIN: JNB TF0,AGAIN ;monitor timer flag 0 until it rolls over
CLR TR0 ;stop timer 0
CLR TF0 ;clear timer 0 flag
RET
Find the delay generated by timer 0 in the following code, using hex as well
as decimal method. Do not include the overhead due to instruction.
CLR P2.3 ;Clear P2.3 (a) (FFFFH – B83E + 1) = 47C2H = 18370 in decimal
MOV TMOD,#01 ;Timer 0, 16-bitmode and 18370 × 1.085 us =
HERE: MOV TL0,#3EH ;TL0=3Eh, the low byte 19.93145 ms
MOV TH0,#0B8H ;TH0=B8H, the high byte (b) Since TH – TL = B83EH = 47166 (in decimal) we
SETB P2.3 ;SET high timer 0 have 65536 – 47166 = 18370. This
SETB TR0 ;Start the timer 0 means that the timer counts from B38EH to FFFF. This
AGAIN: JNB TF0,AGAIN ;Monitor timer flag 0 plus Rolling over to 0 goes
CLR TR0 ;Stop the timer 0 through a total of 18370 clock cycles, where each clock
CLR TF0 ;Clear TF0 for next round is 1.085μs in duration.
CLR P2.3 Therefore, we have 18370 × 1.085 us = 19.93145 ms as
the width of the pulse.
Assume that XTAL = 11.0592 MHz. What value do we need to load the timer’s register if we want to
have a time delay of 5 ms? Show the program for timer 0 to create a pulse width of 5 ms on P2.3.
CLR P2.3 ;Clear P2.3 Since XTAL = 11.0592 MHz, the counter counts up
MOV TMOD,#01 ;Timer 0, 16-bitmode every 1.085 us. This means that out of many 1.085 us
HERE: MOV TL0,#0 ;TL0=0, the low byte intervals we must make a 5 ms pulse. To get that, we
MOV TH0,#0EEH ;TH0=EE, the high byte divide one by the other. We need 5 ms / 1.085μs = 4608
SETB P2.3 ;SET high P2.3 clocks. To Achieve that we need to load into TL and TH
SETB TR0 ;Start timer 0 the value 65536 – 4608 = EE00H. Therefore, we have
AGAIN: JNB TF0,AGAIN ;Monitor timer flag 0 TH = EE and TL = 00.
CLR TR0 ;Stop the timer 0
CLR TF0 ;Clear timer 0 flag
Assume that XTAL = 11.0592 MHz, write a program to generate a square wave of 2 kHz frequency on
pin P1.5.
MOV TMOD,#01 ;Timer 0, 16-bitmode This is similar to previous Example, except that we
AGAIN: MOV TL1,#1AH ;TL1=1A, low byte of timer must toggle the bit to generate the square wave. Look at
MOV TH1,#0FFH ;TH1=FF, the high byte the following steps.
SETB TR1 ;Start timer 1 (a) T = 1 / f = 1 / 2 kHz = 500 us the period of square
BACK: JNB TF1,BACK ;until timer rolls over wave.
CLR TR1 ;Stop the timer 1 (b) 1 / 2 of it for the high and low portion of the pulse is
CLR P1.5 ;Clear timer flag 1 250 us.
CLR TF1 ;Clear timer 1 flag (c) 250 us / 1.085 us = 230 and 65536 – 230 = 65306
SJMP AGAIN ;Reload timer which in hex is FF1AH.
(d) TL = 1A and TH = FF, all in hex.
Assume XTAL = 11.0592 MHz, write a program to generate a square wave
of 50 kHz frequency on pin P2.3.
MOV TMOD, #20H ; T1/8-bit/auto reload First notice the target address of SJMP. In mode 2 we
MOV TH1, #5 ; TH1 = 5 do not need to reload TH since it is auto-reload.
SETB TR1 ; start the timer 1 Now (256 - 05) × 1.085 us = 251 × 1.085 us = 272.33
BACK: JNB TF1, BACK ; till timer rolls over us is the high portion of the pulse. Since it is a 50%
CPL P1.0 ; P1.0 to high, low duty cycle square wave, the period T is twice that; as a
CLR TF1 ; clear Timer 1 flag result
SJMP BACK ; mode 2 is auto-reload T = 2 × 272.33 us = 544.67 us and the frequency =
1.83597 kHz
Write an ALP to generate a square wave of frequency 72Hz on pin P1.0.
MOV TMOD, #2H ; Timer 0, mod 2;(8-bit, auto reload) Assume XTAL=11.0592MHz. With TH=00,
MOV TH0, #0 the delay generated is 256 x 1.085 μs = 277.76 μs.
AGAIN: MOV R5, #250 ; multiple delay count therefore to generate a delay of (1 / 72) = 138.88ms,
ACALL DELAY the count to be loaded is 250 x 2=500.
CPL P1.0 That is T = 2 (250 × 256 × 1.085 μs) = 138.88ms, and
SJMP AGAIN frequency = 72 Hz
DELAY: SETB TR0 ; start the timer 0
BACK: JNB TF0,BACK ; stay timer rolls over
CLR TR0 ; stop timer
CLR TF0 ; clear TF for next round
DJNZ R5,DELAY
RET
Counter Programming
Timers can also be used as counters, counting 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 register.
The C/T bit in the TMOD registers decides the source of the clock for the timer.
When C/T = 1, the timer is used as a counter and gets its pulses from outside the 8051.
The counter counts up as pulses are fed from pins 14 and 15, these pins are called T0
(timer 0 input) and T1 (timer 1 input).
Assuming that clock pulses are fed into pin T1, write a program for counter 1 in mode 2 to count the
pulses and display the state of the TL1 count on P2, which connects to 8 LEDs..
MOV TM0D,#01100000B ;counter 1, mode 2,C/T=1 Notice in the above program the role of the instruction
MOV TH1,#0 ;clear TH1 SETB P3.5.
SETB P3.5 ;make T1 input Since ports are set up for output when the 8051 is
AGAIN: SETB TR1 ;start the counter powered up, we make P3.5 an input port by making it
BACK: MOV A,TL1 ;get copy of TL high. In other words, we must configure (set high) the
MOV P2,A ;display it on port 2 T1 pin (pin P3.5) to allow pulses to be fed into it.
JNB TF1,Back ;keep doing, if TF = 0
CLR TR1 ;stop the counter 1
CLR TF1 ;make TF=0
SJMP AGAIN ;keep doing it
If GATE = 1, the start and stop of the timer are done
externally through pins P3.2 and P3.3 for timers 0 and 1,
respectively. This hardware way allows starting or stopping
the timer externally at any time via a simple switch
2008– Venus has a EXPERIENCE
2010 01 beautiful name, but
it’s hot
Mercury is the
2012–
2015 03 closest planet to
the Sun
Jupiter is a gas
2015–
giant and the
biggest planet
04 2019
JOB POSITIONS
COMPANY
NAME HERE 2010-2018
Job / position title here
● List your responsibilities for this job
● Adapt this to your needs
● Try to keep it short
COMPANY
NAME HERE 2006-2010
Job / position title here
● List your responsibilities for this job
● Adapt this to your needs
● Try to keep it short
EDUCATION
2008–2010 2012–2015
Venus has a beautiful name, but Mercury is the closest planet
it’s terribly hot to the Sun
01 02 03 04
2010–2012 2015–2019
Despite being red, Mars is a Jupiter is a gas giant and
cold place the biggest planet
INSTITUTIONS
INSTITUTION
NAME HERE 2000-2006
Principal subjects / occupational skills covered:
● List the subjects here
● Sort them by relevance
● Adapt this to your needs
● Try to keep it short
● Get straight to the point
COMMUNICATION
SKILLS
COMMUNICATION
SKILLS
Mother tongue(s) Other language(s)
● Language 1 Language 1
● Language 2 Language 2
Language 3
TECHNICAL SKILLS
VENUS MARS
MERCUR
Y JUPITER
VENUS MARS
Venus has a beautiful Despite being red, Mars is
name, but it’s very hot a cold place
MERCURY JUPITER
Mercury is the closest It’s the biggest planet in
planet to the Sun the Solar System
HOBBIES
SIGHTSEE
CHESS TRAVEL ING MUSIC
It’s the second Mercury is the Despite being It’s the biggest
planet from the closest to the Sun red, Mars is a planet in the
Sun cold place Solar System
CONTACT
For more information about editing slides, please read our FAQs:
https://slidesgo.com/faqs
Fonts & colors used
This presentation has been made using the following fonts:
Arvo
(https://fonts.google.com/specimen/Arvo)
Barlow Condensed
(https://fonts.google.com/specimen/Barlow+Condensed)
PHASE 1
Task 1
Task 2
PHASE 2
Task 1
Task 2
PHASE
1
Task 1
Task 2
...and our sets of editable icons
You can resize these icons without losing quality.
You can change the stroke and fill color; just select the icon and click on the paint bucket/pen.
In Google Slides, you can also use Flaticon’s extension, allowing you to customize and add even more icons.
Educational Icons Medical Icons
Business Icons Teamwork Icons
Help & Support Icons Avatar Icons
Creative Process Icons Performing Arts Icons
Nature Icons
SEO & Marketing Icons