SlideShare a Scribd company logo
5
Most read
11
Most read
12
Most read
DR. VIKAS J. DONGRE
HOD ELECTRONICS & TELECOM
GOVERNMENT POLYTECHNIC WASHIM (MS)
EMAIL: DONGREVJ1@GMAIL.COM
 The 8051 has two timers/counters, they can be
used either as
 Timers to generate a time delay or as
 Event counters to count events happening outside the
microcontroller
 Both Timer 0 and Timer 1 are 16 bits wide
 Since 8051 has an 8-bit architecture, each 16-bits timer is
accessed as two separate registers of low byte and high
byte
Timer programming for 8051 using embedded c
 Accessed as low byte and high byte
 The low byte register is called TL0/TL1 and
 The high byte register is called TH0/TH1
 Accessed like any other register
 MOV TL0,#4FH
 MOV R5,TH0
TH0 TL0
D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
TH1 TL1
 Both timers 0 and 1 use the same register, called
TMOD (timer mode), to set the various timer operation modes
 TMOD is a 8-bit register
 The lower 4 bits are for Timer 0
 The upper 4 bits are for Timer 1
 In each case,
 The lower 2 bits are used to set the timer mode
 The upper 2 bits to specify the operation
(MSB) (LSB)
GATE C/T M1 M0
Timer1
GATE C/T M1 M0
Timer0
Timer programming for 8051 using embedded c
Timer programming for 8051 using embedded c
 The following are the characteristics and operations of mode1:
1. It is a 16-bit timer; therefore, it allows value of 0000 to FFFFH to be
loaded into the timer’s register TL and TH
2. After TH and TL are loaded with a 16-bit initial value, the timer must be
started
 This is done by SETB TR0 for timer 0 and SETB TR1 for timer 1
3. After the timer is started, it starts to count up
 It counts up until it reaches its limit of FFFFH
 When it rolls over from FFFFH to 0000, it sets high a flag bit called TF
(timer flag)
– Each timer has its own timer flag: TF0 for
timer 0, and TF1 for timer 1
– This timer flag can be monitored
 When this timer flag is raised, one option would be to stop the timer with
the instructions CLR TR0 or CLR TR1, for timer 0 and timer 1,
respectively
4. After the timer reaches its limit and rolls over, in order to
repeat the process
 TH and TL must be reloaded with the original value, and
 TF must be reloaded to 0
1. Load the TMOD value register indicating which timer
(timer 0 or timer 1) is to be used and which timer mode
(0 or 1) is selected
2. Load registers TL and TH with initial count value
3. Start the timer
4. Keep monitoring the timer flag (TF) with the JNB
TFx,target instruction to see if it is raised
 Get out of the loop when TF becomes high
5. Stop the timer
6. Clear the TF flag for the next round
7. Go back to Step 2 to load TH and TL again
 To generate a time delay
#include <reg51.h>
void T0Delay(void);
void main(void)
{
while(1)
{
P1=0x55;
T0Delay();
P1=0xAA;
T0Delay();
}
}
void T0Delay()
{
TMOD=0x01;
TL0=0x00;
TH0=0x35;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}
FFFFH – 3500H = CAFFH
= 51967 + 1 = 51968
If crystal frequency= 11.0592MHz,
Then T=1/f, i.e. T= 1.085s
51968  1.085 s = 56.384 ms is
the approximate delay
Write an 8051 C program to toggle all the bits of port P1 continuously with
some delay in between. Use Timer 0, 16-bit mode to generate the delay.
Write an 8051 C program to create a frequency of 2500 Hz
on pin P2.7. Use Timer 1, mode 2 to create delay.
#include <reg51.h>
void T1M2Delay(void);
sbit mybit=P2^7;
void main(void)
{
unsigned char x;
while(1)
{
mybit=~mybit;
T1M2Delay();
}
}
void T1M2Delay(void)
{
TMOD=0x20;
TH1=-184;
TR1=1;
while (TF1==0);
TR1=0;
TF1=0;
}
1/2500 Hz = 400 s
400 s /2 = 200 s
200 s / 1.085 s = 184
Assume that a 1-Hz external clock is being fed into pin T1 (P3.5).
Write a C program for counter 1 in mode 2 (8-bit auto reload) to count up and
display the state of the TL1 count on P1. Start the count at 0H.
#include <reg51.h>
sbit T01=P3^5;
void main(void)
{
T01=1;
TMOD=0x60; //01100000
TH1=0;
while (1)
{
do
{
TR1=1;
P1=TL1;
}
while(TF1==0);
TR1=0;
TF1=0;
}}
Timer programming for 8051 using embedded c

