SlideShare une entreprise Scribd logo
1  sur  15
Télécharger pour lire hors ligne
Pari vallal Kannan
Center for Integrated Circuits and Systems
University of Texas at Dallas
8051 Timers
Class 7
EE4380 Fall 2002
24-Sep-02 2
Introduction
l Timers
– Timing devices - Generate specific time delay
– Event counter - Count the occurrence of an
(external) event
l 8051 has Two 16-bit timers T0 and T1
l 8051 Timers can operate in many modes
– Timer mode is controlled by TMOD register
– Timers are controlled by TCON register (upper 4
bits)
l Clock source for the timer is sys_clk/12
24-Sep-02 3
Timer Registers
l 8051 timers are made up of two 8-bit registers
each
l Timer 0 Registers
– TL0 and TH0
– Accessed like any other register
l mov TL0, #55H, mov R1, TH0
l Timer 1 Registers
– TL1 and TH1
24-Sep-02 4
8051 Timer : TMOD Register
l Gate - Gating Control (how to start-stop timer)
– 0 à software gating (TRx bit in TCON register)
– 1 à hardware gating (INTx pin)
l C/T - Counter/Timer Operation
– 0 à Timer operation (clock is sysclk/12)
– 1 à Counter operation (clock is T0 or T1 pin)
l M1:M0 - Mode control
– 00 à Mode 0 (13bit timer)
– 01 à Mode 1 (16 bit timer)
– 10 à Mode 2 (8 bit timer, with auto-reload)
– 11 à Mode 3 (split timer)
M0M1C/T0Gate0M0M1C/T1Gate1
24-Sep-02 5
Timer: Mode –1
l 16 bit timer
– Load the timer with a number and set TR, to start
counting
– When the counter rolls over to 0x0000, it sets the
TF flag and raises the TF interrupt if enabled
XTAL
TR
TH TL TF
TF=1 if
TH:TL goes from
oxFFFF to 0x0000
oscillator divide by 12
24-Sep-02 6
Timer – Mode 1
l Algorithm
1. Load TMOD register to set mode
2. Load TLx and THx with the initial count values
3. Start timer (setb TRx)
4. Keep monitoring TFx flag (jnb TFx, target)
5. Stop timer (clrb TRx) and clear TFx flag
6. Go back to step 2 to load again
l Time spent
– Telapsed = (65536 - intial_value)*cycle_time
l Instead of polling for TFx flag, an ISR could be used
24-Sep-02 7
Timer : Mode –1 Example
l Generate a
50% duty
cycle
square
wave on
P1.5, with
Timer0
mov TMOD, #01 ;Timer 0, mode 1
Here: mov TL0, #0F2H
mov TH0, #0FFH ;Initial Value = FFF2H
cpl P1.5
acall delay
sjmp Here
Delay: setb TR0 ;start Timer0
Again: jnb TF0, Again ;poll for TF0 (timer overflow)
clr TR0 ;stop timer
clr TF0 ;clear TF0 flag
RET
24-Sep-02 8
Timers and Interrupts
l On timer overflow
interrupt TF1 or
TF0 is generated
l Enable the interrupt
and then start the
timer
l ISR automatically
clears the TFx flag
Program to generate a square wave on
Pin1.2
org 0x0000
ljmp MAIN
org 0x000B ;ISR for T0
cpl P1.2 ;toggle P1.2
mov TL0, #0x55 ;reload T0
mov TH0, #0xAA
reti
org 0x0030
main: mov TMOD, #0000 0001B ;T0, Mode1
mov TL0, #0x55 ;load T0
mov TH0, #0xAA
mov IE, #0x82 ;enable T0 interrupt
setb TR0 ;start T0
here: sjmp here ;wait here until interrupted
24-Sep-02 9
Timer for Time Measurement
l Timer are used to measure elapsed time
– Useful for scheduling routine tasks
– Similar to “cron” functionality
– Not as accurate as an RTC, but cheap !
l Timer’s clock is 1/12 of the 8051 clock
– Time period for one “count” is = 1.085us
– Time spent for a count sequence to “roll-over” is
l Number of counts x 1.085us
l Ex: Timer loaded with 0xFFF2
– Number of counts to rollover to 0x0000 is
0xFFFF-0xFFF2 +1 = 14
– Time elapsed = 14 x 1.085us
Timer0 sequence
FFF2 TF=0
FFF3 TF=0
FFF4 TF=0
….
….
FFFE TF=0
FFFF TF=1
24-Sep-02 10
Time measurement (contd.)
l How to calculate the initial load values for a given time
delay requirement T ?
– Divide T by 1.085us to get n (assumed 11.0592MHz crystal)
– Find m = 65536 – n
– Convert m to hex, m = 0xUUVV
– Load TH ß 0xUU and TL ß 0xVV
l For larger delays ?
– Repeat inside a loop
– Introduce a number of additional instructions (nop), before
enabling the timer again
– Go for an RTC
24-Sep-02 11
Timer : Other modes
l Mode 0
– Exactly like Mode1, but it is a 13bit timer
– Count sequence is from 0x0000 to 0x1FFF
l Mode 2
– 8 bit timer, with auto reload
– Load the count value in TH and enable the timer
– 8051 loads TL with TH (TL ß TH)
– When TL rolls-over to 0x00, timer raises TF flag (and interrupt)
– After TF flag is cleared by ISR / code, TL is automatically
reloaded with TH again and the cycle repeats
– Used in serial communication as baud rate generator
24-Sep-02 12
Timers as Counters
l Counters count how many times a particular event has
occurred
– How many 1’s in a bit stream ?
– How many widgets passed the sensor in an assembly line ?
– How many dogs walked past my doggie door ?
l Counters increment their count when they receive a
signal (count pulse)
l 8051 timers can serve as counters
– C/T bit in TMOD reg has to be 1 for counter operation
– Two external pins on 8051 to give the count pulses
l P3.4 (T0, pin 14) : external count pulse for Timer0
l P3.5 (T1, pin 15) : external count pulse for Timer1
24-Sep-02 13
Counter Example
l Count the pulses on pin T1 (P3.5) and display
the counter value on P2. Counter is in mode 2
START: mov TMOD, #01100000B ;counter 1, mode 2, C/T=1
mov TH1, #0 ;count from 0x00 to 0xFF
setb P3.5 ;configure P3.5 as input
AGAIN: setb TR1 ;enable counter
BACK: mov A, TL1 ;read TL1 value
mov P2, A ;display it on P2
jnb TF1, back ;poll for TF1, could use INT1 also
clr TR1 ;stop counter
clr TF1 ;clear TF1 flag
sjmp AGAIN ;while(1)
24-Sep-02 14
Timers : External Gate
l External gate provides the facility of controlling the
timer with an external device
– Push buttons maybe used to enable/disable timer
– Snooze button in an 8051 based clock !
l Set GATE=1 in TMOD, then the timer can be controlled
from an external pin
– Pin P3.2 (INT0) for Timer0
– Pin P3.3 (INT1) for Timer1
l With GATE=1, Timer is enabled iff
– TRx is set by software (setb TR0)
– AND, INT0 (Pin P3.2) has to be pulled HIGH by hardware
24-Sep-02 15
Next Class
l Serial communication on 8051
l Thanks!

