Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Module 3 - Timer

Uploaded by

Lokesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Module 3 - Timer

Uploaded by

Lokesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

8051 Timer

Programming in
Assembly and C
Dr. Lokesh M
Dept. of EEE, NIEIT

Intelligence Plus Character


-that is the goal of true education…
TABLE OF
CONTENTS
01 Timer Programming
02 Counter
Programming
03 Programming timer 0 and
1 in 8051 C
8051 Timer
there are two 16-bit timer registers. These registers are known as Timer0 and Timer1. The timer
registers can be used in two modes. These modes are Timer mode and the Counter mode.
In the timer mode, the internal machine cycles are counted.
In the counter mode, the external events are counted..
TMOD Register (timer mode)
TCON Register
Mode 1 Programming
The following are the characteristics and operations of mode 1:
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..
3. After the timer is started, it starts to count up. It counts up until it reaches its limit of FFFFH. 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 instructions CLR TR0 or CLR TR1, for timer 0 and timer 1,
respectively.
4. 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.
Steps to Program in
mode 1

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,#10H ;Timer 1, mod 1 a) T = 1 / 50 = 20 ms, the period of square wave.


AGAIN: MOV TL1,#00 ;TL1=00,low byte of timer (b) 1 / 2 of it for the high and low portion of the pulse is
MOV TH1,#0DCH ;TH1=DC, the high byte 10 ms.
SETB TR1 ;Start timer 1 (c) 10 ms / 1.085 us = 9216 and 65536 – 9216 = 56320
BACK: JNB TF1,BACK ;until timer rolls over in decimal, and in hex it is
CLR TR1 ;Stop the timer 1 DC00H.
CLR P2.3 ;Comp. p2.3 to get high and (d) TL = 00 and TH = DC (hex).
low
SJMP AGAIN ;Reload timer
;mode 1 isn’t auto-reload
Mode 2 Programming
The following are the characteristics and operations of mode 2:
1. It is an 8-bit timer; therefore, it allows only values of 00 to FFH to be loaded into the timer’s
register TH
2. After TH is loaded with the 8-bit value, 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
3. After the timer is 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 (timer flag)
4. 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 the TH register
 To repeat the process, we must simply clear TF and let it go without any need by the programmer to
reload the original value
 This makes mode 2 an auto-reload, in contrast with mode 1 in which the programmer has to reload
TH and TL
Steps to program in mode 2:
To generate a time delay
1. Load the TMOD value register indicating which timer (timer 0 or timer 1) is to be used, and
the timer mode (mode 2) is selected.
2. Load the TH registers with the initial count value.
3. Start timer.
4. Keep monitoring the timer flag (TF) with the JNB TFx, target instruction to see whether it is
raised. Get out of the loop when TF goes high.
5. Clear the TF flag and
6. Go back to Step 4, since mode 2 is auto reload .
Assume XTAL = 11.0592 MHz, find the frequency of the square wave generated on pin P1.0 in the
following program

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.

C/T bit in TMOD 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

Despite being red,


2010–
Mars is a cold
place
02 2012

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 MERCURY MARS JUPITER


Venus is the Mercury was Despite being It’s the biggest
second planet named after a red, Mars is a planet in the
from the Sun Roman god cold place Solar System
COMPUTER SKILLS

VENUS MARS

Venus has a beautiful name, Despite being red, Mars is a


but it’s very hot cold place

MERCUR
Y JUPITER

Mercury is the closest planet It’s the biggest planet in the


to the Sun Solar System
VOLUNTEER WORK

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

ADDRESS Your address here

PHONE 001 664 123 4567

CELL PHONE 001 664 123 4567

EMAIL Email address here


THANKS!
Do you have any questions?
addyouremail@freepik.com
+91 620 421 838
yourcompany.com

CREDITS: This presentation template was created by


Slidesgo, including icons by Flaticon, infographics &
images by Freepik

Please keep this slide for attribution


This slide is only for Premium users
This slide is only for Premium users
This slide is only for Premium users
This slide is only for Premium users
Instructions for use
In order to use this template, you must credit Slidesgo by keeping the Thanks slide.

You are allowed to:


- Modify this template.
- Use it for both personal and commercial projects.

You are not allowed to:


- Sublicense, sell or rent any of Slidesgo Content (or a modified version of Slidesgo Content).
- Distribute Slidesgo Content unless it has been expressly authorized by Slidesgo.
- Include Slidesgo Content in an online or offline database or file.
- Offer Slidesgo templates (or modified versions of Slidesgo templates) for download.
- Acquire the copyright of Slidesgo Content.

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)

#0c2e3a #018790 #1dcdc3 #ffa73b #ff823b #f5340b #78001b #ffd497 #434343


Storyset by Freepik
Create your Story with our illustrated concepts. Choose the style you like the most, edit its colors, pick
the background and layers you want to show and bring them to life with the animator panel! It will boost
your presentation. Check out How it Works.

Pana Amico Bro Rafiki Cuate


Use our editable graphic resources...
You can easily resize these resources without losing quality. To change the color, just ungroup the resource
and click on the object you want to change. Then, click on the paint bucket and select the color you want.
Group the resource again when you’re done. You can also look for more infographics on Slidesgo.
JANUARY FEBRUARY MARCH APRIL MAY JUNE

PHASE 1

Task 1

Task 2

PHASE 2

Task 1

Task 2

JANUARY FEBRUARY MARCH APRIL

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

You might also like