More Related Content

PPTX
8051 timer counter
PPTX
Interrupts in 8051
PPT
Interrupt programming with 8051 microcontroller
PDF
Seven segment interfacing with 8051.pdf
PPTX
8051 Microcontroller PPT's By Er. Swapnil Kaware
PPTX
Architecture of 8051
PDF
8259 Programmable Interrupt Controller
PDF
8051 Microcontroller I/O ports
8051 timer counter
Interrupts in 8051
Interrupt programming with 8051 microcontroller
Seven segment interfacing with 8051.pdf
8051 Microcontroller PPT's By Er. Swapnil Kaware
Architecture of 8051
8259 Programmable Interrupt Controller
8051 Microcontroller I/O ports

What's hot (20)

PPTX
Timer counter in arm7(lpc2148)
PPTX
8051 Assembly Language Programming
PPTX
Interfacing Stepper motor with 8051
PPT
8255 presentaion.ppt
PPT
pin-diagram-details-of-8086-microprocessor
PDF
Unit II arm 7 Instruction Set
PPT
Arithmetic & logical operations in 8051
PPTX
Microcontroller 8051 and its interfacing
PPT
Microcontroller-8051.ppt
PPTX
PIC Microcontrollers
PPT
Addressing modes of 8051
PPTX
Stack in 8085 microprocessor
PPTX
Interfacing external memory in 8051
PPT
8051 MICROCONTROLLER
PPT
Memory organization of 8051
PPTX
8051 timer counter
PDF
interfacing of temperature sensor LM 35 with 8051.pdf
PPTX
8051 Microcontroller ppt
PDF
8051 interfacing
PPT
8051 block diagram
Timer counter in arm7(lpc2148)
8051 Assembly Language Programming
Interfacing Stepper motor with 8051
8255 presentaion.ppt
pin-diagram-details-of-8086-microprocessor
Unit II arm 7 Instruction Set
Arithmetic & logical operations in 8051
Microcontroller 8051 and its interfacing
Microcontroller-8051.ppt
PIC Microcontrollers
Addressing modes of 8051
Stack in 8085 microprocessor
Interfacing external memory in 8051
8051 MICROCONTROLLER
Memory organization of 8051
8051 timer counter
interfacing of temperature sensor LM 35 with 8051.pdf
8051 Microcontroller ppt
8051 interfacing
8051 block diagram
Ad

Similar to Timer programming for 8051 using embedded c (20)

PDF
9 timer programming
PPT
8051 Timer
PPT
4.Timer_1.ppt
PPT
Programming 8051 Timers
PPT
Timers
PPTX
6-Interrupts Programming-27-03-2024.pptx
PPTX
5-Timer Mode 2 Programming-18-03-2024.pptx
PPTX
Micro c lab7(timers)
PPT
lecture 12 counter_microcontroller2.ppt
PPT
8051 ch9
PPTX
5th unit embedded system and iot design timer and controller
PPTX
Timer programming
PPT
UNIT-5.ppt
PPTX
8051 MICROCONTROLLER TIMER AND ITS APPLICATIONS
PPT
MICROCONTROLLER TIMERS.ppt
PDF
8051 timers--2
PPTX
8051 Timers and Counters
PPT
MICROCONTROLLER TIMERS.GHGHGHGHGHGHGHGHGppt
PPT
Microcontroller 8051 timer and counter module
9 timer programming
8051 Timer
4.Timer_1.ppt
Programming 8051 Timers
Timers
6-Interrupts Programming-27-03-2024.pptx
5-Timer Mode 2 Programming-18-03-2024.pptx
Micro c lab7(timers)
lecture 12 counter_microcontroller2.ppt
8051 ch9
5th unit embedded system and iot design timer and controller
Timer programming
UNIT-5.ppt
8051 MICROCONTROLLER TIMER AND ITS APPLICATIONS
MICROCONTROLLER TIMERS.ppt
8051 timers--2
8051 Timers and Counters
MICROCONTROLLER TIMERS.GHGHGHGHGHGHGHGHGppt
Microcontroller 8051 timer and counter module
Ad

