SlideShare una empresa de Scribd logo
1 de 24
8051 Timer / Counter
 The microcontroller 8051 has two 16 bit Timer/
Counter registers namely Timer 0 (T0) and Timer 1
(T1) .
 When used as a “Timer” the microcontroller is programmed to
count the internal clock pulse.
 When used as a “Counter” the microcontroller is programmed
to count external pulses.
o Maximum count rate is 1/24 of the oscillator frequency.
 The 8051 has 2 timers/counters:
◦ Timer/Counter 0
◦ Timer/Counter 1
 Registers Used in the Timer :
◦ Timer 0 registers:
TH0, TL0
Exclusive
◦ Timer 1 registers:
TH1, TL1
◦ TMOD (Timer mode register)
Shared by both
◦ TCON (Timer control register)
 Registers THx & TLx
They are 16 bit wide.
 These registers store:
The time delay as a timer.
The number of events as a counter.
 Timer 0: TH0 & TL0
Timer 0 high byte , timer 0 low byte
 Timer 1: TH1 & TL1
Timer 1 high byte, timer 1 low byte
Timer Registers
D15 D8D9D10D11D12D13D14 D7 D0D1D2D3D4D5D6
TH0 TL0
D15 D8D9D10D11D12D13D14 D7 D0D1D2D3D4D5D6
TH1 TL1
Timer 0
Timer 1
 Timer mode register = TMOD
◦ An 8-bit register
 lower 4 bits : Timer 0 Mode setting (0000 : not used)
 upper 4 bits : Timer 1 Mode setting (0000 : not used)
◦ Not bit-addressable
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0
(MSB) (LSB)
BIT NAME EXPLANATION OF THE FUNCTION TIMER
7 GATE1
When this bit is set the timer will only run when
INT1 (P3.3) is high. When this bit is clear the
timer will run regardless of the state of INT1.
1
6 C/T1
When this bit is set the timer will count events
on T1 (P3.5). When this bit is clear the timer
will be incremented every machine cycle.
1
5 T1M1 Timer mode bit 1
4 T1M0 Timer mode bit 1
3 GATE0
When this bit is set the timer will only run when
INT0 (P3.2) is high. When this bit is clear the
timer will run regardless of the state of INT0.
0
2 C/T0
When this bit is set the timer will count events
on T0 (P3.4). When this bit is clear the timer
will be incremented every machine cycle.
0
1 T0M1 Timer mode bit 0
0 T0M0 Timer mode bit 0
0 : Timer operation
(clock : Machine cycle)
1 : Counter operation
(clock : Tx input pin)
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0
(MSB) (LSB)
 M0 and M1 select the timer mode for timers 0 & 1.
M1 M0 Mode Operating Mode
0 0 0 13-bit timer mode
8-bit THx + 5-bit TLx (x= 0 or 1)
0 1 1 16-bit timer mode
8-bit THx + 8-bit TLx
1 0 2 8-bit auto reload
8-bit auto reload timer/counter;
THx holds a value which is to be reloaded
into TLx each time it overflows.
1 1 3 Split timer mode
TxM1 TxM0 Timer
Mode
Description of Mode
0 0 0 13-bit Timer
0 1 1 16-bit Timer
1 0 2 8-bit Auto Reload
1 1 3 Split Timer Mode
4
Operating
Modes
 This is a relic mode.
◦ Included in 8051 to maintain compatibility with its
predecessor 8048.
 The counters are counting up:
◦ TLx will count from 0 to 31.
◦ When TLx is incremented from 31, it will “reset”
(overflow) to 0.
◦ Now THx will be incremented.
 Hence effectively only 13 bits are used.
◦ Bits 0-4 of TLx.
◦ Bits 0-7 of THx..
 This is the most commonly used mode.
 This mode operates in a fashion almost like the Mode 0,
only this time all 16 bits are used.
 The counting:
◦ TLx is incremented from 0(00h) to 255(FFh).
◦ When TLx is incremented from 255, it resets to 0 and
causes THx to be incremented by 1.
◦ Hence we have a maximum count of ‘65,025’
(255*255) machine cycles.
÷ 12
TR
TH TL TF
Timer
overflow
flag
C/T = 0
TF goes high when FFFF 0
XTAL
oscillator
 When a timer is in mode 2, THx holds the "reload value"
and TLx is the timer itself.
 Thus the counting proceeds as:
◦ TLx starts counting up.
◦ TLx reaches 255 and is subsequently incremented.
◦ Now instead of resetting to 0 (as in the case of modes
0 and 1), it will be reset to the value stored in THx.
XTAL
oscillator ÷ 12
TR1
TL1
TH1
TF1 overflow flag
reload
C/T = 0
 When Timer 0 is placed in mode 3, it essentially becomes two
separate 8-bit timers.
 That is to say, Timer 0 is TL0 and Timer 1 is TH0.
Both timers count from 0 to 255 and overflow back to 0
independently.
 What happens to timer1?
◦ All the bits that are related to Timer 1 will now be tied to TH0.
◦ While Timer 0 is in split mode, the real Timer 1 (i.e. TH1 and TL1)
can be put into modes 0, 1 or 2 normally.
◦ However, you may not start or stop the real timer 1 since the bits
that do that are now linked to TH0.
◦ The real timer 1, in this case, will be incremented every machine
cycle no matter what.
 Finally, there is one more SFR that controls the two timers and
provides valuable information about them.
 Timer control register: TCON
◦ Upper nibble : TIMER
◦ Lower nibble : INTERRUPTS
The Register bits represent the following values :
BIT NAME BIT ADDRESS EXPLANATION OF THE
FUNCTION
TIMER
7 TF1 8Fh
Timer 1 Overflow. This
bit is set by the
microcontroller when
Timer 1 overflows.
1
6 TR1 8Eh
Timer 1 Run.
When this bit is set
Timer 1 is turned on.
When this bit is clear
Timer 1 is off.
1
5 TF0 8Dh
Timer 0 Overflow. This
bit is set by the
microcontroller when
Timer 0 overflows.
0
4 TR0 8Ch
Timer 0 Run.
When this bit is set
Timer 0 is turned on.
When this bit is clear
Timer 0 is off.
0
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer 1 Timer0 for Interrupt
(MSB) (LSB)
 As far as the use of a timer/counter as an event counter is
concerned ,everything that we have talked about in the last
section also applies to programming it as a counter ,except
the source of the frequency.
 When used as a timer ,the 8051’s crystal is used as the
source of the frequency.
 However ,when used as a counter ,it is a pulse outside of the
8051 that increments the TH,TL registers.
 These timers can also be used as counters counting
events happening outside the 8051.
Pin Port Pin Function Description
14 P3.4 T0 Timer/Counter 0 external input
15 P3.5 T1 Timer/Counter 1 external input
GATE C/T=1 M1 M0 GATE C/T=1 M1 M0
Timer 1 Timer 0
(MSB) (LSB)
8051 timer counter

Más contenido relacionado

La actualidad más candente

Programming 8051 Timers
Programming 8051 Timers Programming 8051 Timers
Programming 8051 Timers
ViVek Patel
 
8051 Timers / Counters
8051 Timers / Counters8051 Timers / Counters
8051 Timers / Counters
Patricio Lima
 

La actualidad más candente (20)

Interrupt programming with 8051 microcontroller
Interrupt programming with 8051  microcontrollerInterrupt programming with 8051  microcontroller
Interrupt programming with 8051 microcontroller
 
Architecture of 8051
Architecture of 8051Architecture of 8051
Architecture of 8051
 
microcontroller vs microprocessor
microcontroller vs microprocessormicrocontroller vs microprocessor
microcontroller vs microprocessor
 
Timing diagram 8085 microprocessor
Timing diagram 8085 microprocessorTiming diagram 8085 microprocessor
Timing diagram 8085 microprocessor
 
Pic microcontroller architecture
Pic microcontroller architecturePic microcontroller architecture
Pic microcontroller architecture
 
Programming 8051 Timers
Programming 8051 Timers Programming 8051 Timers
Programming 8051 Timers
 
8086 pin details
8086 pin details8086 pin details
8086 pin details
 
8051 ch9-950217
8051 ch9-9502178051 ch9-950217
8051 ch9-950217
 
8259 Programmable Interrupt Controller
8259 Programmable Interrupt Controller8259 Programmable Interrupt Controller
8259 Programmable Interrupt Controller
 
8051 Timers / Counters
8051 Timers / Counters8051 Timers / Counters
8051 Timers / Counters
 
ARM Processors
ARM ProcessorsARM Processors
ARM Processors
 
8051 MICROCONTROLLER ARCHITECTURE.pptx
 8051 MICROCONTROLLER ARCHITECTURE.pptx 8051 MICROCONTROLLER ARCHITECTURE.pptx