Contenu connexe

Tendances

Using Ready-for-PIC and SDR Libraries
Using Ready-for-PIC and SDR LibrariesUsing Ready-for-PIC and SDR Libraries
Using Ready-for-PIC and SDR LibrariesCorrado Santoro
 
8 interrupt 8051
8 interrupt 80518 interrupt 8051
8 interrupt 8051daniemol
 
Lecture 5 (system clock crossbar and gpio) rv012
Lecture 5 (system clock crossbar and gpio) rv012Lecture 5 (system clock crossbar and gpio) rv012
Lecture 5 (system clock crossbar and gpio) rv012cairo university
 
Lecture 4 (8051 instruction set) rv01
Lecture 4 (8051 instruction set) rv01Lecture 4 (8051 instruction set) rv01
Lecture 4 (8051 instruction set) rv01cairo university
 
8051 serialp port
8051 serialp port8051 serialp port
8051 serialp portTeju Kotti
 
Handling Interrupts in Microchip MCUs
Handling Interrupts in Microchip MCUsHandling Interrupts in Microchip MCUs
Handling Interrupts in Microchip MCUsCorrado Santoro
 
8051 Timers / Counters
8051 Timers / Counters8051 Timers / Counters
8051 Timers / CountersPatricio Lima
 
Programming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontrollerProgramming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontrollerCorrado Santoro
 
