SlideShare a Scribd company logo
Module-03
Timers and Counters in 8051
Introduction
The 8051 microcontroller has two 16-bit timer/counter registers, Timer 0
(P3.4) and Timer 1(P3.5). They can be used either as Timers to generate a time
delay or as Event counters to count events happening outside the
microcontroller.
Application of Timers & Counters
•Timers:
• Generating accurate delays
• PWM signal generation
• Event scheduling
•Counters:
• Counting external pulses (e.g., sensor triggers)
• Measuring the frequency of external signals
• Counting events in automation systems
Real-Time Application of Timer (7 Seg Display)
Real-Time Application of Counters (Heart
rate monitoring)
Timer 0 and Timer 1
Timer 0 and Timer 1 are 16-bits wide. Since 8051 has an 8-bit architecture, each
16-bit timer is accessed as two separate registers of low byte and high byte.
The low-byte register is called TL0/TL1 and The high-byte register is called
TH0/TH1. They can be accessed like any other register.
Differences b/w Timers and counters
Timer/Counter Registers
Timer/Counter Registers
•TMOD (Timer Mode Register): Configures the mode and operation of Timer 0
and Timer 1.
•TCON (Timer Control Register): Contains control flags for starting, stopping,
and checking overflow.
•TH & TL (Timer High and Timer Low Registers): Store the timer/counter
values.
Module-03 Timers and serial port communication
Timer Control (TCON) Register
• TCON register is also one of the registers whose bits are directly in
control of timer operation.
• Only 4 bits of this register are used for this purpose, while rest of them is
used for interrupt control to be discussed later.
TF1 bit is automatically set on the Timer 1 overflow.
TR1 bit enables the Timer 1.
• 1 – Timer 1 is enabled.
• 0 – Timer 1 is disabled.
TF0 bit is automatically set on the Timer 0 overflow.
TR0 bit enables the timer 0.
• 1 – Timer 0 is enabled.
• 0 – Timer 0 is disabled.
Timer Control (TCON) Register
TMOD Register (Timer Mode)
• The TMOD register selects the operational mode of the timers
T0 and T1.
• The low 4 bits (bit0 – bit3) refer to the timer 0, while the high
4 bits (bit4 – bit7) refer to the timer 1.
• GATE1 enables and disables Timer 1 by means of a signal
brought to the INT1 pin (P3.3):
• 1 – Timer 1 operates only if the INT1 bit is set.
• 0 – Timer 1 operates regardless of the logic state of the
INT1 bit.
• C/T1 selects pulses to be counted up by the timer/counter 1:
• 1 – Timer counts pulses brought to the T1 pin (P3.5).
• 0 – Timer counts pulses from internal oscillator.
• T1M1, T1M0 These two bits select the operational mode of the
Timer 1.
TMOD Register (Timer Mode)
TMOD Register (Timer Mode)
GATE0 enables and disables Timer 0 using a signal brought to
the INT0 pin (P3.2):
• 1 – Timer 0 operates only if the INT0 bit is set.
• 0 – Timer 0 operates regardless of the logic state of the
INT0 bit.
• C/T0 selects pulses to be counted up by the timer/counter 0:
• 1 – Timer counts pulses brought to the T0 pin (P3.4).
• 0 – Timer counts pulses from internal oscillator.
• T0M1, T0M0 These two bits select the operational mode of the
Timer 0.
TMOD Register (Timer Mode)
TMOD Register (Timer Mode)
Timer register Rolls Over
Timer Hardware in 8051
Mode-0
Mode-0
Mode-1
Mode-2
Mode-3
Summary of Modes in the Timers
How to load the Timer registers with the required time delay
Mode 1 (16 bit timer/counter)
Step:1
Value to be loaded in the Timer = FFFF – (delay Value) + 1
Step:2
load into THx and TLx register
Mode 0 (13 bit timer/counter)
Step:1
Value to be loaded in Timer = 1FFF – (Delay Value) +1
Step:2
load into THx and TLx register
Steps for programming timers in 8051
Load the TMOD value register indicating which timer (0 or 1) is to be used
and which timer mode is selected.
Load registers TL and TH with initial count values.
Start the timer by the instruction “SETB TR0” for timer 0 and “SETB TR1
for timer 1.
Keep monitoring the timer flag (TF) with the “JNB TFx, target” instruction to
see if it is raised.
Stop the timer with the instructions “CLR TR0” or “CLR TR1”, for timer 0 and
timer 1, respectively.
Clear the TF flag for the next round with the instruction “CLR TF0” or “CLR
TF1”, for timer 0 and timer 1, respectively.
Go back to step 2 to load TH and TL again.
Data Types and Time Delay
in the 8051 using C
Data Types and Time Delay in the 8051 using C
Why Program the 8051 using C Language???
Compilers produce hex files that is downloaded to ROM of
microcontroller
The size of hex file is the main concern
Microcontrollers have limited on-chip ROM
Code space for 8051 is limited to 64K bytes
C programming is less time consuming, but has larger hex file size
 The reasons for writing programs in C
 It is easier and less time consuming to write in C than Assembly
 C is easier to modify and update
 We can use code available in function libraries
 C code is portable to other microcontroller with little modification
 A good understanding of C data types for 8051 can help programmers