8051 MICROCONTROLLER ARCHITECTURE.pptx
 
INTRODUCTION TO MICROCONTROLLER
INTRODUCTION TO MICROCONTROLLERINTRODUCTION TO MICROCONTROLLER
INTRODUCTION TO MICROCONTROLLER
 
Digital electronics logic families
Digital electronics logic familiesDigital electronics logic families
Digital electronics logic families
 
8051 MICROCONTROLLER
8051 MICROCONTROLLER 8051 MICROCONTROLLER
8051 MICROCONTROLLER
 
UART
UARTUART
UART
 
Interfacing Stepper motor with 8051
Interfacing Stepper motor with 8051Interfacing Stepper motor with 8051
Interfacing Stepper motor with 8051
 
8251 USART
8251 USART8251 USART
8251 USART
 
MICROCONTROLLER 8051- Architecture & Pin Configuration
MICROCONTROLLER 8051- Architecture & Pin Configuration MICROCONTROLLER 8051- Architecture & Pin Configuration
MICROCONTROLLER 8051- Architecture & Pin Configuration
 
8051 Assembly Language Programming
8051 Assembly Language Programming8051 Assembly Language Programming
8051 Assembly Language Programming
 

Similar a 8051 timer counter

8051 timer counter
8051 timer counter8051 timer counter
8051 timer counter
ankit3991
 
lecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.pptlecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.ppt
HebaEng
 

Similar a 8051 timer counter (20)

8051 timer counter
8051 timer counter8051 timer counter
8051 timer counter
 
8051 Microcontroller Timer
8051 Microcontroller Timer8051 Microcontroller Timer
8051 Microcontroller Timer
 
lecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.pptlecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.ppt
 
8051 timers
8051 timers8051 timers
8051 timers
 
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
 
8051 ch9
8051 ch98051 ch9
8051 ch9
 
Timers
TimersTimers
Timers
 
Timers
TimersTimers
Timers
 
8051 timers--2
   8051 timers--2   8051 timers--2
8051 timers--2
 
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
 
Timers
TimersTimers
Timers
 
TIMERS.pptx
TIMERS.pptxTIMERS.pptx
TIMERS.pptx
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and Counters
 
8051 Timer
8051 Timer8051 Timer
8051 Timer
 
4.Timer_1.ppt
4.Timer_1.ppt4.Timer_1.ppt
4.Timer_1.ppt
 
Timer And Counter in 8051 Microcontroller
Timer And Counter in 8051 MicrocontrollerTimer And Counter in 8051 Microcontroller
Timer And Counter in 8051 Microcontroller
 
UNIT-5.ppt
UNIT-5.pptUNIT-5.ppt
UNIT-5.ppt
 
9 timer programming
9 timer programming9 timer programming
9 timer programming
 
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
 

Más de vishalgohel12195

Más de vishalgohel12195 (20)

Variable frequency drive and variable frequency control
Variable frequency drive and variable frequency controlVariable frequency drive and variable frequency control
Variable frequency drive and variable frequency control
 
Self control of synchronous motor drives
Self control of synchronous motor drivesSelf control of synchronous motor drives
Self control of synchronous motor drives
 
Owen’s bridge and measurement of increment inductance
Owen’s bridge and measurement of increment inductanceOwen’s bridge and measurement of increment inductance
Owen’s bridge and measurement of increment inductance
 
Initial and final condition for circuit
Initial and final condition for circuitInitial and final condition for circuit
Initial and final condition for circuit
 
FSK , FM DEMODULATOR & VOLTAGE REGULATOR ICS
FSK , FM DEMODULATOR & VOLTAGE REGULATOR ICS  FSK , FM DEMODULATOR & VOLTAGE REGULATOR ICS
FSK , FM DEMODULATOR & VOLTAGE REGULATOR ICS
 
Differential equations of first order
Differential equations of first orderDifferential equations of first order
Differential equations of first order
 
Cost for production including breaking analysis
Cost for production including breaking  analysisCost for production including breaking  analysis
Cost for production including breaking analysis
 
Concept of general terms pertaining to rotating machines
Concept of general terms pertaining to rotating machinesConcept of general terms pertaining to rotating machines
Concept of general terms pertaining to rotating machines
 
Transfer function and mathematical modeling
Transfer  function  and  mathematical  modelingTransfer  function  and  mathematical  modeling
Transfer function and mathematical modeling
 
armature Winding
armature Windingarmature Winding
armature Winding
 
Switching regulators
Switching regulatorsSwitching regulators
Switching regulators
 
