0% found this document useful (0 votes)
32 views

COI1 Timers Counters in 8051

The document discusses timer/counter operation in the 8051 microcontroller. It describes the two timers/counters as 16-bit registers that can count up and generate overflows. The timers can be used for delay generation or as event counters. Key registers for timer/counter configuration and control include TMOD, TCON, TH1, TL1, TH0, and TL0. The document outlines the steps to program the timers, including calculating values based on the oscillator frequency, configuring the TMOD register, loading the timer registers, running the timer, and monitoring for overflow. Counter operation and examples of timer programming are also provided.

Uploaded by

Hi
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)
32 views

COI1 Timers Counters in 8051

The document discusses timer/counter operation in the 8051 microcontroller. It describes the two timers/counters as 16-bit registers that can count up and generate overflows. The timers can be used for delay generation or as event counters. Key registers for timer/counter configuration and control include TMOD, TCON, TH1, TL1, TH0, and TL0. The document outlines the steps to program the timers, including calculating values based on the oscillator frequency, configuring the TMOD register, loading the timer registers, running the timer, and monitoring for overflow. Counter operation and examples of timer programming are also provided.

Uploaded by

Hi
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/ 18

Microprocessors and Microcontrollers – 11EC311

Timer/Counter Operation in 8051


Introduction

● 2 Timers/Counters – 16 Bit
● Counts UP and Generates an Overflow after reaching Maximum
● Timer
– Delay Generator
– Uses Oscillator as Clock
● Counter
– Event Counter
– External Events are Counted
● SFR's
– TMOD,TCON, TH1,TL1,TH0,TL0

22/09/15 8051 Timer/Counter - Copyleft


TMOD Register

22/09/15 8051 Timer/Counter - Copyleft


TMOD Register – Timer Modes

22/09/15 8051 Timer/Counter - Copyleft


TMOD Register – Timer Modes
Overflow Value
1FFF

FFFF

FF

FF

22/09/15 8051 Timer/Counter - Copyleft


TCON Register

22/09/15 8051 Timer/Counter - Copyleft


Programming the Timer – Steps Involved

● Calculating the Time to run ONE Machine Cycle


– (1 Machine Cycle = 12 Clock Cycles)
● Calculating the value to be loaded in Timer
● Configuring TMOD Register
● Loading THx and TLx
● Running the Timer using TCON
● Waiting for an overflow in TCON
● Stopping the Timer

22/09/15 8051 Timer/Counter - Copyleft


Time Period to Run ONE Machine Cycle
Procedure Example
Oscillator Frequency (XTAL) XTAL = 12 Mhz
will be given, say f1.

As 8051 takes 12 clocks to XTAL/12 = 12/12


complete one instruction cycle, => F1= 1 Mhz
divide XTAL frequency with 12
i.e f1/12 ..Which is lets say F1

Time period is inverse of T1= 1/F1 = 1/1 = 1us


frequency i.e 1/F1... Which is
lets say T1

So the time taken to run one Time taken to run one opcode
byte of opcode is T1 is 1 micro second
22/09/15 8051 Timer/Counter - Copyleft
Count to be given for Timer –
Case 1(Time Period Given to Generate)

Procedure Example

Time T2 will be given , say T2 T2 = 10us

Dividing the time period to run the T2/T1 = 10us/1us


timer(T2) with the time of one
instruction cycle(T1)
T2/T1

Resultant rounded off INTEGER part is Count for the Timer = 10


the COUNT to the timer registers

22/09/15 8051 Timer/Counter - Copyleft


Count to be given for Timer –
Case 2(Frequency Given to Generate)
Procedure Example
Frequency will be given, say F2 = 0.1 Mhz
F2

Time period is inverse of T2= 1/F2 = 1/0.1Mhz = 10us


frequency i.e 1/F2... Which is
lets say T2

Dividing the time period which T2/T1 = 10us/1us


we want to run the timer(T2)
with the time period taken to
run one instruction cycle(T1)

Resultant rounded off Count for the Timer = 10


INTEGER part is the COUNT
for timer

22/09/15 8051 Timer/Counter - Copyleft