Using Timers in PIC18F Microcontrollers
Using Timers in PIC18F MicrocontrollersUsing Timers in PIC18F Microcontrollers
Using Timers in PIC18F MicrocontrollersCorrado Santoro
 
Interfacing with Atmega 16
Interfacing with Atmega 16Interfacing with Atmega 16
Interfacing with Atmega 16Ramadan Ramadan
 
8051 Instruction Set
8051 Instruction Set8051 Instruction Set
8051 Instruction SetStupidsid.com
 
Pt 51 kit - Peripheral self-test
Pt 51 kit - Peripheral self-testPt 51 kit - Peripheral self-test
Pt 51 kit - Peripheral self-testrajbabureliance
 
Microcontroller Instruction Set atmel
Microcontroller Instruction Set atmelMicrocontroller Instruction Set atmel
Microcontroller Instruction Set atmelRuderocker Billy
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Aarav Soni
 

Tendances (20)

Using Ready-for-PIC and SDR Libraries
Using Ready-for-PIC and SDR LibrariesUsing Ready-for-PIC and SDR Libraries
Using Ready-for-PIC and SDR Libraries
 
8051 Presentation
8051 Presentation8051 Presentation
8051 Presentation
 
8 interrupt 8051
8 interrupt 80518 interrupt 8051
8 interrupt 8051
 
8051 Inturrpt
8051 Inturrpt8051 Inturrpt
8051 Inturrpt
 
Lecture 5 (system clock crossbar and gpio) rv012
Lecture 5 (system clock crossbar and gpio) rv012Lecture 5 (system clock crossbar and gpio) rv012
Lecture 5 (system clock crossbar and gpio) rv012
 
Lecture 4 (8051 instruction set) rv01
Lecture 4 (8051 instruction set) rv01Lecture 4 (8051 instruction set) rv01
Lecture 4 (8051 instruction set) rv01
 
8051 serialp port
8051 serialp port8051 serialp port
8051 serialp port
 
8051 archi
8051 archi8051 archi
8051 archi
 
Handling Interrupts in Microchip MCUs
Handling Interrupts in Microchip MCUsHandling Interrupts in Microchip MCUs
Handling Interrupts in Microchip MCUs
 
8051 microcontrollers ch3
8051 microcontrollers ch38051 microcontrollers ch3
8051 microcontrollers ch3
 
8051 Timers / Counters
8051 Timers / Counters8051 Timers / Counters
8051 Timers / Counters
 
Programming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontrollerProgramming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontroller
 
Using Timers in PIC18F Microcontrollers
Using Timers in PIC18F MicrocontrollersUsing Timers in PIC18F Microcontrollers
Using Timers in PIC18F Microcontrollers
 
89c5131datasheet
89c5131datasheet89c5131datasheet
89c5131datasheet
 
Interfacing with Atmega 16
Interfacing with Atmega 16Interfacing with Atmega 16
Interfacing with Atmega 16
 
8051 Instruction Set
8051 Instruction Set8051 Instruction Set
8051 Instruction Set
 
8051 Microcontroller
8051 Microcontroller8051 Microcontroller
8051 Microcontroller
 
Pt 51 kit - Peripheral self-test
Pt 51 kit - Peripheral self-testPt 51 kit - Peripheral self-test
Pt 51 kit - Peripheral self-test
 
Microcontroller Instruction Set atmel
Microcontroller Instruction Set atmelMicrocontroller Instruction Set atmel
Microcontroller Instruction Set atmel
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)
 

En vedette (10)

Class4
Class4Class4
Class4
 
Class6
Class6Class6
Class6
 