Sag in overhead transmission line, sag calculation & string chart
Sag in overhead transmission line, sag   calculation & string chartSag in overhead transmission line, sag   calculation & string chart
Sag in overhead transmission line, sag calculation & string chart
 
Rectifier
RectifierRectifier
Rectifier
 
Protection against overvoltage
Protection against overvoltageProtection against overvoltage
Protection against overvoltage
 
Pantograph,catenary wire,smothing reactor
Pantograph,catenary wire,smothing reactorPantograph,catenary wire,smothing reactor
Pantograph,catenary wire,smothing reactor
 
On load tap changer in a.c. locomotive transformer & air blast circuit b...
On load tap changer in a.c. locomotive transformer  & air blast circuit b...On load tap changer in a.c. locomotive transformer  & air blast circuit b...
On load tap changer in a.c. locomotive transformer & air blast circuit b...
 
Logical instruction of 8085
Logical instruction of 8085Logical instruction of 8085
Logical instruction of 8085
 
Effect of non sinusoidal waveform of a.c. machine performance
Effect of non sinusoidal waveform of a.c. machine performanceEffect of non sinusoidal waveform of a.c. machine performance
Effect of non sinusoidal waveform of a.c. machine performance
 
Disadvantages of corona, radio interference, inductive interference between p...
Disadvantages of corona, radio interference, inductive interference between p...Disadvantages of corona, radio interference, inductive interference between p...
Disadvantages of corona, radio interference, inductive interference between p...
 
Design, Planning and Layout of high voltage laboratory
Design, Planning and Layout of high voltage laboratory Design, Planning and Layout of high voltage laboratory
Design, Planning and Layout of high voltage laboratory
 

Último

UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Último (20)

