SlideShare a Scribd company logo
2/4/2023 1
Timer
2/4/2023 2
Timer / counter programming in the 8051:
The 8051 has two timerscounters. They
can be used either as timers to generate
time delay or as counters to count events
happening outside the micro computer.
Now we shall see how they are
programmed. .
2/4/2023 3
Programming 8051 timers:
8051 has two timers, timer 0 & timer 1. this module
has two 16bit registers. T0 and T1 registers. These
registers can be configured to operate either as timers or
event counters.
In the ‘timer’ function. The register is incremented
every machine cycle. Thus, one can think it as counting
machine cycles. Since a machine cycle consists of 12
oscillator periods, the count rate is 1/12 of the oscillator
frequency.
2/4/2023 4
The 16 bit register of T0 / T1 is accessed as low
byte and high byte (TH0 / TH1)
THO TLO
DB15 DB0
2/4/2023 5
GATE C/T’ M1 M0 GATE C/T’ M1 M0
TMOD register:
Both timers t0 and t1 use the same register called TMOD to set the
various timer operating modes.
2/4/2023 6
Gate: Gating control when set timer/counter is enabled only while the
INTx pin is high and TRx pin is set. When cleared, the timer is enabled
whenever the TRx control bit is set.
C / T :Timer / Counter selected cleared for timer application( c input
from interval system clock). Set for counter operation( input from Tx
input pin).
M1, M0 MODE BITS
M1 M0 MODE OPERATING MODE
0 0 0 13 bit timer mode
0 1 1 16 bit timer mode
1 0 2 8 bit timer mode
1 1 3 split timer mode
2/4/2023 7
oscillator
12
Source of the clock:
if C/T =0 the crystal frequency attached to the 8051 is the source
of the clock for the timer.
Find the timer’s clock frequency and its period for various 8051
based systems with the following crystal frequency.
1) 12 MHZ
2) 16 MHZ
11.0592 MHZ
2/4/2023 8
The frequency for the timer is always 1/12th of the
frequency of crystal attached to the 8051
1) 1/12 X 12 MHZ =1MHZ and T=1/1MHZ
=1s.
2) 1/12 X 16 MHZ=1.33MHZ and
T=1/1.33MHZ=0.75s
3) 1/12 X 11.0592 MHZ=921.6KHZ and
T=1/921.6kHz=1.085s
2/4/2023 9
Influence of gate:
It is to start or stop the timer, using hardware or
software control.
SETB TR1, CLR TR1 for timer 1 and
SETB TR0,CLR TR0 for timer 0 instruction start
or stop timers as long as gate =0in TMOD
register.
The use of external hardware to stop or start the
timer is discussed later.
2/4/2023 10
Mode 0:
This mode is compatible for MCS 48
family. The 13 bit counter can hold values
between 0000 to 1FFF in TH and TL
registers. Therefore, when the timer
reaches its maximum of 1FFF it rolls over
to 0000 and TF is raised.
2/4/2023 11
Mode 1:
The characteristics of this mode are as follows:
1) it is a 16 bit timer. It allows values of 0000 to FFFFH into
timer register TH and TL.
2) After TH and TL are loaded with 16 bit initial value. The
timer must be started. This is done by SETB TR0 for SETB TR1
for T1.
3) After timer is started, it starts to count up. It counts up to
FFFFH . when it rolls over from FFFFH to 0000, it sets a flag bit
timer flag(TF). This flag can be monitored. When it is raised,
one option is to stop the timer with the instruction CLR TR0 or
CLR TR1.
4)After the timer reaches its limit and rolls over, in order to
repeat the process the register TH and TL must be reloaded with
the original value and TF must be reset to 0.
2/4/2023 12
Xtal
osc
12
TR
C/T’
TF goes high when
FFFF
TH TL TF
Over flow
flag
2/4/2023 13
Mode 1 programming :
To generate a time delay, using time mode, the following steps
are taken:
 load the TMOD value register indicating which timer(T0 0r
T1) is to be used and which timer mode is selected.
 Load register TL and TH with initial count values.
 Start the timer.
 Keep monitoring the status of timer flag (TF). Come out of