More from Vikas Dongre (20)

PPTX
Lcd interfaing using 8051 and assambly language programming
PPTX
Job opportunities for electronics engineering
PPTX
Educational video creation: Tools and tips
PPTX
Scope of job education and business after HSC
PPTX
Introduction to digital logic gates
PPTX
Introduction to binary number system
PPTX
Arithmetic and Logic instructions in Embedded C
PPTX
Introduction to Embedded system programming using 8051
PPTX
Interrupts programming in embedded C using 8051
PPTX
Arithmetic and logic operations in c
PPTX
Arithmetic and logic operations in c
PPTX
Classification of embedded systems
PPTX
Characteristics of embedded systems
PPTX
Features of 89c51,pic,avr &amp; arm processors
PPTX
Microcontroller architecture
PPTX
2. block diagram and components of embedded system
PPTX
1. advantages and applications of embedded system
PPTX
Serial communication
PPTX
Innovative improvements in electronic engineering laboratory education using eml
PDF
Devnagari handwritten numeral recognition using geometric features and statis...
Lcd interfaing using 8051 and assambly language programming
Job opportunities for electronics engineering
Educational video creation: Tools and tips
Scope of job education and business after HSC
Introduction to digital logic gates
Introduction to binary number system
Arithmetic and Logic instructions in Embedded C
Introduction to Embedded system programming using 8051
Interrupts programming in embedded C using 8051
Arithmetic and logic operations in c
Arithmetic and logic operations in c
Classification of embedded systems
Characteristics of embedded systems
Features of 89c51,pic,avr &amp; arm processors
Microcontroller architecture
2. block diagram and components of embedded system
1. advantages and applications of embedded system
Serial communication
Innovative improvements in electronic engineering laboratory education using eml
Devnagari handwritten numeral recognition using geometric features and statis...

Recently uploaded (20)

PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PDF
Abrasive, erosive and cavitation wear.pdf
PPTX
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
PPTX
CyberSecurity Mobile and Wireless Devices
PPTX
communication and presentation skills 01
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PDF
Visual Aids for Exploratory Data Analysis.pdf
PPTX
Feature types and data preprocessing steps
PPT
Total quality management ppt for engineering students
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PPTX
Amdahl’s law is explained in the above power point presentations
PPTX
Module 8- Technological and Communication Skills.pptx
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PPTX
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
PPTX
ASME PCC-02 TRAINING -DESKTOP-NLE5HNP.pptx
PPTX
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
Improvement effect of pyrolyzed agro-food biochar on the properties of.pdf
PPTX
tack Data Structure with Array and Linked List Implementation, Push and Pop O...
Exploratory_Data_Analysis_Fundamentals.pdf
Abrasive, erosive and cavitation wear.pdf
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
CyberSecurity Mobile and Wireless Devices
communication and presentation skills 01
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Visual Aids for Exploratory Data Analysis.pdf
Feature types and data preprocessing steps
Total quality management ppt for engineering students
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Amdahl’s law is explained in the above power point presentations
Module 8- Technological and Communication Skills.pptx
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
ASME PCC-02 TRAINING -DESKTOP-NLE5HNP.pptx
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Improvement effect of pyrolyzed agro-food biochar on the properties of.pdf
tack Data Structure with Array and Linked List Implementation, Push and Pop O...