Class10
Class10Class10
Class10
 
Class2
Class2Class2
Class2
 
Class11
Class11Class11
Class11
 
Class5
Class5Class5
Class5
 
Chapter 7 8051 programming in c
Chapter 7  8051 programming in cChapter 7  8051 programming in c
Chapter 7 8051 programming in c
 
Microcontroller 8051 and its interfacing
Microcontroller 8051 and its interfacingMicrocontroller 8051 and its interfacing
Microcontroller 8051 and its interfacing
 
8051 Microcontroller Notes
8051 Microcontroller Notes8051 Microcontroller Notes
8051 Microcontroller Notes
 
Class7
Class7Class7
Class7
 

Similaire à Class9

5-Timer Mode 2 Programming-18-03-2024.pptx
5-Timer Mode 2 Programming-18-03-2024.pptx5-Timer Mode 2 Programming-18-03-2024.pptx
5-Timer Mode 2 Programming-18-03-2024.pptxRahultater4
 
6-Interrupts Programming-27-03-2024.pptx
6-Interrupts Programming-27-03-2024.pptx6-Interrupts Programming-27-03-2024.pptx
6-Interrupts Programming-27-03-2024.pptxRahultater4
 
Timer programming
Timer programming Timer programming
Timer programming vijaydeepakg
 
Micro c lab7(timers)
Micro c lab7(timers)Micro c lab7(timers)
Micro c lab7(timers)Mashood
 
Programming 8051 Timers
Programming 8051 Timers Programming 8051 Timers
Programming 8051 Timers ViVek Patel
 
lecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.pptlecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.pptHebaEng
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and CountersShreyans Pathak
 
MICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.pptMICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.pptreemasajin1
 
Timer programming for 8051 using embedded c
Timer programming for 8051 using embedded cTimer programming for 8051 using embedded c
Timer programming for 8051 using embedded cVikas Dongre
 
Microcontrollers-MODULE4.pptx
Microcontrollers-MODULE4.pptxMicrocontrollers-MODULE4.pptx
Microcontrollers-MODULE4.pptxAmoghR3
 
Microprocessor Week 9: Timer and Counter
Microprocessor Week 9: Timer and CounterMicroprocessor Week 9: Timer and Counter
Microprocessor Week 9: Timer and CounterArkhom Jodtang
 

Similaire à Class9 (20)

8051 Timer
8051 Timer8051 Timer
8051 Timer
 
8051 ch9
8051 ch98051 ch9
8051 ch9
 
8051 ch9-950217
8051 ch9-9502178051 ch9-950217
8051 ch9-950217
 
4.Timer_1.ppt
4.Timer_1.ppt4.Timer_1.ppt
4.Timer_1.ppt
 
5-Timer Mode 2 Programming-18-03-2024.pptx
5-Timer Mode 2 Programming-18-03-2024.pptx5-Timer Mode 2 Programming-18-03-2024.pptx
5-Timer Mode 2 Programming-18-03-2024.pptx
 
9 timer programming
9 timer programming9 timer programming
9 timer programming
 
6-Interrupts Programming-27-03-2024.pptx
6-Interrupts Programming-27-03-2024.pptx6-Interrupts Programming-27-03-2024.pptx
6-Interrupts Programming-27-03-2024.pptx
 
UNIT-5.ppt
UNIT-5.pptUNIT-5.ppt
UNIT-5.ppt
 
Timer programming
Timer programming Timer programming
Timer programming
 
8051 timers--2
   8051 timers--2   8051 timers--2
8051 timers--2
 
Micro c lab7(timers)
Micro c lab7(timers)Micro c lab7(timers)
Micro c lab7(timers)
 
Programming 8051 Timers
Programming 8051 Timers Programming 8051 Timers
Programming 8051 Timers
 
lecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.pptlecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.ppt
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and Counters
 
12 mt06ped007
12 mt06ped007 12 mt06ped007
12 mt06ped007
 
MICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.pptMICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.ppt
 
Timer programming for 8051 using embedded c
Timer programming for 8051 using embedded cTimer programming for 8051 using embedded c
Timer programming for 8051 using embedded c
 