to create smaller hex files
 Unsigned char
 Signed char
 Unsigned int
 Signed int
 Sbit (single bit)
 Bit and sfr
Data Types and Time Delay in the 8051 using C
Data Types and Time Delay in the 8051 using C
Data Types and Time Delay in the 8051 using C
Data Types and Time Delay in the 8051 using C
Data Types and Time Delay in the 8051 using C
Data Types and Time Delay in the 8051 using C
Data Types and Time Delay in the 8051 using C
Data Types and Time Delay in the 8051 using C
Data Types and Time Delay in the 8051 using C
Data Types and Time Delay in the 8051 using C
Data Types and Time Delay in the 8051 using C
Thank You

More Related Content

PPTX
Timers
PPTX
6-Interrupts Programming-27-03-2024.pptx
PDF
Timer And Counter in 8051 Microcontroller
PPT
8051 ch9-950217
PPTX
TIMERS.pptx
PPTX
5th unit embedded system and iot design timer and controller
PPTX
Timer programming for 8051 using embedded c
PPT
Microcontroller 8051 timer and counter module
Timers
6-Interrupts Programming-27-03-2024.pptx
Timer And Counter in 8051 Microcontroller
8051 ch9-950217
TIMERS.pptx
5th unit embedded system and iot design timer and controller
Timer programming for 8051 using embedded c
Microcontroller 8051 timer and counter module

Similar to Module-03 Timers and serial port communication (20)

PPTX
8051 MICROCONTROLLER TIMER AND ITS APPLICATIONS
PPT
Timers
PPT
UNIT-5.ppt
PPT
MICROCONTROLLER TIMERS.ppt
PPT
Programming 8051 Timers
PPTX
5-Timer Mode 2 Programming-18-03-2024.pptx
PPTX
KTU_Microprocessor and Microcontrollers_Module2
PPT
8051 ch9
PDF
UNIT 1 8 BIT EMBEDDED PROCESSOR CS3691 6TH SEM CSE
PPTX
89C51 PROGRAMMING in Unit-4 of Microprocessor
PPTX
8051 timer counter
PPT
4.Timer_1.ppt
PPTX
8051 Timers and Counters
PPT
MICROCONTROLLER TIMERS.GHGHGHGHGHGHGHGHGppt
PPTX
8051 timer counter
PDF
PPT
12 mt06ped007
PDF
8051 Timers, Interrupts and Serial Communication
PPT
8051 Timer
8051 MICROCONTROLLER TIMER AND ITS APPLICATIONS
Timers
UNIT-5.ppt
MICROCONTROLLER TIMERS.ppt
Programming 8051 Timers
5-Timer Mode 2 Programming-18-03-2024.pptx
KTU_Microprocessor and Microcontrollers_Module2
8051 ch9
UNIT 1 8 BIT EMBEDDED PROCESSOR CS3691 6TH SEM CSE
89C51 PROGRAMMING in Unit-4 of Microprocessor
8051 timer counter
4.Timer_1.ppt
8051 Timers and Counters
MICROCONTROLLER TIMERS.GHGHGHGHGHGHGHGHGppt
8051 timer counter
12 mt06ped007
8051 Timers, Interrupts and Serial Communication
8051 Timer
Ad

More from KiranG764628 (19)