Timer programming for 8051 using embedded c

  • 1. DR. VIKAS J. DONGRE HOD ELECTRONICS & TELECOM GOVERNMENT POLYTECHNIC WASHIM (MS) EMAIL: [email protected]
  • 2.  The 8051 has two timers/counters, they can be used either as  Timers to generate a time delay or as  Event counters to count events happening outside the microcontroller  Both Timer 0 and Timer 1 are 16 bits wide  Since 8051 has an 8-bit architecture, each 16-bits timer is accessed as two separate registers of low byte and high byte
  • 4.  Accessed as low byte and high byte  The low byte register is called TL0/TL1 and  The high byte register is called TH0/TH1  Accessed like any other register  MOV TL0,#4FH  MOV R5,TH0 TH0 TL0 D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 TH1 TL1
  • 5.  Both timers 0 and 1 use the same register, called TMOD (timer mode), to set the various timer operation modes  TMOD is a 8-bit register  The lower 4 bits are for Timer 0  The upper 4 bits are for Timer 1  In each case,  The lower 2 bits are used to set the timer mode  The upper 2 bits to specify the operation (MSB) (LSB) GATE C/T M1 M0 Timer1 GATE C/T M1 M0 Timer0
  • 8.  The following are the characteristics and operations of mode1: 1. It is a 16-bit timer; therefore, it allows value of 0000 to FFFFH to be loaded into the timer’s register TL and TH 2. After TH and TL are loaded with a 16-bit initial value, the timer must be started  This is done by SETB TR0 for timer 0 and SETB TR1 for timer 1 3. After the timer is started, it starts to count up  It counts up until it reaches its limit of FFFFH
  • 9.  When it rolls over from FFFFH to 0000, it sets high a flag bit called TF (timer flag) – Each timer has its own timer flag: TF0 for timer 0, and TF1 for timer 1 – This timer flag can be monitored  When this timer flag is raised, one option would be to stop the timer with the instructions CLR TR0 or CLR TR1, for timer 0 and timer 1, respectively 4. After the timer reaches its limit and rolls over, in order to repeat the process  TH and TL must be reloaded with the original value, and  TF must be reloaded to 0
  • 10. 1. Load the TMOD value register indicating which timer (timer 0 or timer 1) is to be used and which timer mode (0 or 1) is selected 2. Load registers TL and TH with initial count value 3. Start the timer 4. Keep monitoring the timer flag (TF) with the JNB TFx,target instruction to see if it is raised  Get out of the loop when TF becomes high 5. Stop the timer 6. Clear the TF flag for the next round 7. Go back to Step 2 to load TH and TL again  To generate a time delay
  • 11. #include <reg51.h> void T0Delay(void); void main(void) { while(1) { P1=0x55; T0Delay(); P1=0xAA; T0Delay(); } } void T0Delay() { TMOD=0x01; TL0=0x00; TH0=0x35; TR0=1; while (TF0==0); TR0=0; TF0=0; } FFFFH – 3500H = CAFFH = 51967 + 1 = 51968 If crystal frequency= 11.0592MHz, Then T=1/f, i.e. T= 1.085s 51968  1.085 s = 56.384 ms is the approximate delay Write an 8051 C program to toggle all the bits of port P1 continuously with some delay in between. Use Timer 0, 16-bit mode to generate the delay.
  • 12. Write an 8051 C program to create a frequency of 2500 Hz on pin P2.7. Use Timer 1, mode 2 to create delay. #include <reg51.h> void T1M2Delay(void); sbit mybit=P2^7; void main(void) { unsigned char x; while(1) { mybit=~mybit; T1M2Delay(); } } void T1M2Delay(void) { TMOD=0x20; TH1=-184; TR1=1; while (TF1==0); TR1=0; TF1=0; } 1/2500 Hz = 400 s 400 s /2 = 200 s 200 s / 1.085 s = 184
  • 13. Assume that a 1-Hz external clock is being fed into pin T1 (P3.5). Write a C program for counter 1 in mode 2 (8-bit auto reload) to count up and display the state of the TL1 count on P1. Start the count at 0H. #include <reg51.h> sbit T01=P3^5; void main(void) { T01=1; TMOD=0x60; //01100000 TH1=0; while (1) { do { TR1=1; P1=TL1; } while(TF1==0); TR1=0; TF1=0; }}