Uc
UcUc
Uc
 
Microcontrollers-MODULE4.pptx
Microcontrollers-MODULE4.pptxMicrocontrollers-MODULE4.pptx
Microcontrollers-MODULE4.pptx
 
Microprocessor Week 9: Timer and Counter
Microprocessor Week 9: Timer and CounterMicroprocessor Week 9: Timer and Counter
Microprocessor Week 9: Timer and Counter
 

Dernier

Rums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfRums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfsmsksolar
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projectssmsksolar
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Air Compressor reciprocating single stage
Air Compressor reciprocating single stageAir Compressor reciprocating single stage
Air Compressor reciprocating single stageAbc194748
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...HenryBriggs2
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...soginsider
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersMairaAshraf6
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxmaisarahman1
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxSCMS School of Architecture
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesRAJNEESHKUMAR341697
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 

Dernier (20)

Rums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfRums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdf
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Air Compressor reciprocating single stage
Air Compressor reciprocating single stageAir Compressor reciprocating single stage
Air Compressor reciprocating single stage
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 

Class9

  • 1. Pari vallal Kannan Center for Integrated Circuits and Systems University of Texas at Dallas 8051 Timers Class 7 EE4380 Fall 2002
  • 2. 24-Sep-02 2 Introduction l Timers – Timing devices - Generate specific time delay – Event counter - Count the occurrence of an (external) event l 8051 has Two 16-bit timers T0 and T1 l 8051 Timers can operate in many modes – Timer mode is controlled by TMOD register – Timers are controlled by TCON register (upper 4 bits) l Clock source for the timer is sys_clk/12
  • 3. 24-Sep-02 3 Timer Registers l 8051 timers are made up of two 8-bit registers each l Timer 0 Registers – TL0 and TH0 – Accessed like any other register l mov TL0, #55H, mov R1, TH0 l Timer 1 Registers – TL1 and TH1
  • 4. 24-Sep-02 4 8051 Timer : TMOD Register l Gate - Gating Control (how to start-stop timer) – 0 à software gating (TRx bit in TCON register) – 1 à hardware gating (INTx pin) l C/T - Counter/Timer Operation – 0 à Timer operation (clock is sysclk/12) – 1 à Counter operation (clock is T0 or T1 pin) l M1:M0 - Mode control – 00 à Mode 0 (13bit timer) – 01 à Mode 1 (16 bit timer) – 10 à Mode 2 (8 bit timer, with auto-reload) – 11 à Mode 3 (split timer) M0M1C/T0Gate0M0M1C/T1Gate1
  • 5. 24-Sep-02 5 Timer: Mode –1 l 16 bit timer – Load the timer with a number and set TR, to start counting – When the counter rolls over to 0x0000, it sets the TF flag and raises the TF interrupt if enabled XTAL TR TH TL TF TF=1 if TH:TL goes from oxFFFF to 0x0000 oscillator divide by 12
  • 6. 24-Sep-02 6 Timer – Mode 1 l Algorithm 1. Load TMOD register to set mode 2. Load TLx and THx with the initial count values 3. Start timer (setb TRx) 4. Keep monitoring TFx flag (jnb TFx, target) 5. Stop timer (clrb TRx) and clear TFx flag 6. Go back to step 2 to load again l Time spent – Telapsed = (65536 - intial_value)*cycle_time l Instead of polling for TFx flag, an ISR could be used
  • 7. 24-Sep-02 7 Timer : Mode –1 Example l Generate a 50% duty cycle square wave on P1.5, with Timer0 mov TMOD, #01 ;Timer 0, mode 1 Here: mov TL0, #0F2H mov TH0, #0FFH ;Initial Value = FFF2H cpl P1.5 acall delay sjmp Here Delay: setb TR0 ;start Timer0 Again: jnb TF0, Again ;poll for TF0 (timer overflow) clr TR0 ;stop timer clr TF0 ;clear TF0 flag RET
  • 8. 24-Sep-02 8 Timers and Interrupts l On timer overflow interrupt TF1 or TF0 is generated l Enable the interrupt and then start the timer l ISR automatically clears the TFx flag Program to generate a square wave on Pin1.2 org 0x0000 ljmp MAIN org 0x000B ;ISR for T0 cpl P1.2 ;toggle P1.2 mov TL0, #0x55 ;reload T0 mov TH0, #0xAA reti org 0x0030 main: mov TMOD, #0000 0001B ;T0, Mode1 mov TL0, #0x55 ;load T0 mov TH0, #0xAA mov IE, #0x82 ;enable T0 interrupt setb TR0 ;start T0 here: sjmp here ;wait here until interrupted
  • 9. 24-Sep-02 9 Timer for Time Measurement l Timer are used to measure elapsed time – Useful for scheduling routine tasks – Similar to “cron” functionality – Not as accurate as an RTC, but cheap ! l Timer’s clock is 1/12 of the 8051 clock – Time period for one “count” is = 1.085us – Time spent for a count sequence to “roll-over” is l Number of counts x 1.085us l Ex: Timer loaded with 0xFFF2 – Number of counts to rollover to 0x0000 is 0xFFFF-0xFFF2 +1 = 14 – Time elapsed = 14 x 1.085us Timer0 sequence FFF2 TF=0 FFF3 TF=0 FFF4 TF=0 …. …. FFFE TF=0 FFFF TF=1
  • 10. 24-Sep-02 10 Time measurement (contd.) l How to calculate the initial load values for a given time delay requirement T ? – Divide T by 1.085us to get n (assumed 11.0592MHz crystal) – Find m = 65536 – n – Convert m to hex, m = 0xUUVV – Load TH ß 0xUU and TL ß 0xVV l For larger delays ? – Repeat inside a loop – Introduce a number of additional instructions (nop), before enabling the timer again – Go for an RTC
  • 11. 24-Sep-02 11 Timer : Other modes l Mode 0 – Exactly like Mode1, but it is a 13bit timer – Count sequence is from 0x0000 to 0x1FFF l Mode 2 – 8 bit timer, with auto reload – Load the count value in TH and enable the timer – 8051 loads TL with TH (TL ß TH) – When TL rolls-over to 0x00, timer raises TF flag (and interrupt) – After TF flag is cleared by ISR / code, TL is automatically reloaded with TH again and the cycle repeats – Used in serial communication as baud rate generator
  • 12. 24-Sep-02 12 Timers as Counters l Counters count how many times a particular event has occurred – How many 1’s in a bit stream ? – How many widgets passed the sensor in an assembly line ? – How many dogs walked past my doggie door ? l Counters increment their count when they receive a signal (count pulse) l 8051 timers can serve as counters – C/T bit in TMOD reg has to be 1 for counter operation – Two external pins on 8051 to give the count pulses l P3.4 (T0, pin 14) : external count pulse for Timer0 l P3.5 (T1, pin 15) : external count pulse for Timer1
  • 13. 24-Sep-02 13 Counter Example l Count the pulses on pin T1 (P3.5) and display the counter value on P2. Counter is in mode 2 START: mov TMOD, #01100000B ;counter 1, mode 2, C/T=1 mov TH1, #0 ;count from 0x00 to 0xFF setb P3.5 ;configure P3.5 as input AGAIN: setb TR1 ;enable counter BACK: mov A, TL1 ;read TL1 value mov P2, A ;display it on P2 jnb TF1, back ;poll for TF1, could use INT1 also clr TR1 ;stop counter clr TF1 ;clear TF1 flag sjmp AGAIN ;while(1)
  • 14. 24-Sep-02 14 Timers : External Gate l External gate provides the facility of controlling the timer with an external device – Push buttons maybe used to enable/disable timer – Snooze button in an 8051 based clock ! l Set GATE=1 in TMOD, then the timer can be controlled from an external pin – Pin P3.2 (INT0) for Timer0 – Pin P3.3 (INT1) for Timer1 l With GATE=1, Timer is enabled iff – TRx is set by software (setb TR0) – AND, INT0 (Pin P3.2) has to be pulled HIGH by hardware
  • 15. 24-Sep-02 15 Next Class l Serial communication on 8051 l Thanks!