Value To Be Loaded In Timer Registers

● Timers work UPwards, hence count is to be deducted


from Overflow values(Depends on Mode)
Method 1(Decimal Way) Method 2(HexaDecimal Way)
Overflow Value(d) – Count(d) Overflow Value(h) – Count(h)+1
Ex: Ex:
Mode 1: 65536 – 10 Mode 1: FFFF – A + 1
Mode 2: 256-10 Mode 2: FF – A + 1
Convert Resultant Decimal to Hex Ex:
Ex: Mode 1: FFF6
Mode 1: FFF6 Mode 2: F6
Mode 2: F6
Store to THx and TLx Respectively Store to THx and TLx Respectively
Ex: Timer 0 Ex: Timer 1
Mode 1: TH0 = FF TL0= F6 Mode 1: TH1 = FF TL1 = F6
Mode 2: TH0 = F6 Mode 2: TH1 = F6
22/09/15 8051 Timer/Counter - Copyleft
Assembly Programming of Timer - Steps

● Clear Timer Flag (TFx) and TRx


● Load TMOD value calculated
● Load TLx and THx registers with the values
● Run the timer by SETB operation on TRx
● Keep Monitoring Timer Flag(TFx) until it is raised with
“label: JNB TFx,label”
● Stop the timer by clearing TRx & Clear Timer Flag (TFx)

22/09/15 8051 Timer/Counter - Copyleft


Assembly Programming of Timer - Example
Assume XTAL = 11.0592 MHz, Program to generate a delay of 10ms using Timer 1
Solution:
(a) 10ms(Delay) /1.085us(Machine Cycle) = 9216d =>65536d –9216d =56320d=DC00H
(b) TL1 = 00h and TH1 = DCh
(c) We are using 16 bit mode of Timer 1 , so TMOD = 10h
Assembly Program for Timer:
CLR TR1
CLR TF1
MOV TMOD,#10H ;Timer 1, mod 1
MOV TL1,#00 ;TL1=00,low byte of timer
MOV TH1,#0DCH ;TH1=DC, the high byte
SETB TR1 ;Start timer 1
BACK: JNB TF1,BACK ;until timer rolls over
CLR TR1 ;Stop the timer 1
CLR TF1
22/09/15 8051 Timer/Counter - Copyleft
Assembly Programming of Counter - Steps

● Clear Timer Flag (TFx) and TRx


● Load TMOD value calculated
● Load TLx and THx registers with 00h (Count starts at 0)
● Run the timer by SETB operation on TRx
● Whenever Pulse is generated in Tx(T0 or T1) pin
automatically Timer Registers are incremented
● Stop the timer by clearing TRx if required ( You may use
another Timer to set the amount of time counter should run)
● Copy counter values available in THx , TLx if necessary

22/09/15 8051 Timer/Counter - Copyleft


Programs on Timers/Counters for LTC

1) Explain Configuration of Timers if TMOD = 52h

2) XTAL=12Mhz
MOV TMOD,#01
MOV TL0,#0F0h
MOV TH0,#0FF0h
SETB TR0
After how much time will the timer 0 Flag be set?

22/09/15 8051 Timer/Counter - Copyleft


Programs on Timers/Counters for LTC

3) Using Timer in Mode 2 generate a delay of 92us?

4) Use Timer 1 to generate square wave of 5Khz on P1.1 ?

5) Using Timer in Mode 1 generate a delay of 1 Second?

6) Count Number of events occured during 1 second using


Counter 0 ? (Hint: Use Timer and Counter Both)

22/09/15 8051 Timer/Counter - Copyleft


References

● https://www.sites.google.com/site/sripathroykoganti/my-forms

● The 8051 Microcontroller, 3rd Edition, Ayala, CENGAGE


Learning
● Microcontrollers[Theory and Applications], Ajay V
Deshmukh, Tata McGraw Hill
● The 8051 Microcontroller and Embedded Systems,
Muhammad Ali Mazidi, Pearson Education

22/09/15 8051 Timer/Counter - Copyleft


Thank You

22/09/15 8051 Timer/Counter - Copyleft

You might also like