(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 

8051 timer counter

  • 1. 8051 Timer / Counter
  • 2.
  • 3.  The microcontroller 8051 has two 16 bit Timer/ Counter registers namely Timer 0 (T0) and Timer 1 (T1) .  When used as a “Timer” the microcontroller is programmed to count the internal clock pulse.  When used as a “Counter” the microcontroller is programmed to count external pulses. o Maximum count rate is 1/24 of the oscillator frequency.
  • 4.  The 8051 has 2 timers/counters: ◦ Timer/Counter 0 ◦ Timer/Counter 1  Registers Used in the Timer : ◦ Timer 0 registers: TH0, TL0 Exclusive ◦ Timer 1 registers: TH1, TL1 ◦ TMOD (Timer mode register) Shared by both ◦ TCON (Timer control register)
  • 5.  Registers THx & TLx They are 16 bit wide.  These registers store: The time delay as a timer. The number of events as a counter.  Timer 0: TH0 & TL0 Timer 0 high byte , timer 0 low byte  Timer 1: TH1 & TL1 Timer 1 high byte, timer 1 low byte
  • 6. Timer Registers D15 D8D9D10D11D12D13D14 D7 D0D1D2D3D4D5D6 TH0 TL0 D15 D8D9D10D11D12D13D14 D7 D0D1D2D3D4D5D6 TH1 TL1 Timer 0 Timer 1
  • 7.  Timer mode register = TMOD ◦ An 8-bit register  lower 4 bits : Timer 0 Mode setting (0000 : not used)  upper 4 bits : Timer 1 Mode setting (0000 : not used) ◦ Not bit-addressable
  • 8. GATE C/T M1 M0 GATE C/T M1 M0 Timer 1 Timer 0 (MSB) (LSB) BIT NAME EXPLANATION OF THE FUNCTION TIMER 7 GATE1 When this bit is set the timer will only run when INT1 (P3.3) is high. When this bit is clear the timer will run regardless of the state of INT1. 1 6 C/T1 When this bit is set the timer will count events on T1 (P3.5). When this bit is clear the timer will be incremented every machine cycle. 1 5 T1M1 Timer mode bit 1 4 T1M0 Timer mode bit 1 3 GATE0 When this bit is set the timer will only run when INT0 (P3.2) is high. When this bit is clear the timer will run regardless of the state of INT0. 0 2 C/T0 When this bit is set the timer will count events on T0 (P3.4). When this bit is clear the timer will be incremented every machine cycle. 0 1 T0M1 Timer mode bit 0 0 T0M0 Timer mode bit 0
  • 9. 0 : Timer operation (clock : Machine cycle) 1 : Counter operation (clock : Tx input pin) GATE C/T M1 M0 GATE C/T M1 M0 Timer 1 Timer 0 (MSB) (LSB)
  • 10.  M0 and M1 select the timer mode for timers 0 & 1. M1 M0 Mode Operating Mode 0 0 0 13-bit timer mode 8-bit THx + 5-bit TLx (x= 0 or 1) 0 1 1 16-bit timer mode 8-bit THx + 8-bit TLx 1 0 2 8-bit auto reload 8-bit auto reload timer/counter; THx holds a value which is to be reloaded into TLx each time it overflows. 1 1 3 Split timer mode
  • 11. TxM1 TxM0 Timer Mode Description of Mode 0 0 0 13-bit Timer 0 1 1 16-bit Timer 1 0 2 8-bit Auto Reload 1 1 3 Split Timer Mode 4 Operating Modes
  • 12.  This is a relic mode. ◦ Included in 8051 to maintain compatibility with its predecessor 8048.  The counters are counting up: ◦ TLx will count from 0 to 31. ◦ When TLx is incremented from 31, it will “reset” (overflow) to 0. ◦ Now THx will be incremented.  Hence effectively only 13 bits are used. ◦ Bits 0-4 of TLx. ◦ Bits 0-7 of THx..
  • 13.  This is the most commonly used mode.  This mode operates in a fashion almost like the Mode 0, only this time all 16 bits are used.  The counting: ◦ TLx is incremented from 0(00h) to 255(FFh). ◦ When TLx is incremented from 255, it resets to 0 and causes THx to be incremented by 1. ◦ Hence we have a maximum count of ‘65,025’ (255*255) machine cycles.
  • 14. ÷ 12 TR TH TL TF Timer overflow flag C/T = 0 TF goes high when FFFF 0 XTAL oscillator
  • 15.  When a timer is in mode 2, THx holds the "reload value" and TLx is the timer itself.  Thus the counting proceeds as: ◦ TLx starts counting up. ◦ TLx reaches 255 and is subsequently incremented. ◦ Now instead of resetting to 0 (as in the case of modes 0 and 1), it will be reset to the value stored in THx.
  • 16. XTAL oscillator ÷ 12 TR1 TL1 TH1 TF1 overflow flag reload C/T = 0
  • 17.  When Timer 0 is placed in mode 3, it essentially becomes two separate 8-bit timers.  That is to say, Timer 0 is TL0 and Timer 1 is TH0. Both timers count from 0 to 255 and overflow back to 0 independently.  What happens to timer1? ◦ All the bits that are related to Timer 1 will now be tied to TH0. ◦ While Timer 0 is in split mode, the real Timer 1 (i.e. TH1 and TL1) can be put into modes 0, 1 or 2 normally. ◦ However, you may not start or stop the real timer 1 since the bits that do that are now linked to TH0. ◦ The real timer 1, in this case, will be incremented every machine cycle no matter what.
  • 18.
  • 19.  Finally, there is one more SFR that controls the two timers and provides valuable information about them.  Timer control register: TCON ◦ Upper nibble : TIMER ◦ Lower nibble : INTERRUPTS
  • 20. The Register bits represent the following values : BIT NAME BIT ADDRESS EXPLANATION OF THE FUNCTION TIMER 7 TF1 8Fh Timer 1 Overflow. This bit is set by the microcontroller when Timer 1 overflows. 1 6 TR1 8Eh Timer 1 Run. When this bit is set Timer 1 is turned on. When this bit is clear Timer 1 is off. 1 5 TF0 8Dh Timer 0 Overflow. This bit is set by the microcontroller when Timer 0 overflows. 0 4 TR0 8Ch Timer 0 Run. When this bit is set Timer 0 is turned on. When this bit is clear Timer 0 is off. 0
  • 21. TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 Timer 1 Timer0 for Interrupt (MSB) (LSB)
  • 22.  As far as the use of a timer/counter as an event counter is concerned ,everything that we have talked about in the last section also applies to programming it as a counter ,except the source of the frequency.  When used as a timer ,the 8051’s crystal is used as the source of the frequency.  However ,when used as a counter ,it is a pulse outside of the 8051 that increments the TH,TL registers.  These timers can also be used as counters counting events happening outside the 8051.
  • 23. Pin Port Pin Function Description 14 P3.4 T0 Timer/Counter 0 external input 15 P3.5 T1 Timer/Counter 1 external input GATE C/T=1 M1 M0 GATE C/T=1 M1 M0 Timer 1 Timer 0 (MSB) (LSB)