the loop, when TF becomes HIGH.
 Stop the timer.
 Clear the TF flag for the next count.
 Go back to step 2.
.
2/4/2023 14
NAME EXAMPLE 11
ORG 0H
MOV TMOD,#01H
HERE: MOV TL0, #0F2 H 2
MOV TH0, #OFFH 2
CPL P1.5 1
ACALL DELAY 2
SJMP HERE 2
DELAY: SETB TR0 1
AGAIN: JNB TF0,AGAIN 14
CLR TR0 1
CLR TF0 1
RET 1
27
2/4/2023 15
In the above program, observe the following:
 TMOD is loaded.
 FFF2H is loaded into TH0-TL0.
 P1.5 is toggled for the high and low portions of the pulse.
 The DELAY subroutine , timer0 is started by SETB TR0
instruction.
 Timer0 counts up with the passing of each clock. As timer
counts up, it goes through FFF3, ….., FFFF, 0000 states . now
TF0 becomes HIGH.
T0 is stopped by the instruction CLR TR0.And the process is
repeated.
2/4/2023 16
FFF2 FFF3 FFF4 FFFF 0000
TF=0 TF=0 TF=0 TF=0 TF=1
2/4/2023 17
Delay calculation:
[( FFFF – YYXX) +1] * 1.085 s
The delay calculations for the above example are as follows.
for the entire period,
T=2*27*1.085s=58.59s
Problem: Find the delay generated by T0 in the following code.
CLR P2.3
MOV TMOD,#01H
HERE: MOV TL0,#3EH
MOV TH0,#0B8H
SETB P 2.3
SETB TR0
AGAIN: JNB TF0, AGAIN
CLR TR0
CLR TF0
CLR P2.3
2/4/2023 18
• NAME EXAMPLE 12
ORG 0000 H
MOV TMOD, #10 H
AGAIN: MOV TL1, #34 H
MOV TH1, #76 H
SETB TR1
BACK: JNB TF1, BACK
CLR TR1
CPL P1.5
CLR TF1
SJMP AGAIN
2/4/2023 19
Explanation:
The above program generates a square wave on pin P1.5
continuously using timer 1 for a time delay. find the frequency
of the square wave if XTAL=11.0592MHZ . in your
calculation, do not include the OVERHEAD due to
instructions in the loop.
Since FFFFh- 634h=89CBh+1=89CCh and 89CCH=35276
clock count and 35276X1.0875s=38.274ms
2/4/2023 20
Problem :
To find values of TH and TL registers, once the amount of time
delay is known.
Assuming that XTAL = 11.0592Mhz write a program to generate a
square wave of 2Khz frequency on pin P1.5.
Assuming XTAL= 11.0592 MHZ, the following steps are to
be followed.
1) Divide the desired time delay by 1.085s.
2) Perform 65536-n where n is the decimal value we got in step1.
3) Convert the result of step to hex.
4) Set TL and TH registers accordingly.
2/4/2023 21
Assume that XTAL =11.0592 MHZ, what value do we need to load into
the timer’s register, to have a delay of 5ms; show the program for timer 0
to create a pulse width of 5 ms on P2.3.
Step 1: 5 ms / 1.085 s= 4608 clocks.
2. 65536 – 4608 = 60228.
3. EE00H
4. TH : EE & TL :00H.
2/4/2023 22
PROGRAM:
NAME EXAMPLE-13
ORG 0H
CLR P2.3
MOV TMOD,#01H
HERE: MOV TL0, #00H
MOV TH0, #0EEH
SETB P2.3
SETB TR0
AGAIN: JNB TF0,AGAIN
CLR TR0
CLR TF0.
SJMP HERE.
2/4/2023 23
Xtal
osc
12
TR
C/T’
TF goes high
when
FFFF
TL TF
Over flow flag
TH
2/4/2023 24
The characteristics of mod 2 are as follows:
1) It is an 8-bit timer and it allows values of 00h to FFH.
2) After TH is loaded with the 8 bit value, 8051 gives a copy of it to
TL. Then the timer must be started. This is done by the instruction
‘SETB TR0’ for T0 and ‘SETB TR1’ for T1.
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 H; it sets high the TF(timer flag). If we are using
timer 0, TF0 goes high. If we are using T/C TF1 is raised.
4) When the TL register roll from FFH to 0 and TF is set to 1. TL is
reloaded automatically with the original value kept at TH register. To
repeat the process, we must simpler clear TF and automatically reloads
the originally values.
In auto reload, TH is loaded with the initial count and a cop0y of
it is given to TL. This reloading leaves TH unchanged still holding a
copy of original value.
2/4/2023 25
Mode 2 programming:
NAME EXAMPLE14
ORG 0000H
MOV TMOD,#2H ;
MOV TH0,#0H
AGAIN: MOV R5,#250
ACALL Delay
CPL P1.0
SJMP AGAIN
Delay: SETB TR0
Back: JNB TF0, Back
CLR TR0
CLR TF0
DJNZ R5, Delay
RET
END
In the above program, time period is, T= 2(250*256*1.085µS)
= 138.8ms
2/4/2023 26
Counter programming:
In the counter function, the register T0 and T1 are incremented in
response to a 1 to 0 transition at its external inputs T0, T1.In this
function , the external input is sampled during S5P2 of every machine
cycle.
When the samples show a high in one cycle and a low in the next cycle,
the count is incremented.
The new count value appears in the register during S3P1 of the cycle
following the one in which the transition was detected.
Since it takes two machine cycles( 24 oscillator periods) to recognize a 1
to 0 transition.
The maximum count rate is 1/24 of the oscillator frequency. It is to
ensure that given level is sampled at least once before it changes, it
should be held for at least one machine cycle.
P3.4 is used as T0, timer/counter 0 external input.
P3.5 is used as T1, timer/counter 0 external input.
2/4/2023 27
Example –15:
.
Q. Assuming that clock pulses are fed in to pin T1, write a
program for counter 1 in mode 2 to count the pulses and display
the state of TL1 count on P2.
Solution:
MOV TMOD,#01100000B ;counter 1, mode 2, C/T=1
;external pulses
MOV TH1, #0 ;clear TH1
SETB P3.5 ; make T1 input
AGAIN:SETB TR 1 ; start the counter
BACK: MOV A, TL1 ; get copy of count TL1
MOV P2, A ; display it on port 2
JNB TF1, BACK ; keep doing it if TF=0
CLR TR 1 ;stop the counter 1
CLR TF 1 ;make TF=0
SJMP AGAIN ; keep doing it
2/4/2023 28
Notice in the above program the role of the instruction
“SETB P3.5”. Since ports are set up for output when 8051
is powered up, we make P3.5 an input port by making it
high. In other words, we must configure (set high) the T1
pin (Pin3.5) to allow pulses to be fed into it.
TF0 goes high when
Th fromFFFF to 0000
TH0 TL0 TF0
T0
P3.4
2/4/2023 29
Equivalent instructions for the timer control
registers
FOR TIMER 0
SETB TR0 = SETB TCON.4
CLR TR0 = CLR TCON.4
SETB TF0 = SETB TCON.5
CLR TF0 = CLR TCON.5
FOR TIMER 1
SETB TR1 = SETB TCON.6
CLR TR1 = CLR TCON.6
SETB TF1 = SETB TCON.7
CLR TF1 = CLR TCON.7
TCON : Timer/Counter Control Register
TF 1 TR1 TF 0 TR 0 IE 1 IT 1 IE 0 IT 1
2/4/2023 30
The case of gate = 1 in TMOD:
So far, the task of timer is started through software using
SETB TR0 and TR1 instruction.
If gate bit in TMOD is set to 1,the start and stop of a the
timer are done externally through pins P3.2 and P3.3 of T0 &
T1.
This way of operation has several application.
For example, assume that on 8051 system is used to sound an
alarm every second using T0, in addition to many things. A
switch can be connected to pin 3.2 , which can be used to turn
on and off the timer, there by shutting down the alarm.
2/4/2023 31
XTAL
oscillator % 12
O
O
O
C/ T=0
C/ T =1
T 0 Pin
Pin 3.4
TR 0
Gate
INT0 Pin
Pin3.2

