SlideShare une entreprise Scribd logo
1  sur  30
MICROCONTROLLER
MCS-51:
TIMER / COUNTER
Arkhom JODTANG
Civil Aviation Training Center
Timer / Counter
Contents
 Timer and Counter
 Timer Register
 Timer Mode
 Timer Mode 1
 Sample: Daley Programming
 Sample: Pulse generation programming
2
MCS51 Timer / Counter
 The 8051 has two timers/counters are Timer 0 and Timer 1
 Both of them can be used either as timers to generate a
time delay or as counters to count events from external
signal coming to microcontroller.
 By Set interval value, enable timer to start running while
monitoring overflow bit to know the completion interval.
3
Timer Register
4
 TMOD (Timer operation Mode) register
 TCON (Timer Control) register
 THx Register (TH1 for Timer 1, TH0 for Timer 0)
 TLx Register (TL1 for Timer 1, TL0 for Timer 0)
Mode 1 Equivalent circuit
5
Modified from http://blog.circuits4you.com/2015/07/programming-timers-on-8051.html
/ 12
THx TLx
Clock
Gen.
Machine Cycle
TMOD (Timer operation Mode) register
6
 Gate:
 “0” : enable timer by TRx bit
 “1” : enable timer by TRx bit with external signal at pin INTx
 C/T: Counter and Timer selector
 1 for Count signal from Tx Pin (Counter)
 0 for Count internal clock signal (Timer)
 M1: Mode bit 1 (MSB)
 M0: Mode bit 0 (LSB)
TMOD
Timer 1 Timer 0
Gate C/T M1 M0 Gate C/T M1 M0
0 0 0 1 0 0 0 0
MCS51 Pin configuration
7
Mode of Operation
8
 Mode 0: 13-bits
 THx (8-bits) and TLx (5-bits) combine as 13-bits
register
 Mode 1: 16-bits
 THx and TLx combine as 16-bits register
 Mode 2: 8-bit Auto reload
 THx is master value of TLx
Gate C/T M1 M0 Gate C/T M1 M0
0 1 1 0
Setting Mode Operation
9
Gate C/T M1 M0 Gate C/T M1 M0
0 1 0 1
Setting mode of operation by assign appropriate
value to TMOD register
 Timer 1 Mode 1
 MOV TMOD, #00010000B
 MOV TMOD, #10H
 Timer 0 Mode 1
 MOV TMOD, #00000001B
 MOV TMOD, #01H
Example 9.1
10
TMOD
Gate C/T M1 M0 Gate C/T M1 M0
x x x x x x x x
Find the value for TMOD if we want to program
Timer 0 in mode 1, count from internal clock source,
and use instructions to start and stop the timer.
Timer 1 in mode 2, count from external and start
counting by instruction.
01100001
Mode 1 Equivalent circuit
11
Modified from http://blog.circuits4you.com/2015/07/programming-timers-on-8051.html
/ 12
THx TLx
Clock
Gen.
Machine Cycle
Example 9.1
12
TMOD
Timer 1 Timer 0
Gate C/T M1 M0 Gate C/T M1 M0
0 0 0 0 x x x x
Find the value for TMOD if we want to program
Timer 0 in mode 1, count from internal clock
source, and use instructions to start and stop the
timer.
Solution:
 Timer 0, So, we care only Timer 0 Nibble
Example 9.1
13
TMOD
Timer 1 Timer 0
Gate C/T M1 M0 Gate C/T M1 M0
0 0 0 0 x x 0 1
Find the value for TMOD if we want to program
Timer 0 in mode 1, count from internal clock source,
and use instructions to enable the timer.
Solution:
 Timer 0: So, we care only Timer 0 Nibble
 Mode 1: M1 = 0, M0 = 1
Example 9.1
14
TMOD
Timer 1 Timer 0
Gate C/T M1 M0 Gate C/T M1 M0
0 0 0 0 x 0 0 1
Find the value for TMOD if we want to program
Timer 0 in mode 1, count from internal clock source,
and use instructions to enable the timer.
Solution:
 Timer 0: So, we care only Timer 0 Nibble
 Mode 1: M1 = 0, M0 = 1
 Internal Clock: That mean Timer. So, C/T = 0
Example 9.1
15
TMOD
Timer 1 Timer 0
Gate C/T M1 M0 Gate C/T M1 M0
0 0 0 0 0 0 0 1
Find the value for TMOD if we want to program Timer 0
in mode 1, count from internal clock source, and use
instructions to enable the timer.
Solution:
 Timer 0: So, we care only Timer 0 Nibble
 Mode 1: M1 = 0, M0 = 1
 Internal Clock: That mean Timer. So, C/T = 0
 Enable by TR0 bits: Gate = 0
Example 9.1
16
TMOD
Timer 1 Timer 0
Gate C/T M1 M0 Gate C/T M1 M0
0 0 0 0 0 0 0 1
Find the value for TMOD if we want to program Timer 0 in mode 1, count from
internal clock source, and use instructions to enable the timer.
Solution:
 Timer 0: So, we care only Timer 0 Nibble
 Mode 1: M1 = 0, M0 = 1
 Internal Clock: That mean Timer. So, C/T = 0
 Enable by TR0 bits: Gate = 0
