0% found this document useful (0 votes)
20 views25 pages

MicroprocessorBasedSystems Term-I Lec8 Timer Counter Programming1

The document discusses programming timers in mode 1 on the 8051 microprocessor in assembly language. It describes the timer registers TL0, TL1, TH0 and TH1 used to access the 16-bit timers 0 and 1. The TMOD register is used to set the timer modes and operations. Mode 1 allows loading a 16-bit value into the timer which then counts up to rollover and trigger the timer flag, allowing generation of time delays. Examples are given for calculating the hex values to load into the timer registers to achieve the desired delay.

Uploaded by

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

MicroprocessorBasedSystems Term-I Lec8 Timer Counter Programming1

The document discusses programming timers in mode 1 on the 8051 microprocessor in assembly language. It describes the timer registers TL0, TL1, TH0 and TH1 used to access the 16-bit timers 0 and 1. The TMOD register is used to set the timer modes and operations. Mode 1 allows loading a 16-bit value into the timer which then counts up to rollover and trigger the timer flag, allowing generation of time delays. Examples are given for calculating the hex values to load into the timer registers to achieve the desired delay.

Uploaded by

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

E1321

Microprocessor based Systems I

‘Timer/Counter Programming’

Dr. Ahmed El-Awamry

Date: 25.11.2023, Time: 9:00


Objective

▪ Be able to program the 8051 timers in Mode-


1 in assembly language.

© Dr. Ahmed El-Awamry 23.11.2023 2


Programming Timers

▪ The 8051 has two timers/counters, they can


be used either as
▪ Timers to generate a time delay or as
▪ Event counters to count events happening outside
the microcontroller
▪ Both Timer 0 and Timer 1 are 16 bits wide
▪ Since 8051 has an 8-bit architecture, each 16-bits
timer is accessed as two separate registers of low
byte and high byte

© Dr. Ahmed El-Awamry 23.11.2023 3


Timer 0 & 1 Registers

▪ Accessed as low byte and high byte


▪ The low byte register is called TL0/TL1 and
▪ The high byte register is called TH0/TH1
▪ Accessed like any other register
MOV TL0,#4FH
MOV R5,TH0

© Dr. Ahmed El-Awamry 23.11.2023 4


TMOD Register

▪ Both timers 0 and 1 use the same register,


called TMOD (timer mode), to set the various
timer operation modes
▪ TMOD is an 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

© Dr. Ahmed El-Awamry 23.11.2023 5


TMOD Register

© Dr. Ahmed El-Awamry 23.11.2023 6


TMOD Register

© Dr. Ahmed El-Awamry 23.11.2023 7


TMOD Register, Gate
▪ Timers of 8051 do starting and stopping 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

© Dr. Ahmed El-Awamry 23.11.2023 8


TMOD Register, Gate

© Dr. Ahmed El-Awamry 23.11.2023 9


Mode 1 Programming
▪ The following are the characteristics and
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
▪ 3) After the timer is started, it starts to count up
▪ It counts up until it reaches its limit of FFFFH

© Dr. Ahmed El-Awamry 23.11.2023 10


Mode 1 Programming
▪ 3) 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 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

© Dr. Ahmed El-Awamry 23.11.2023 11


Mode 1 Programming

▪ To generate a time delay


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
▪ Keep monitoring the timer flag (TF) with the JNB
4 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

© Dr. Ahmed El-Awamry 23.11.2023 12


Mode 1 Programming

© Dr. Ahmed El-Awamry 23.11.2023 13


Mode 1 Programming

© Dr. Ahmed El-Awamry 23.11.2023 14


Mode 1 Programming

Important

© Dr. Ahmed El-Awamry 23.11.2023 15


Mode 1 Programming

Important

© Dr. Ahmed El-Awamry 23.11.2023 16


Mode 1 Programming

© Dr. Ahmed El-Awamry 23.11.2023 17


Mode 1 Programming

© Dr. Ahmed El-Awamry 23.11.2023 18


Mode 1 Programming

© Dr. Ahmed El-Awamry 23.11.2023 19


Mode 1 Programming, Finding the Loaded Timer
Values
▪ To calculate the values to be loaded into the
TL and TH registers, look at the following
example
▪ Assume XTAL = 11.0592 MHz, we can use the
following steps for finding the TH, TL registers’
values
▪ 1) Divide the desired time delay by 1.085 us
▪ 2) Perform 65536 – n, where n is the decimal value we got
in Step1
▪ 3) Convert the result of Step2 to hex, where yyxx is the
initial hex value to be loaded into the timer’s register
▪ 4) Set TL = xx and TH = yy

© Dr. Ahmed El-Awamry 23.11.2023 20


Mode 1 Programming, Finding the Loaded Timer
Values

© Dr. Ahmed El-Awamry 23.11.2023 21


Mode 1 Programming, Finding the Loaded Timer
Values

Important

© Dr. Ahmed El-Awamry 23.11.2023 22


Mode 1 Programming, Finding the Loaded Timer
Values

© Dr. Ahmed El-Awamry 23.11.2023 23


Mode 1 Programming, Generating Large Time
Delay

© Dr. Ahmed El-Awamry 23.11.2023 24


Appendix

You might also like