More Related Content

PPT
8051 Timer
PDF
9 timer programming
PPT
8051 ch9
PPTX
Micro c lab7(timers)
PPTX
5-Timer Mode 2 Programming-18-03-2024.pptx
PPT
lecture 12 counter_microcontroller2.ppt
PPTX
Timer programming for 8051 using embedded c
PPTX
Microcontrollers-MODULE4.pptx
8051 Timer
9 timer programming
8051 ch9
Micro c lab7(timers)
5-Timer Mode 2 Programming-18-03-2024.pptx
lecture 12 counter_microcontroller2.ppt
Timer programming for 8051 using embedded c
Microcontrollers-MODULE4.pptx

Similar to 4.Timer_1.ppt (20)

PPTX
5th unit embedded system and iot design timer and controller
PPT
UNIT-5.ppt
PPT
Programming 8051 Timers
PPT
MICROCONTROLLER TIMERS.ppt
PPTX
Timer programming
PPT
8051 ch9-950217
PPTX
Microprocessor Week 9: Timer and Counter
PPTX
6-Interrupts Programming-27-03-2024.pptx
PPTX
8051 MICROCONTROLLER TIMER AND ITS APPLICATIONS
PPT
MICROCONTROLLER TIMERS.GHGHGHGHGHGHGHGHGppt
PPTX
8051 timer counter
PDF
8051 timers--2
PPT
Microcontroller 8051 timer and counter module
PPTX
8051 Timers and Counters
PPTX
Timers
PPT
12 mt06ped007
PDF
PDF
Timer And Counter in 8051 Microcontroller
PPTX
8051 timer counter
5th unit embedded system and iot design timer and controller
UNIT-5.ppt
Programming 8051 Timers
MICROCONTROLLER TIMERS.ppt
Timer programming
8051 ch9-950217
Microprocessor Week 9: Timer and Counter
6-Interrupts Programming-27-03-2024.pptx
8051 MICROCONTROLLER TIMER AND ITS APPLICATIONS
MICROCONTROLLER TIMERS.GHGHGHGHGHGHGHGHGppt
8051 timer counter
8051 timers--2
Microcontroller 8051 timer and counter module
8051 Timers and Counters
Timers
12 mt06ped007
Timer And Counter in 8051 Microcontroller
8051 timer counter
Ad