Answer: Value of TMOD is #00000001B or #01H
(Use instruction MOV TMOD, #01H)
TCON (Timer Control register)
17
 TF1: Timer 1 Over Flow bit
 This bit will set when timer is over flow
 TR1: Timer 1 Enable bit
 Start running of Timer 1
 TF0: Timer 0 Over Flow bit
 This bit will set when timer is over flow
 TR0: Timer 0 Enable bit
 Start running of Timer 0
 IE1, IT1, IE0, IT0 will describe in next chapter
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
0 0 0 1 0 0 0 0
Mode 1’s Operation
18
THx TLx
0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1
 THx and TLx store start value
 Timer operation always count-up (increase) on [THx, TLX] each machine cycle
 SETB TRx instruction to start running timer
 Over flow when Increase FFFF to 0000
 Interval value = FFFF – [TH,TL]
 FFFF give minimum interval time
 0000 give maximum interval time
 Example:
 MOV TH0, #22H
 MOV TL0, #00H
 ; Then interval is FFFF – 2200 = DDFF Machine Cycle.
 Tips: Monitor TFx with instruction
 “MonitorLable : JNB TFx, MonitorLable”
Example 9.2
19
Calculate amount of time delay in the DELAY subroutine below
generated by the timer. Assume that XTAL = 11.0592 MHz.
Delay:
MOV TMOD, #01
MOV TL0, #0F2H
MOV TH0, #0FFH
SETB TR0
MonitorTX : JNB TF0, MonitorTX
CLR TR0
CLR TF0
RET
Example 9.2
20
Solution:
The timer works with a clock frequency of 1/12
of the X’TAL frequency; therefore, we have
11.0592 MHz / 12 = 921.6 kHz as the timer
(Machine Cycle) frequency.
As a result, each clock has a period of T = 1 /
921.6 kHz = 1.085 (is. In other words, Timer 0
counts up each 1.085 s resulting in delay =
number of counts x 1.085 s.
Example 9.2
21
Solution:
As the timer counts up, it goes through the
states of FFF3, FFF4, FFF5, FFF6, FFF7, FFF8,
FFF9, FFFA, FFFB, FFFC, FFFD, FFFE, and
FFFFH. One more clock rolls it to 0000, raising
the timer flag (TF0 = 1). At that point, the JNB
instruction falls through.
Timer 0 is stopped by the instruction “CLR TRO”.
The DELAY subroutine ends
Example 9.2
22
Place Machine cycle to the code:
Delay:
MOV TMOD, #01 2
MOV TL0, #0F2H 2
MOV TH0, #0FFH 2
SETB TR0 1
MonitorTX : JNB TF0, MonitorTX 14
CLR TR0 1
CLR TF0 1
RET 2
Total = 25
Example 9.2
23
From total machine cycle used was 25 machine cycles.
25 x 1.058 s. = 27.125 s.
So, This subroutine generate delay for 27.125 s.
Conclusion: Total Machine cycle to overflow is
Machine Cycle = 1 + FFFF – Interval (TH,TL) value
Workshop 9.1
24
Calculate amount of time delay in the DELAY subroutine below
generated by the timer. Assume that XTAL = 22.1184 MHz.
Delay:
MOV TMOD, #01
MOV TL0, #034H
MOV TH0, #012H
SETB TR0
MonitorTX : JNB TF0, MonitorTX
CLR TR0
CLR TF0
RET
Example 9.3
25
Connect Port 3.0 with
Input Switch and Port 1.0
with LED Display
Write Program for MCS51
to Turn ON LED for 2
Second after user press
input switch.
Show calculate delay
generate by MCS51.
Assume that X’TAL =
11.0592 MHz.
Start
TMOD
End
P3.0
= 1
ON
Display
Delay 2
Seconds
OFF
Display
WaitSW:
R3 =33D
DJNZ R3, Loop
Delay
Loop:
Example 9.3
26
Connect Port 3.0 with
Input Switch and Port
1.0 with LED Display
From code below
calculate delay
generate by MCS51.
Assume that X’TAL =
11.0592 MHz.
MOV TMOD, #10H ; Set Timer Mode
CLR P1.0
WaitSW: JB P3.0, WaitSW
SETB P1.0
ACALL Delay
CLR P1.0
SJMP $
Delay:
MOV R3,#31D
MainProcess:
MOV TL1,#08H
MOV TH1,#1H
SETB TR1
Monitoring: JNB TF1, Monitoring
CLR TR1
CLR TF1
DJNZ R3, MainProcess
RET
END
27
DJNZ (Sample)
 ; DJNZ is very convenion for
 ; specified number of turn for
some process.
 MOV R3,#31D
 Loop:
 ; Process here
 DJNZ R3, Loop
 END
28
Start
R5 =10D
End
Loop:
DJNZ R5, Loop
Process
Example 9.4
29
From code below calculate frequency of signal generate by MCS51. Assume that X’TAL =
11.0592 MHz.
MOV TMOD, #10H
Loop:
CPL P1.1
ACALL Delay
JMP Loop ; All Loop All Delay + 7 = FF04H = 65286D Machine cycle
Delay:
MOV TL1,#08H
MOV TH1,#1H
SETB TR1
Monitoring: JNB TF1, Monitoring
CLR TR1
CLR TF1
RET ; All Delay code FEFF
END
Example 9.5
30
Write assembly
program for MCS51
to generate Pulse
signal with duty cycle
20% to pin P1.1
MOV TMOD, #10H
Loop: SETB P1.1
ACALL DelayON
CLR P1.1
ACALL DelayOFF
JMP Loop
DelayON: MOV TL1,#00D
MOV TH1,#236D ; 256-20
SETB TR1
MonitoringOn: JNB TF1, MonitoringOn
CLR TR1
CLR TF1
RET
DelayOFF: MOV TL1,#00D
MOV TH1,#176D ; 256-80
SETB TR1
MonitoringOff: JNB TF1, MonitoringOff
CLR TR1
CLR TF1
RET
END

Contenu connexe

Tendances

8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and CountersShreyans Pathak
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and Counterscjbas
 
Chapter 16 timers and counters
Chapter 16 timers and countersChapter 16 timers and counters
Chapter 16 timers and countersforgotteniman
 
8051 Timers / Counters
8051 Timers / Counters8051 Timers / Counters
8051 Timers / CountersPatricio Lima
 
AVR_Course_Day7 timers counters and interrupt programming
AVR_Course_Day7 timers counters and  interrupt programmingAVR_Course_Day7 timers counters and  interrupt programming
AVR_Course_Day7 timers counters and interrupt programmingMohamed Ali
 
Timer And Counter in 8051 Microcontroller
Timer And Counter in 8051 MicrocontrollerTimer And Counter in 8051 Microcontroller
Timer And Counter in 8051 MicrocontrollerJay Makwana
 
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND CPIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C raosandy11
 
Timers done by Priyanga KR
Timers done by Priyanga KRTimers done by Priyanga KR
Timers done by Priyanga KRPriyangaKR1
 

Tendances (20)

8051 Timer
8051 Timer8051 Timer
8051 Timer
 
8051 timers
8051 timers8051 timers
8051 timers
 
8051 ch9
8051 ch98051 ch9
8051 ch9
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and Counters
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and Counters
 
8051 timer counter
8051 timer counter8051 timer counter
8051 timer counter
 
9 timer programming
9 timer programming9 timer programming
9 timer programming
 
Chapter 16 timers and counters
Chapter 16 timers and countersChapter 16 timers and counters
Chapter 16 timers and counters
 
8051 timers--2
   8051 timers--2   8051 timers--2
8051 timers--2
 
Timers
TimersTimers
Timers
 
12 mt06ped007
12 mt06ped007 12 mt06ped007
12 mt06ped007
 
Uc
UcUc
Uc
 
8051 Timers / Counters
8051 Timers / Counters8051 Timers / Counters
8051 Timers / Counters
 
Timers
TimersTimers
Timers
 
AVR_Course_Day7 timers counters and interrupt programming
AVR_Course_Day7 timers counters and  interrupt programmingAVR_Course_Day7 timers counters and  interrupt programming
AVR_Course_Day7 timers counters and interrupt programming
 
Timer And Counter in 8051 Microcontroller
Timer And Counter in 8051 MicrocontrollerTimer And Counter in 8051 Microcontroller
Timer And Counter in 8051 Microcontroller
 
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND CPIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
 
Timers done by Priyanga KR
Timers done by Priyanga KRTimers done by Priyanga KR
Timers done by Priyanga KR
 
8051e
8051e8051e
8051e
 
Lpc 1768 timers
Lpc 1768 timersLpc 1768 timers
Lpc 1768 timers
 

En vedette

8254 timer - Microprocessor and interfacing
8254 timer - Microprocessor and interfacing8254 timer - Microprocessor and interfacing
8254 timer - Microprocessor and interfacingAmitabh Shukla
 
Pengetahuan Dasar penggunaan Timer dan Counter Microcontroller AVR
Pengetahuan Dasar penggunaan Timer dan Counter Microcontroller AVR Pengetahuan Dasar penggunaan Timer dan Counter Microcontroller AVR
Pengetahuan Dasar penggunaan Timer dan Counter Microcontroller AVR KEN KEN
 
Interrupt programming with 8051 microcontroller
Interrupt programming with 8051  microcontrollerInterrupt programming with 8051  microcontroller
Interrupt programming with 8051 microcontrollerAnkit Bhatnagar
 
8 interrupt 8051
8 interrupt 80518 interrupt 8051
8 interrupt 8051daniemol
 
8254 Programmable Interval Timer by vijay
8254 Programmable Interval Timer by vijay8254 Programmable Interval Timer by vijay
8254 Programmable Interval Timer by vijayVijay Kumar
 
8051 microcontroller features
8051 microcontroller features8051 microcontroller features
8051 microcontroller featuresTech_MX
 
Microcontroller 8051
Microcontroller 8051Microcontroller 8051
Microcontroller 8051Rashmi
 
8085 microprocessor ramesh gaonkar
8085 microprocessor   ramesh gaonkar8085 microprocessor   ramesh gaonkar
8085 microprocessor ramesh gaonkarSAQUIB AHMAD
 
Introduction to microprocessor
Introduction to microprocessorIntroduction to microprocessor
Introduction to microprocessorKashyap Shah
 
Assembly Language Programming Of 8085
Assembly Language Programming Of 8085Assembly Language Programming Of 8085
Assembly Language Programming Of 8085techbed
 

En vedette (20)

Programmable Timer 8253/8254
Programmable Timer 8253/8254Programmable Timer 8253/8254
Programmable Timer 8253/8254
 
8254 timer - Microprocessor and interfacing
8254 timer - Microprocessor and interfacing8254 timer - Microprocessor and interfacing
8254 timer - Microprocessor and interfacing
 
Pengetahuan Dasar penggunaan Timer dan Counter Microcontroller AVR
Pengetahuan Dasar penggunaan Timer dan Counter Microcontroller AVR Pengetahuan Dasar penggunaan Timer dan Counter Microcontroller AVR
Pengetahuan Dasar penggunaan Timer dan Counter Microcontroller AVR
 
8051 interrupts
8051 interrupts8051 interrupts
8051 interrupts
 
Interrupt programming with 8051 microcontroller
Interrupt programming with 8051  microcontrollerInterrupt programming with 8051  microcontroller
Interrupt programming with 8051 microcontroller
 
8 interrupt 8051
8 interrupt 80518 interrupt 8051
8 interrupt 8051
 
8254 Programmable Interval Timer by vijay
8254 Programmable Interval Timer by vijay8254 Programmable Interval Timer by vijay
8254 Programmable Interval Timer by vijay
 
Interrupt
InterruptInterrupt
Interrupt
 
Programming with 8085
Programming with 8085Programming with 8085
Programming with 8085
 
8085 MICROPROCESSOR
8085 MICROPROCESSOR 8085 MICROPROCESSOR
8085 MICROPROCESSOR
 
8051 microcontroller features
8051 microcontroller features8051 microcontroller features
8051 microcontroller features
 
Microcontroller 8051
Microcontroller 8051Microcontroller 8051
Microcontroller 8051
 
Microprocessor 8086
Microprocessor 8086Microprocessor 8086
Microprocessor 8086
 
Microprocessor ppt
Microprocessor pptMicroprocessor ppt
Microprocessor ppt
 
8085 microprocessor ramesh gaonkar
8085 microprocessor   ramesh gaonkar8085 microprocessor   ramesh gaonkar
8085 microprocessor ramesh gaonkar
 
1
11
1
 
Counters
CountersCounters
Counters
 
Chapter 5 counter
Chapter 5 counterChapter 5 counter
Chapter 5 counter
 
Introduction to microprocessor
Introduction to microprocessorIntroduction to microprocessor
Introduction to microprocessor
 
Assembly Language Programming Of 8085
Assembly Language Programming Of 8085Assembly Language Programming Of 8085
Assembly Language Programming Of 8085
 

Similaire à Microprocessor Week 9: Timer and Counter

Microcontrollers-MODULE4.pptx
Microcontrollers-MODULE4.pptxMicrocontrollers-MODULE4.pptx
Microcontrollers-MODULE4.pptxAmoghR3
 
lecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.pptlecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.pptHebaEng
 
MICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.pptMICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.pptreemasajin1
 
PIC-Chapter_10.pptx
PIC-Chapter_10.pptxPIC-Chapter_10.pptx
PIC-Chapter_10.pptxAliBzeih7
 
EC8691 - UNIT 5.pdf
EC8691 - UNIT 5.pdfEC8691 - UNIT 5.pdf
EC8691 - UNIT 5.pdfSPonmalar1
 
PIC timer programming
PIC timer programmingPIC timer programming
PIC timer programmingAkash Puri
 
ControlLogix Timers FA16
ControlLogix Timers FA16ControlLogix Timers FA16
ControlLogix Timers FA16John Todora
 
L15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 pL15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 prsamurti
 
Using Timers in PIC18F Microcontrollers
Using Timers in PIC18F MicrocontrollersUsing Timers in PIC18F Microcontrollers
Using Timers in PIC18F MicrocontrollersCorrado Santoro
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Aarav Soni
 

Similaire à Microprocessor Week 9: Timer and Counter (20)

Microcontrollers-MODULE4.pptx
Microcontrollers-MODULE4.pptxMicrocontrollers-MODULE4.pptx
Microcontrollers-MODULE4.pptx
 
4.Timer_1.ppt
4.Timer_1.ppt4.Timer_1.ppt
4.Timer_1.ppt
 
UNIT-5.ppt
UNIT-5.pptUNIT-5.ppt
UNIT-5.ppt
 
lecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.pptlecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.ppt
 
MICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.pptMICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.ppt
 
Class9
Class9Class9
Class9
 
AVRTIMER.pptx
AVRTIMER.pptxAVRTIMER.pptx
AVRTIMER.pptx
 
PIC-Chapter_10.pptx
PIC-Chapter_10.pptxPIC-Chapter_10.pptx
PIC-Chapter_10.pptx
 
timers.pdf
timers.pdftimers.pdf
timers.pdf
 
Timers in Arduino
Timers in ArduinoTimers in Arduino
Timers in Arduino
 
EC8691 - UNIT 5.pdf
EC8691 - UNIT 5.pdfEC8691 - UNIT 5.pdf
EC8691 - UNIT 5.pdf
 
Timers of 8051
Timers of 8051Timers of 8051
Timers of 8051
 
PIC timer programming
PIC timer programmingPIC timer programming
PIC timer programming
 
Chapter 08
Chapter 08Chapter 08
Chapter 08
 
ControlLogix Timers FA16
ControlLogix Timers FA16ControlLogix Timers FA16
ControlLogix Timers FA16
 
L15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 pL15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 p
 
Using Timers in PIC18F Microcontrollers
Using Timers in PIC18F MicrocontrollersUsing Timers in PIC18F Microcontrollers
Using Timers in PIC18F Microcontrollers
 
8051 Timers
8051 Timers8051 Timers
8051 Timers
 
TIMERS.pptx
TIMERS.pptxTIMERS.pptx
TIMERS.pptx
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)
 

Plus de Arkhom Jodtang

MCS51 Training board Model CATC2016A
MCS51 Training board Model CATC2016AMCS51 Training board Model CATC2016A
MCS51 Training board Model CATC2016AArkhom Jodtang
 
Microprocessor Week 10: Applications
Microprocessor Week 10: ApplicationsMicroprocessor Week 10: Applications
Microprocessor Week 10: ApplicationsArkhom Jodtang
 
Microprocessor Week 8: Advance programming
Microprocessor Week 8: Advance programmingMicroprocessor Week 8: Advance programming
Microprocessor Week 8: Advance programmingArkhom Jodtang
 
Microprocessor Week 8: Subroutine
Microprocessor Week 8: Subroutine Microprocessor Week 8: Subroutine
Microprocessor Week 8: Subroutine Arkhom Jodtang
 
Microprocessor Week2: Data Transfer
Microprocessor Week2: Data TransferMicroprocessor Week2: Data Transfer
Microprocessor Week2: Data TransferArkhom Jodtang
 
Microprocessor Week 2: CH2 Circuit and Operation
Microprocessor Week 2: CH2 Circuit and OperationMicroprocessor Week 2: CH2 Circuit and Operation
Microprocessor Week 2: CH2 Circuit and OperationArkhom Jodtang
 
Microprocessor Week1: Introduction
Microprocessor Week1: IntroductionMicroprocessor Week1: Introduction
Microprocessor Week1: IntroductionArkhom Jodtang
 
Microprocessor laboratory 03 Arithmetic Operation (Additional and Subtraction)
Microprocessor laboratory 03 Arithmetic Operation (Additional and Subtraction)Microprocessor laboratory 03 Arithmetic Operation (Additional and Subtraction)
Microprocessor laboratory 03 Arithmetic Operation (Additional and Subtraction)Arkhom Jodtang
 
Microprocessor Week 4-5 MCS-51 Arithmetic operation
Microprocessor Week 4-5 MCS-51 Arithmetic operationMicroprocessor Week 4-5 MCS-51 Arithmetic operation
Microprocessor Week 4-5 MCS-51 Arithmetic operationArkhom Jodtang
 
Use of Computer & IT, Laboratory MS Word
Use of Computer & IT, Laboratory MS WordUse of Computer & IT, Laboratory MS Word
Use of Computer & IT, Laboratory MS WordArkhom Jodtang
 
Microprocessor Laboratory 2: Logical instructions
Microprocessor Laboratory 2: Logical instructionsMicroprocessor Laboratory 2: Logical instructions
Microprocessor Laboratory 2: Logical instructionsArkhom Jodtang
 
Microprocessor: Delay technique
Microprocessor: Delay techniqueMicroprocessor: Delay technique
Microprocessor: Delay techniqueArkhom Jodtang
 
Distance Measuring Car
Distance Measuring CarDistance Measuring Car
Distance Measuring CarArkhom Jodtang
 
Microprocessor Week 7: Branch Instruction
Microprocessor Week 7: Branch InstructionMicroprocessor Week 7: Branch Instruction
Microprocessor Week 7: Branch InstructionArkhom Jodtang
 
Tamech 2013 Presentation
Tamech 2013 PresentationTamech 2013 Presentation
Tamech 2013 PresentationArkhom Jodtang
 
Electronics & Avionics project
Electronics & Avionics projectElectronics & Avionics project
Electronics & Avionics projectArkhom Jodtang
 

Plus de Arkhom Jodtang (16)

MCS51 Training board Model CATC2016A
MCS51 Training board Model CATC2016AMCS51 Training board Model CATC2016A
MCS51 Training board Model CATC2016A
 
Microprocessor Week 10: Applications
Microprocessor Week 10: ApplicationsMicroprocessor Week 10: Applications
Microprocessor Week 10: Applications
 
Microprocessor Week 8: Advance programming
Microprocessor Week 8: Advance programmingMicroprocessor Week 8: Advance programming
Microprocessor Week 8: Advance programming
 
Microprocessor Week 8: Subroutine
Microprocessor Week 8: Subroutine Microprocessor Week 8: Subroutine
Microprocessor Week 8: Subroutine
 
Microprocessor Week2: Data Transfer
Microprocessor Week2: Data TransferMicroprocessor Week2: Data Transfer
Microprocessor Week2: Data Transfer
 
Microprocessor Week 2: CH2 Circuit and Operation
Microprocessor Week 2: CH2 Circuit and OperationMicroprocessor Week 2: CH2 Circuit and Operation
Microprocessor Week 2: CH2 Circuit and Operation
 
Microprocessor Week1: Introduction
Microprocessor Week1: IntroductionMicroprocessor Week1: Introduction
Microprocessor Week1: Introduction
 
Microprocessor laboratory 03 Arithmetic Operation (Additional and Subtraction)
Microprocessor laboratory 03 Arithmetic Operation (Additional and Subtraction)Microprocessor laboratory 03 Arithmetic Operation (Additional and Subtraction)
Microprocessor laboratory 03 Arithmetic Operation (Additional and Subtraction)
 
Microprocessor Week 4-5 MCS-51 Arithmetic operation
Microprocessor Week 4-5 MCS-51 Arithmetic operationMicroprocessor Week 4-5 MCS-51 Arithmetic operation
Microprocessor Week 4-5 MCS-51 Arithmetic operation
 
Use of Computer & IT, Laboratory MS Word
Use of Computer & IT, Laboratory MS WordUse of Computer & IT, Laboratory MS Word
Use of Computer & IT, Laboratory MS Word
 
Microprocessor Laboratory 2: Logical instructions
Microprocessor Laboratory 2: Logical instructionsMicroprocessor Laboratory 2: Logical instructions
Microprocessor Laboratory 2: Logical instructions
 
Microprocessor: Delay technique
Microprocessor: Delay techniqueMicroprocessor: Delay technique
Microprocessor: Delay technique
 
Distance Measuring Car
Distance Measuring CarDistance Measuring Car
Distance Measuring Car
 
Microprocessor Week 7: Branch Instruction
Microprocessor Week 7: Branch InstructionMicroprocessor Week 7: Branch Instruction
Microprocessor Week 7: Branch Instruction
 
Tamech 2013 Presentation
Tamech 2013 PresentationTamech 2013 Presentation
Tamech 2013 Presentation
 
Electronics & Avionics project
Electronics & Avionics projectElectronics & Avionics project
Electronics & Avionics project
 

Dernier

System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingBootNeck1
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating SystemRashmi Bhat
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Industrial Safety Unit-I SAFETY TERMINOLOGIES
Industrial Safety Unit-I SAFETY TERMINOLOGIESIndustrial Safety Unit-I SAFETY TERMINOLOGIES
Industrial Safety Unit-I SAFETY TERMINOLOGIESNarmatha D
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Internet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxInternet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxVelmuruganTECE
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxsiddharthjain2303
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating SystemRashmi Bhat
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxRomil Mishra
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 

Dernier (20)

System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event Scheduling
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating System
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Industrial Safety Unit-I SAFETY TERMINOLOGIES
Industrial Safety Unit-I SAFETY TERMINOLOGIESIndustrial Safety Unit-I SAFETY TERMINOLOGIES
Industrial Safety Unit-I SAFETY TERMINOLOGIES
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Internet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxInternet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptx
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptx
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating System
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptx
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 

Microprocessor Week 9: Timer and Counter

  • 1. MICROCONTROLLER MCS-51: TIMER / COUNTER Arkhom JODTANG Civil Aviation Training Center
  • 2. Timer / Counter Contents  Timer and Counter  Timer Register  Timer Mode  Timer Mode 1  Sample: Daley Programming  Sample: Pulse generation programming 2
  • 3. MCS51 Timer / Counter  The 8051 has two timers/counters are Timer 0 and Timer 1  Both of them can be used either as timers to generate a time delay or as counters to count events from external signal coming to microcontroller.  By Set interval value, enable timer to start running while monitoring overflow bit to know the completion interval. 3
  • 4. Timer Register 4  TMOD (Timer operation Mode) register  TCON (Timer Control) register  THx Register (TH1 for Timer 1, TH0 for Timer 0)  TLx Register (TL1 for Timer 1, TL0 for Timer 0)
  • 5. Mode 1 Equivalent circuit 5 Modified from http://blog.circuits4you.com/2015/07/programming-timers-on-8051.html / 12 THx TLx Clock Gen. Machine Cycle
  • 6. TMOD (Timer operation Mode) register 6  Gate:  “0” : enable timer by TRx bit  “1” : enable timer by TRx bit with external signal at pin INTx  C/T: Counter and Timer selector  1 for Count signal from Tx Pin (Counter)  0 for Count internal clock signal (Timer)  M1: Mode bit 1 (MSB)  M0: Mode bit 0 (LSB) TMOD Timer 1 Timer 0 Gate C/T M1 M0 Gate C/T M1 M0 0 0 0 1 0 0 0 0
  • 8. Mode of Operation 8  Mode 0: 13-bits  THx (8-bits) and TLx (5-bits) combine as 13-bits register  Mode 1: 16-bits  THx and TLx combine as 16-bits register  Mode 2: 8-bit Auto reload  THx is master value of TLx Gate C/T M1 M0 Gate C/T M1 M0 0 1 1 0
  • 9. Setting Mode Operation 9 Gate C/T M1 M0 Gate C/T M1 M0 0 1 0 1 Setting mode of operation by assign appropriate value to TMOD register  Timer 1 Mode 1  MOV TMOD, #00010000B  MOV TMOD, #10H  Timer 0 Mode 1  MOV TMOD, #00000001B  MOV TMOD, #01H
  • 10. Example 9.1 10 TMOD Gate C/T M1 M0 Gate C/T M1 M0 x x x x x x x x Find the value for TMOD if we want to program Timer 0 in mode 1, count from internal clock source, and use instructions to start and stop the timer. Timer 1 in mode 2, count from external and start counting by instruction. 01100001
  • 11. Mode 1 Equivalent circuit 11 Modified from http://blog.circuits4you.com/2015/07/programming-timers-on-8051.html / 12 THx TLx Clock Gen. Machine Cycle
  • 12. Example 9.1 12 TMOD Timer 1 Timer 0 Gate C/T M1 M0 Gate C/T M1 M0 0 0 0 0 x x x x Find the value for TMOD if we want to program Timer 0 in mode 1, count from internal clock source, and use instructions to start and stop the timer. Solution:  Timer 0, So, we care only Timer 0 Nibble
  • 13. Example 9.1 13 TMOD Timer 1 Timer 0 Gate C/T M1 M0 Gate C/T M1 M0 0 0 0 0 x x 0 1 Find the value for TMOD if we want to program Timer 0 in mode 1, count from internal clock source, and use instructions to enable the timer. Solution:  Timer 0: So, we care only Timer 0 Nibble  Mode 1: M1 = 0, M0 = 1
  • 14. Example 9.1 14 TMOD Timer 1 Timer 0 Gate C/T M1 M0 Gate C/T M1 M0 0 0 0 0 x 0 0 1 Find the value for TMOD if we want to program Timer 0 in mode 1, count from internal clock source, and use instructions to enable the timer. Solution:  Timer 0: So, we care only Timer 0 Nibble  Mode 1: M1 = 0, M0 = 1  Internal Clock: That mean Timer. So, C/T = 0
  • 15. Example 9.1 15 TMOD Timer 1 Timer 0 Gate C/T M1 M0 Gate C/T M1 M0 0 0 0 0 0 0 0 1 Find the value for TMOD if we want to program Timer 0 in mode 1, count from internal clock source, and use instructions to enable the timer. Solution:  Timer 0: So, we care only Timer 0 Nibble  Mode 1: M1 = 0, M0 = 1  Internal Clock: That mean Timer. So, C/T = 0  Enable by TR0 bits: Gate = 0
  • 16. Example 9.1 16 TMOD Timer 1 Timer 0 Gate C/T M1 M0 Gate C/T M1 M0 0 0 0 0 0 0 0 1 Find the value for TMOD if we want to program Timer 0 in mode 1, count from internal clock source, and use instructions to enable the timer. Solution:  Timer 0: So, we care only Timer 0 Nibble  Mode 1: M1 = 0, M0 = 1  Internal Clock: That mean Timer. So, C/T = 0  Enable by TR0 bits: Gate = 0 Answer: Value of TMOD is #00000001B or #01H (Use instruction MOV TMOD, #01H)
  • 17. TCON (Timer Control register) 17  TF1: Timer 1 Over Flow bit  This bit will set when timer is over flow  TR1: Timer 1 Enable bit  Start running of Timer 1  TF0: Timer 0 Over Flow bit  This bit will set when timer is over flow  TR0: Timer 0 Enable bit  Start running of Timer 0  IE1, IT1, IE0, IT0 will describe in next chapter TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 0 0 0 1 0 0 0 0
  • 18. Mode 1’s Operation 18 THx TLx 0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1  THx and TLx store start value  Timer operation always count-up (increase) on [THx, TLX] each machine cycle  SETB TRx instruction to start running timer  Over flow when Increase FFFF to 0000  Interval value = FFFF – [TH,TL]  FFFF give minimum interval time  0000 give maximum interval time  Example:  MOV TH0, #22H  MOV TL0, #00H  ; Then interval is FFFF – 2200 = DDFF Machine Cycle.  Tips: Monitor TFx with instruction  “MonitorLable : JNB TFx, MonitorLable”
  • 19. Example 9.2 19 Calculate amount of time delay in the DELAY subroutine below generated by the timer. Assume that XTAL = 11.0592 MHz. Delay: MOV TMOD, #01 MOV TL0, #0F2H MOV TH0, #0FFH SETB TR0 MonitorTX : JNB TF0, MonitorTX CLR TR0 CLR TF0 RET
  • 20. Example 9.2 20 Solution: The timer works with a clock frequency of 1/12 of the X’TAL frequency; therefore, we have 11.0592 MHz / 12 = 921.6 kHz as the timer (Machine Cycle) frequency. As a result, each clock has a period of T = 1 / 921.6 kHz = 1.085 (is. In other words, Timer 0 counts up each 1.085 s resulting in delay = number of counts x 1.085 s.
  • 21. Example 9.2 21 Solution: As the timer counts up, it goes through the states of FFF3, FFF4, FFF5, FFF6, FFF7, FFF8, FFF9, FFFA, FFFB, FFFC, FFFD, FFFE, and FFFFH. One more clock rolls it to 0000, raising the timer flag (TF0 = 1). At that point, the JNB instruction falls through. Timer 0 is stopped by the instruction “CLR TRO”. The DELAY subroutine ends
  • 22. Example 9.2 22 Place Machine cycle to the code: Delay: MOV TMOD, #01 2 MOV TL0, #0F2H 2 MOV TH0, #0FFH 2 SETB TR0 1 MonitorTX : JNB TF0, MonitorTX 14 CLR TR0 1 CLR TF0 1 RET 2 Total = 25
  • 23. Example 9.2 23 From total machine cycle used was 25 machine cycles. 25 x 1.058 s. = 27.125 s. So, This subroutine generate delay for 27.125 s. Conclusion: Total Machine cycle to overflow is Machine Cycle = 1 + FFFF – Interval (TH,TL) value
  • 24. Workshop 9.1 24 Calculate amount of time delay in the DELAY subroutine below generated by the timer. Assume that XTAL = 22.1184 MHz. Delay: MOV TMOD, #01 MOV TL0, #034H MOV TH0, #012H SETB TR0 MonitorTX : JNB TF0, MonitorTX CLR TR0 CLR TF0 RET
  • 25. Example 9.3 25 Connect Port 3.0 with Input Switch and Port 1.0 with LED Display Write Program for MCS51 to Turn ON LED for 2 Second after user press input switch. Show calculate delay generate by MCS51. Assume that X’TAL = 11.0592 MHz. Start TMOD End P3.0 = 1 ON Display Delay 2 Seconds OFF Display WaitSW: R3 =33D DJNZ R3, Loop Delay Loop:
  • 26. Example 9.3 26 Connect Port 3.0 with Input Switch and Port 1.0 with LED Display From code below calculate delay generate by MCS51. Assume that X’TAL = 11.0592 MHz. MOV TMOD, #10H ; Set Timer Mode CLR P1.0 WaitSW: JB P3.0, WaitSW SETB P1.0 ACALL Delay CLR P1.0 SJMP $ Delay: MOV R3,#31D MainProcess: MOV TL1,#08H MOV TH1,#1H SETB TR1 Monitoring: JNB TF1, Monitoring CLR TR1 CLR TF1 DJNZ R3, MainProcess RET END
  • 27. 27
  • 28. DJNZ (Sample)  ; DJNZ is very convenion for  ; specified number of turn for some process.  MOV R3,#31D  Loop:  ; Process here  DJNZ R3, Loop  END 28 Start R5 =10D End Loop: DJNZ R5, Loop Process
  • 29. Example 9.4 29 From code below calculate frequency of signal generate by MCS51. Assume that X’TAL = 11.0592 MHz. MOV TMOD, #10H Loop: CPL P1.1 ACALL Delay JMP Loop ; All Loop All Delay + 7 = FF04H = 65286D Machine cycle Delay: MOV TL1,#08H MOV TH1,#1H SETB TR1 Monitoring: JNB TF1, Monitoring CLR TR1 CLR TF1 RET ; All Delay code FEFF END
  • 30. Example 9.5 30 Write assembly program for MCS51 to generate Pulse signal with duty cycle 20% to pin P1.1 MOV TMOD, #10H Loop: SETB P1.1 ACALL DelayON CLR P1.1 ACALL DelayOFF JMP Loop DelayON: MOV TL1,#00D MOV TH1,#236D ; 256-20 SETB TR1 MonitoringOn: JNB TF1, MonitoringOn CLR TR1 CLR TF1 RET DelayOFF: MOV TL1,#00D MOV TH1,#176D ; 256-80 SETB TR1 MonitoringOff: JNB TF1, MonitoringOff CLR TR1 CLR TF1 RET END