PPTX
E-WASTE contents and their avoidness on the earth
PPTX
TIME(BEC501) Course File Presentation.pptx
PPT
Module-01 8051 Microcontroller for engineering
PPT
Module-02 Instruction Set in 8051 and their usage
PPT
Module-01 8051 Microcontroller presentation
PPTX
Unit_2 Sensors used in automobiles for Mechatronics Engineering
PPT
Automotive_Electronics_for Mechatronics Engineering
PPTX
Successive approximation Analog to Digital Convertor
PPT
8051 Microcontroller slides foe electronics
PPTX
Clippers and clampers for electronics engineering
PPT
Resistor Capacitor coupled amplifier for electronics
PPTX
UNIT-1 Metal oxide semconductor for electronics
PPTX
Clippers and clampers for electronics engineering
PPT
Resistor Capacitor Coupled Amplifier Basics and Design
PPT
Junction Field Effect Transistor Basics for Electronics Engineering
PPTX
Metal Oxide Semiconductor Field Effect Transistor
PPTX
Transistor fundamentals: Bipolar junction Transistor (BJT)
PPTX
Automotive Electronics for Mechanical Engineering
PDF
Use of Electronics In The Automobile, Antilock Brake Systems, (ABS), Electron...
E-WASTE contents and their avoidness on the earth
TIME(BEC501) Course File Presentation.pptx
Module-01 8051 Microcontroller for engineering
Module-02 Instruction Set in 8051 and their usage
Module-01 8051 Microcontroller presentation
Unit_2 Sensors used in automobiles for Mechatronics Engineering
Automotive_Electronics_for Mechatronics Engineering
Successive approximation Analog to Digital Convertor
8051 Microcontroller slides foe electronics
Clippers and clampers for electronics engineering
Resistor Capacitor coupled amplifier for electronics
UNIT-1 Metal oxide semconductor for electronics
Clippers and clampers for electronics engineering
Resistor Capacitor Coupled Amplifier Basics and Design
Junction Field Effect Transistor Basics for Electronics Engineering
Metal Oxide Semiconductor Field Effect Transistor
Transistor fundamentals: Bipolar junction Transistor (BJT)
Automotive Electronics for Mechanical Engineering
Use of Electronics In The Automobile, Antilock Brake Systems, (ABS), Electron...
Ad

Recently uploaded (20)

PDF
PPT on Performance Review to get promotions
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
composite construction of structures.pdf
PPTX
additive manufacturing of ss316l using mig welding
PDF
Digital Logic Computer Design lecture notes
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Welding lecture in detail for understanding
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Construction Project Organization Group 2.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Well-logging-methods_new................
PPTX
web development for engineering and engineering
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPT on Performance Review to get promotions
Model Code of Practice - Construction Work - 21102022 .pdf
Lesson 3_Tessellation.pptx finite Mathematics
composite construction of structures.pdf
additive manufacturing of ss316l using mig welding
Digital Logic Computer Design lecture notes
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
UNIT 4 Total Quality Management .pptx
Welding lecture in detail for understanding
CYBER-CRIMES AND SECURITY A guide to understanding
Construction Project Organization Group 2.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Well-logging-methods_new................
web development for engineering and engineering
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd

Module-03 Timers and serial port communication

  • 2. Introduction The 8051 microcontroller has two 16-bit timer/counter registers, Timer 0 (P3.4) and Timer 1(P3.5). They can be used either as Timers to generate a time delay or as Event counters to count events happening outside the microcontroller. Application of Timers & Counters •Timers: • Generating accurate delays • PWM signal generation • Event scheduling •Counters: • Counting external pulses (e.g., sensor triggers) • Measuring the frequency of external signals • Counting events in automation systems
  • 3. Real-Time Application of Timer (7 Seg Display)
  • 4. Real-Time Application of Counters (Heart rate monitoring)
  • 5. Timer 0 and Timer 1 Timer 0 and Timer 1 are 16-bits wide. Since 8051 has an 8-bit architecture, each 16-bit timer is accessed as two separate registers of low byte and high byte. The low-byte register is called TL0/TL1 and The high-byte register is called TH0/TH1. They can be accessed like any other register.
  • 6. Differences b/w Timers and counters
  • 7. Timer/Counter Registers Timer/Counter Registers •TMOD (Timer Mode Register): Configures the mode and operation of Timer 0 and Timer 1. •TCON (Timer Control Register): Contains control flags for starting, stopping, and checking overflow. •TH & TL (Timer High and Timer Low Registers): Store the timer/counter values.
  • 9. Timer Control (TCON) Register • TCON register is also one of the registers whose bits are directly in control of timer operation. • Only 4 bits of this register are used for this purpose, while rest of them is used for interrupt control to be discussed later.
  • 10. TF1 bit is automatically set on the Timer 1 overflow. TR1 bit enables the Timer 1. • 1 – Timer 1 is enabled. • 0 – Timer 1 is disabled. TF0 bit is automatically set on the Timer 0 overflow. TR0 bit enables the timer 0. • 1 – Timer 0 is enabled. • 0 – Timer 0 is disabled. Timer Control (TCON) Register
  • 11. TMOD Register (Timer Mode) • The TMOD register selects the operational mode of the timers T0 and T1. • The low 4 bits (bit0 – bit3) refer to the timer 0, while the high 4 bits (bit4 – bit7) refer to the timer 1.
  • 12. • GATE1 enables and disables Timer 1 by means of a signal brought to the INT1 pin (P3.3): • 1 – Timer 1 operates only if the INT1 bit is set. • 0 – Timer 1 operates regardless of the logic state of the INT1 bit. • C/T1 selects pulses to be counted up by the timer/counter 1: • 1 – Timer counts pulses brought to the T1 pin (P3.5). • 0 – Timer counts pulses from internal oscillator. • T1M1, T1M0 These two bits select the operational mode of the Timer 1. TMOD Register (Timer Mode)
  • 14. GATE0 enables and disables Timer 0 using a signal brought to the INT0 pin (P3.2): • 1 – Timer 0 operates only if the INT0 bit is set. • 0 – Timer 0 operates regardless of the logic state of the INT0 bit. • C/T0 selects pulses to be counted up by the timer/counter 0: • 1 – Timer counts pulses brought to the T0 pin (P3.4). • 0 – Timer counts pulses from internal oscillator. • T0M1, T0M0 These two bits select the operational mode of the Timer 0. TMOD Register (Timer Mode)
  • 23. Summary of Modes in the Timers
  • 24. How to load the Timer registers with the required time delay Mode 1 (16 bit timer/counter) Step:1 Value to be loaded in the Timer = FFFF – (delay Value) + 1 Step:2 load into THx and TLx register Mode 0 (13 bit timer/counter) Step:1 Value to be loaded in Timer = 1FFF – (Delay Value) +1 Step:2 load into THx and TLx register
  • 25. Steps for programming timers in 8051 Load the TMOD value register indicating which timer (0 or 1) is to be used and which timer mode is selected. Load registers TL and TH with initial count values. Start the timer by the instruction “SETB TR0” for timer 0 and “SETB TR1 for timer 1. Keep monitoring the timer flag (TF) with the “JNB TFx, target” instruction to see if it is raised. Stop the timer with the instructions “CLR TR0” or “CLR TR1”, for timer 0 and timer 1, respectively. Clear the TF flag for the next round with the instruction “CLR TF0” or “CLR TF1”, for timer 0 and timer 1, respectively. Go back to step 2 to load TH and TL again.
  • 26. Data Types and Time Delay in the 8051 using C
  • 27. Data Types and Time Delay in the 8051 using C Why Program the 8051 using C Language??? Compilers produce hex files that is downloaded to ROM of microcontroller The size of hex file is the main concern Microcontrollers have limited on-chip ROM Code space for 8051 is limited to 64K bytes C programming is less time consuming, but has larger hex file size  The reasons for writing programs in C  It is easier and less time consuming to write in C than Assembly  C is easier to modify and update  We can use code available in function libraries  C code is portable to other microcontroller with little modification
  • 28.  A good understanding of C data types for 8051 can help programmers to create smaller hex files  Unsigned char  Signed char  Unsigned int  Signed int  Sbit (single bit)  Bit and sfr Data Types and Time Delay in the 8051 using C
  • 29. Data Types and Time Delay in the 8051 using C
  • 30. Data Types and Time Delay in the 8051 using C
  • 31. Data Types and Time Delay in the 8051 using C
  • 32. Data Types and Time Delay in the 8051 using C
  • 33. Data Types and Time Delay in the 8051 using C
  • 34. Data Types and Time Delay in the 8051 using C
  • 35. Data Types and Time Delay in the 8051 using C
  • 36. Data Types and Time Delay in the 8051 using C
  • 37. Data Types and Time Delay in the 8051 using C
  • 38. Data Types and Time Delay in the 8051 using C