Recently uploaded (20)

PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
additive manufacturing of ss316l using mig welding
PPTX
web development for engineering and engineering
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Digital Logic Computer Design lecture notes
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Well-logging-methods_new................
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
CH1 Production IntroductoryConcepts.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Automation-in-Manufacturing-Chapter-Introduction.pdf
Model Code of Practice - Construction Work - 21102022 .pdf
bas. eng. economics group 4 presentation 1.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
additive manufacturing of ss316l using mig welding
web development for engineering and engineering
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Digital Logic Computer Design lecture notes
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
CYBER-CRIMES AND SECURITY A guide to understanding
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Well-logging-methods_new................
Ad

4.Timer_1.ppt

  • 2. 2/4/2023 2 Timer / counter programming in the 8051: The 8051 has two timerscounters. They can be used either as timers to generate time delay or as counters to count events happening outside the micro computer. Now we shall see how they are programmed. .
  • 3. 2/4/2023 3 Programming 8051 timers: 8051 has two timers, timer 0 & timer 1. this module has two 16bit registers. T0 and T1 registers. These registers can be configured to operate either as timers or event counters. In the ‘timer’ function. The register is incremented every machine cycle. Thus, one can think it as counting machine cycles. Since a machine cycle consists of 12 oscillator periods, the count rate is 1/12 of the oscillator frequency.
  • 4. 2/4/2023 4 The 16 bit register of T0 / T1 is accessed as low byte and high byte (TH0 / TH1) THO TLO DB15 DB0
  • 5. 2/4/2023 5 GATE C/T’ M1 M0 GATE C/T’ M1 M0 TMOD register: Both timers t0 and t1 use the same register called TMOD to set the various timer operating modes.
  • 6. 2/4/2023 6 Gate: Gating control when set timer/counter is enabled only while the INTx pin is high and TRx pin is set. When cleared, the timer is enabled whenever the TRx control bit is set. C / T :Timer / Counter selected cleared for timer application( c input from interval system clock). Set for counter operation( input from Tx input pin). M1, M0 MODE BITS M1 M0 MODE OPERATING MODE 0 0 0 13 bit timer mode 0 1 1 16 bit timer mode 1 0 2 8 bit timer mode 1 1 3 split timer mode
  • 7. 2/4/2023 7 oscillator 12 Source of the clock: if C/T =0 the crystal frequency attached to the 8051 is the source of the clock for the timer. Find the timer’s clock frequency and its period for various 8051 based systems with the following crystal frequency. 1) 12 MHZ 2) 16 MHZ 11.0592 MHZ
  • 8. 2/4/2023 8 The frequency for the timer is always 1/12th of the frequency of crystal attached to the 8051 1) 1/12 X 12 MHZ =1MHZ and T=1/1MHZ =1s. 2) 1/12 X 16 MHZ=1.33MHZ and T=1/1.33MHZ=0.75s 3) 1/12 X 11.0592 MHZ=921.6KHZ and T=1/921.6kHz=1.085s
  • 9. 2/4/2023 9 Influence of gate: It is to start or stop the timer, using hardware or software control. SETB TR1, CLR TR1 for timer 1 and SETB TR0,CLR TR0 for timer 0 instruction start or stop timers as long as gate =0in TMOD register. The use of external hardware to stop or start the timer is discussed later.
  • 10. 2/4/2023 10 Mode 0: This mode is compatible for MCS 48 family. The 13 bit counter can hold values between 0000 to 1FFF in TH and TL registers. Therefore, when the timer reaches its maximum of 1FFF it rolls over to 0000 and TF is raised.
  • 11. 2/4/2023 11 Mode 1: The characteristics of this mode are as follows: 1) it is a 16 bit timer. It allows values of 0000 to FFFFH into timer register TH and TL. 2) After TH and TL are loaded with 16 bit initial value. The timer must be started. This is done by SETB TR0 for SETB TR1 for T1. 3) After timer is started, it starts to count up. It counts up to FFFFH . when it rolls over from FFFFH to 0000, it sets a flag bit timer flag(TF). This flag can be monitored. When it is raised, one option is to stop the timer with the instruction CLR TR0 or CLR TR1. 4)After the timer reaches its limit and rolls over, in order to repeat the process the register TH and TL must be reloaded with the original value and TF must be reset to 0.
  • 12. 2/4/2023 12 Xtal osc 12 TR C/T’ TF goes high when FFFF TH TL TF Over flow flag
  • 13. 2/4/2023 13 Mode 1 programming : To generate a time delay, using time mode, the following steps are taken:  load the TMOD value register indicating which timer(T0 0r T1) is to be used and which timer mode is selected.  Load register TL and TH with initial count values.  Start the timer.  Keep monitoring the status of timer flag (TF). Come out of the loop, when TF becomes HIGH.  Stop the timer.  Clear the TF flag for the next count.  Go back to step 2. .
  • 14. 2/4/2023 14 NAME EXAMPLE 11 ORG 0H MOV TMOD,#01H HERE: MOV TL0, #0F2 H 2 MOV TH0, #OFFH 2 CPL P1.5 1 ACALL DELAY 2 SJMP HERE 2 DELAY: SETB TR0 1 AGAIN: JNB TF0,AGAIN 14 CLR TR0 1 CLR TF0 1 RET 1 27
  • 15. 2/4/2023 15 In the above program, observe the following:  TMOD is loaded.  FFF2H is loaded into TH0-TL0.  P1.5 is toggled for the high and low portions of the pulse.  The DELAY subroutine , timer0 is started by SETB TR0 instruction.  Timer0 counts up with the passing of each clock. As timer counts up, it goes through FFF3, ….., FFFF, 0000 states . now TF0 becomes HIGH. T0 is stopped by the instruction CLR TR0.And the process is repeated.
  • 16. 2/4/2023 16 FFF2 FFF3 FFF4 FFFF 0000 TF=0 TF=0 TF=0 TF=0 TF=1
  • 17. 2/4/2023 17 Delay calculation: [( FFFF – YYXX) +1] * 1.085 s The delay calculations for the above example are as follows. for the entire period, T=2*27*1.085s=58.59s Problem: Find the delay generated by T0 in the following code. CLR P2.3 MOV TMOD,#01H HERE: MOV TL0,#3EH MOV TH0,#0B8H SETB P 2.3 SETB TR0 AGAIN: JNB TF0, AGAIN CLR TR0 CLR TF0 CLR P2.3
  • 18. 2/4/2023 18 • NAME EXAMPLE 12 ORG 0000 H MOV TMOD, #10 H AGAIN: MOV TL1, #34 H MOV TH1, #76 H SETB TR1 BACK: JNB TF1, BACK CLR TR1 CPL P1.5 CLR TF1 SJMP AGAIN
  • 19. 2/4/2023 19 Explanation: The above program generates a square wave on pin P1.5 continuously using timer 1 for a time delay. find the frequency of the square wave if XTAL=11.0592MHZ . in your calculation, do not include the OVERHEAD due to instructions in the loop. Since FFFFh- 634h=89CBh+1=89CCh and 89CCH=35276 clock count and 35276X1.0875s=38.274ms
  • 20. 2/4/2023 20 Problem : To find values of TH and TL registers, once the amount of time delay is known. Assuming that XTAL = 11.0592Mhz write a program to generate a square wave of 2Khz frequency on pin P1.5. Assuming XTAL= 11.0592 MHZ, the following steps are to be followed. 1) Divide the desired time delay by 1.085s. 2) Perform 65536-n where n is the decimal value we got in step1. 3) Convert the result of step to hex. 4) Set TL and TH registers accordingly.
  • 21. 2/4/2023 21 Assume that XTAL =11.0592 MHZ, what value do we need to load into the timer’s register, to have a delay of 5ms; show the program for timer 0 to create a pulse width of 5 ms on P2.3. Step 1: 5 ms / 1.085 s= 4608 clocks. 2. 65536 – 4608 = 60228. 3. EE00H 4. TH : EE & TL :00H.
  • 22. 2/4/2023 22 PROGRAM: NAME EXAMPLE-13 ORG 0H CLR P2.3 MOV TMOD,#01H HERE: MOV TL0, #00H MOV TH0, #0EEH SETB P2.3 SETB TR0 AGAIN: JNB TF0,AGAIN CLR TR0 CLR TF0. SJMP HERE.
  • 23. 2/4/2023 23 Xtal osc 12 TR C/T’ TF goes high when FFFF TL TF Over flow flag TH
  • 24. 2/4/2023 24 The characteristics of mod 2 are as follows: 1) It is an 8-bit timer and it allows values of 00h to FFH. 2) After TH is loaded with the 8 bit value, 8051 gives a copy of it to TL. Then the timer must be started. This is done by the instruction ‘SETB TR0’ for T0 and ‘SETB TR1’ for T1. 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 H; it sets high the TF(timer flag). If we are using timer 0, TF0 goes high. If we are using T/C TF1 is raised. 4) When the TL register roll from FFH to 0 and TF is set to 1. TL is reloaded automatically with the original value kept at TH register. To repeat the process, we must simpler clear TF and automatically reloads the originally values. In auto reload, TH is loaded with the initial count and a cop0y of it is given to TL. This reloading leaves TH unchanged still holding a copy of original value.
  • 25. 2/4/2023 25 Mode 2 programming: NAME EXAMPLE14 ORG 0000H MOV TMOD,#2H ; MOV TH0,#0H AGAIN: MOV R5,#250 ACALL Delay CPL P1.0 SJMP AGAIN Delay: SETB TR0 Back: JNB TF0, Back CLR TR0 CLR TF0 DJNZ R5, Delay RET END In the above program, time period is, T= 2(250*256*1.085µS) = 138.8ms
  • 26. 2/4/2023 26 Counter programming: In the counter function, the register T0 and T1 are incremented in response to a 1 to 0 transition at its external inputs T0, T1.In this function , the external input is sampled during S5P2 of every machine cycle. When the samples show a high in one cycle and a low in the next cycle, the count is incremented. The new count value appears in the register during S3P1 of the cycle following the one in which the transition was detected. Since it takes two machine cycles( 24 oscillator periods) to recognize a 1 to 0 transition. The maximum count rate is 1/24 of the oscillator frequency. It is to ensure that given level is sampled at least once before it changes, it should be held for at least one machine cycle. P3.4 is used as T0, timer/counter 0 external input. P3.5 is used as T1, timer/counter 0 external input.
  • 27. 2/4/2023 27 Example –15: . Q. Assuming that clock pulses are fed in to pin T1, write a program for counter 1 in mode 2 to count the pulses and display the state of TL1 count on P2. Solution: MOV TMOD,#01100000B ;counter 1, mode 2, C/T=1 ;external pulses MOV TH1, #0 ;clear TH1 SETB P3.5 ; make T1 input AGAIN:SETB TR 1 ; start the counter BACK: MOV A, TL1 ; get copy of count TL1 MOV P2, A ; display it on port 2 JNB TF1, BACK ; keep doing it if TF=0 CLR TR 1 ;stop the counter 1 CLR TF 1 ;make TF=0 SJMP AGAIN ; keep doing it
  • 28. 2/4/2023 28 Notice in the above program the role of the instruction “SETB P3.5”. Since ports are set up for output when 8051 is powered up, we make P3.5 an input port by making it high. In other words, we must configure (set high) the T1 pin (Pin3.5) to allow pulses to be fed into it. TF0 goes high when Th fromFFFF to 0000 TH0 TL0 TF0 T0 P3.4
  • 29. 2/4/2023 29 Equivalent instructions for the timer control registers FOR TIMER 0 SETB TR0 = SETB TCON.4 CLR TR0 = CLR TCON.4 SETB TF0 = SETB TCON.5 CLR TF0 = CLR TCON.5 FOR TIMER 1 SETB TR1 = SETB TCON.6 CLR TR1 = CLR TCON.6 SETB TF1 = SETB TCON.7 CLR TF1 = CLR TCON.7 TCON : Timer/Counter Control Register TF 1 TR1 TF 0 TR 0 IE 1 IT 1 IE 0 IT 1
  • 30. 2/4/2023 30 The case of gate = 1 in TMOD: So far, the task of timer is started through software using SETB TR0 and TR1 instruction. If gate bit in TMOD is set to 1,the start and stop of a the timer are done externally through pins P3.2 and P3.3 of T0 & T1. This way of operation has several application. For example, assume that on 8051 system is used to sound an alarm every second using T0, in addition to many things. A switch can be connected to pin 3.2 , which can be used to turn on and off the timer, there by shutting down the alarm.
  • 31. 2/4/2023 31 XTAL oscillator % 12 O O O C/ T=0 C/ T =1 T 0 Pin Pin 3.4 TR 0 Gate INT0 Pin Pin3.2