SlideShare a Scribd company logo
1
Unit 5: 8051 Timers/Counters Programming
Part 2
Subject: Processor Architecture & Interfacing
(SPPU, Pune 2015 Course of Information Technology)
Class: SEIT Semester: II
Prepared By,
Ms. K. D. Patil, Assistant Professor
Department of Information Technology
(NBA accredited)
Sanjivani College of Engineering, Kopargaon-423603.
Maharashtra, India.
(An Autonomous Institute, Affiliated with SPPU, Pune.)
NAAC ‘A’ Grade Accredited, ISO 9001:2015 certified
Contents
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon2
 Part 1
 Introduction
 Special Function Registers
 Timer 0 Register
 Timer 1 Register
 TMOD Register
 TCON Register
 How to UseTimer
 Operating Modes ofTimer
 Part 2
 Timer Programming
 Mode 1 Programming
 Mode 2 Programming
 Introduction to Counters
 Counter Programming
Steps in Mode 1 Programming
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon3
1. Load theTMOD 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 registersTL andTH with initial count value
3. Start the timer (SETBTRx)
4. Keep monitoring the timer flag (TF) with JNBTFx, target
instruction to see if it is raised. Get out of the loop whenTF
becomes high
5. Stop the timer (CLRTRx)
6. Clear theTF flag for the next round (CLRTFx)
7. Go back to Step 2 to loadTH andTL again
Steps in Mode 2 Programming
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon4
1. Load theTMOD value register indicating which timer
(timer 0 or timer 1) is to be used and which timer mode
(2) is selected
2. Load registers TH with initial count value
3. Start the timer (SETBTRx)
4. Keep monitoring the timer flag (TF) with JNBTFx, target
instruction to see if it is raised. Get out of the loop when
TF becomes high
5. Clear theTF flag for the next round (CLRTFx)
6. Go back to Step 4, since mode 2 is auto reloaded
Clock Source for Timer
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon5
 Timer needs a clock pulse to ticks
 if C/T = 0, the crystal frequency attached to the 8051 is the source
of the clock for the timer
 Size of Crystal frequency also decides the speed at which 8051
timer ticks
 8051 based systems have XTAL frequency of 10MHz to 40MHz
 XTAL = 11.0592 MHz allows 8051 system to communicate with
IBM PC with no errors
 The frequency for the timer is always 1/12th the frequency of the
crystal attached to the 8051
Clock Source for Timer
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon6
 When , XTAL =11.0592MHz
 1 Machine cycle of 8051 consists of 12 OSC periods (see fig.)
 Timer Frequency
= (11.0592/12) = 921.6 KHz
 Time period of each machine cycle
= 1/921.6 KHz= 1.085 us
 1 instruction takes 1.085 us for execution
Finding Values to be loaded in TH-TL
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon7
 If XTAL = 11.0592 MHz
 Timer Frequency = (11.0592/12) = 921.6 KHz
 We know that each clock hasTime period of each machine
cycle = 1/921.6 KHz= 1.085 us
1. To get the number of counts, Divide the desired time delay
by 1.085 us, we will get n
2. Perform 65536 – n, where n is the decimal value we got in
step 1
3. Convert the result of Step2 to hex, whereYYXX is the
initial hex value to be loaded into the timer’s register
4. SetTL = XX andTH =YY
Calculate the amount of Time Delay
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon8
 If XTAL = 11.0592 MHz
 Timer Frequency = (11.0592/12) = 921.6 KHz
 We know that each clock hasTime period of each machine cycle
= 1/921.6 KHz= 1.085 uSec
 Means,Timer 0 counts up each 1.085 us resulting in
delay = number of counts × 1.085us.
 The number of counts for the roll over is
FFFF H –YYXX H = Result
 However, we add one to Result because of the extra clock needed
when it rolls over from FFFF to 0 and raise theTF flag
 This gives (Result + 1) × 1.085us = ___us for half the pulse
 For the entire period it isT = 2 × ___us = ___us as the time delay
generated by the timer
Calculate the amount of Time Delay
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon9
 In Hex
(FFFF –YYXX + 1) × 1.085 us,
whereYYXX areTH,TL initial
values respectively.
 Notice that value YYXX are in
hex.
will give you delay for half
pulse
 In Decimal
 ConvertYYXX values of
theTH,TL register to
decimal to get a NNNN
decimal, then
(65536 - NNNN) × 1.085 us
will give you delay for half
pulse
Example No.1 (Mode 1 Programming)
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon10
Assume that XTAL = 11.0592 MHz.What value do we need to
load the timer’s register if we want to have a time delay of 5 ms
(milliseconds)? Show the program for timer 0 to create a pulse
width of 5 ms on P2.3.
Solution:
XTAL = 11.0592 MHz
Timer Frequency = (11.0592/12) = 921.6 KHz
Each clock hasTime period = 1/921.6 KHz= 1.085 uSec
No. of clocks = 5000 us /1.085us = 4608
To achieve that we need to load the values inTH-TL
= 65536 – 4608 = 60928 in decimal
60928 D = EE00 H so, TH = EEH and TL = 00H
Example No.1 (Mode 1 Programming)
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon11
Program:
ORG 00H
CLR P2.3 ;Clear P2.3
MOV TMOD,#01 ;Timer 0, 16-bitmode
HERE: MOV TL0,#00H ;TL0=00, the low byte
MOVTH0,#0EEH ;TH0=EE, the high byte
SETB P2.3 ;SET high P2.3
SETBTR0 ;Start timer 0
AGAIN: JNBTF0,AGAIN ;Monitor timer flag 0
CLRTR0 ;Stop the timer 0
CLRTF0 ;Clear timer 0 flag
RET
END
Example No.2 (Mode 1 Programming)
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon12
 Assume that XTAL = 11.0592 MHz, write a program to
generate a square wave of 2 kHz frequency on pin P1.5.
Solution:
 T = 1 / f = 1 / 2 kHz = 500 us the period of square wave.
 1 / 2 of it for the high and low portion of the pulse is 250
us.
 250 us / 1.085 us = 230 and 65536 – 230 = 65306 which
in hex is FF1AH.
 TL = 1A andTH = FF, all in hex.
Example No.2 (Mode 1 Programming)
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon13
Program:
ORG 00H
MOV P1, # 0000 0000 B
MOV TMOD,#0000 0001 B ;Timer 0, 16-bitmode
MAIN:
SETB P1.5 ; make P1.5 as high
ACALL DELAY
CLR P1.5 ; make P1.5 as low
ACALL DELAY
SJMP MAIN ;Reload timer
DELAY: MOV TL1,#1AH ;TL1=1A,low byte of timer
MOV TH1,#0FFH ;TH1=FF, the high byte
SETB TR0 ;Start timer 0
BACK: JNB TF0, BACK ;until timer rolls over
CLR TR0 ;Stop the timer 0
CLR TF0 ;Clear timer 0 flag
SETB P1.5
RET
END
Example No.3 (Mode 2 Programming)
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon14
 Assume XTAL = 11.0592 MHz, find the
frequency of the square wave generated on
pin P1.0 in the following program
MOVTMOD,#20H ;T1/8-bit/auto reload
MOVTH1,#5 ;TH1 = 5
SETBTR1 ;start the timer 1
BACK: JNB TF1,BACK ;till timer rolls over
CPL P1.0 ;P1.0 to high, low
CLRTF1 ;clearTimer 1 flag
SJMP BACK ;mode 2 is auto-reload
Solution:
 In mode 2 we do not need to
reloadTH since it is auto-reload.
 Delay
= (256 - 05) × 1.085 us
= 251 × 1.085 us = 272.33 us is the
high portion of the pulse. Since
 it is a 50% duty cycle square wave,
the periodT is twice that
 T = 2 × 272.33 us = 544.67 us
 frequency = 1.83597 kHz
Example No.4 (Mode 2 Programming)
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon15
 Assume that XTAL = 22 MHz, write a program to generate a
square wave of 1 kHz frequency on pin P1.2.
Solution:
 XTAL = 22MHz
 Timer Frequency = 22/12 = 1.83 us
 Clock Period = 1 / 1.83us = 0.546 us
 T = 1 / f = 1 / 1 kHz = 1000 us the period of square wave.
 1 / 2 of it for the high and low portion of the pulse is 500 us.
 No. of Cycles in 5 ms = 500 us / 0.546 us = 915
 No. of Cycles in 1 ms = 915/5 = 183 cycles
 Value loaded toTH = FF H – B7H = 48 H
Example No.4 (Mode 2 Programming)
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon16
Program:
ORG 00H
MOV TMOD, #02 H ;Timer 0, mode 2
MAIN: CPL P1.2 ; complement P1.2
MOV R6, #05 ; ; count for multiple delay
DELAY: MOV TH0, #48H ;TH0=48 H
SETB TR0 ;Start timer 0
BACK: JNB TF0, BACK ;until timer rolls over
CLR TR0 ;Stop the timer 0
CLR TF0 ;Clear timer 0 flag
DJNZ R0, DELAY ; repeat until R0= 0
SJMP MAIN ; repeat to get train of pulses
END
Counter Programming
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon17
 Timers can also be used as counters counting events
happening outside the 8051
 When it is used as a counter, it is a pulse outside of the 8051
that increments theTH,TL registers
 TMOD andTH,TL registers are the same as for the timer
discussed previously
 Programming the timer in the last section also applies to
programming it as a counter Except the source of the
frequency
Counter Programming
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon18
 The C/T bit in theTMOD registers decides the source of
the clock for the timer
 When C/T = 1, the timer is used as a counter and gets its pulses
from outside the 8051
 The counter counts up as pulses are fed from pins 14 and 15,
these pins are calledT0 (timer 0 input) andT1 (timer 1 input)
Counter Programming
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon19
 GATE Bit inTMOD
 If GATE = 1, the start and stop of the timer are done externally
through pins P3.2 and P3.3 for timers 0 and 1, respectively
 This hardware way allows to start or stop the timer externally at
any time via a simple switch
Example No. 5 (Counter Programming)
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon20
 Assuming that clock pulses are fed into pinT1, write a program for counter 1 in
mode 2 to count the pulses and display the state of theTL1 count on P2, which
connects to 8 LEDs.
Solution:
MOVTM0D,#01100000B ;counter 1, mode 2, ;C/T=1 external pulses
MOVTH1,#0 ;clearTH1
SETB P3.5 ;makeT1 input
AGAIN: SETBTR1 ;start the counter
BACK: MOV A,TL1 ;get copy ofTL
MOV P2,A ;display it on port 2
JNBTF1,Back ;keep doing, ifTF = 0
CLRTR1 ;stop the counter 1
CLRTF1 ;makeTF=0
SJMPAGAIN ;keep doing it
Reference
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon21
 “The 8051 Microcontroller and Embedded Systems using
Assembly and C” , Muhammad Ali Mazidi, Janice Gillispie
Mazidi, Rolin D. McKinlay, Second Edition, Pearson
publication
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon22

More Related Content

What's hot

Introduction to Built In Self Test (BIST).pdf
Introduction to Built In Self Test (BIST).pdfIntroduction to Built In Self Test (BIST).pdf
Introduction to Built In Self Test (BIST).pdf
FEMILAECE
 
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
Vikas Dongre
 
8051 Timer
8051 Timer8051 Timer
8051 Timer
Ramasubbu .P
 
Chapter 6 hardware structure of 8086
Chapter 6  hardware structure of 8086Chapter 6  hardware structure of 8086
Chapter 6 hardware structure of 8086
SHREEHARI WADAWADAGI
 
AMBA BUS.pptx
AMBA BUS.pptxAMBA BUS.pptx
AMBA BUS.pptx
SHABANASALIM4
 
8051 assembly programming
8051 assembly programming8051 assembly programming
8051 assembly programming
sergeiseq
 
Question paper with solution the 8051 microcontroller based embedded systems...
Question paper with solution  the 8051 microcontroller based embedded systems...Question paper with solution  the 8051 microcontroller based embedded systems...
Question paper with solution the 8051 microcontroller based embedded systems...
manishpatel_79
 
8251 USART
8251 USART8251 USART
8251 USART
coolsdhanesh
 
Contamination delay
Contamination delayContamination delay
Contamination delayNima Afraz
 
LECT 1: ARM PROCESSORS
LECT 1: ARM PROCESSORSLECT 1: ARM PROCESSORS
LECT 1: ARM PROCESSORS
Dr.YNM
 
ARM architcture
ARM architcture ARM architcture
ARM architcture
Hossam Adel
 
Timers and counters of microcontroller 8051
Timers and counters of microcontroller 8051Timers and counters of microcontroller 8051
Timers and counters of microcontroller 8051
Nilesh Bhaskarrao Bahadure
 
8086 Interrupts & With DOS and BIOS by vijay
8086 Interrupts &  With DOS and BIOS  by vijay8086 Interrupts &  With DOS and BIOS  by vijay
8086 Interrupts & With DOS and BIOS by vijay
Vijay Kumar
 
TMS320C5x
TMS320C5xTMS320C5x
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)
Aarav Soni
 
Microcontroller
MicrocontrollerMicrocontroller
MicrocontrollerSpitiq
 
8086 – CPU –Pin Diagram.pptx
8086 – CPU –Pin Diagram.pptx8086 – CPU –Pin Diagram.pptx
8086 – CPU –Pin Diagram.pptx
5G8Rajendra
 

What's hot (20)

Introduction to Built In Self Test (BIST).pdf
Introduction to Built In Self Test (BIST).pdfIntroduction to Built In Self Test (BIST).pdf
Introduction to Built In Self Test (BIST).pdf
 
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
 
8051 Timer
8051 Timer8051 Timer
8051 Timer
 
Chapter 6 hardware structure of 8086
Chapter 6  hardware structure of 8086Chapter 6  hardware structure of 8086
Chapter 6 hardware structure of 8086
 
AMBA BUS.pptx
AMBA BUS.pptxAMBA BUS.pptx
AMBA BUS.pptx
 
8051 assembly programming
8051 assembly programming8051 assembly programming
8051 assembly programming
 
8051 ch9
8051 ch98051 ch9
8051 ch9
 
Question paper with solution the 8051 microcontroller based embedded systems...
Question paper with solution  the 8051 microcontroller based embedded systems...Question paper with solution  the 8051 microcontroller based embedded systems...
Question paper with solution the 8051 microcontroller based embedded systems...
 
8251 USART
8251 USART8251 USART
8251 USART
 
Contamination delay
Contamination delayContamination delay
Contamination delay
 
LECT 1: ARM PROCESSORS
LECT 1: ARM PROCESSORSLECT 1: ARM PROCESSORS
LECT 1: ARM PROCESSORS
 
ARM architcture
ARM architcture ARM architcture
ARM architcture
 
Timers and counters of microcontroller 8051
Timers and counters of microcontroller 8051Timers and counters of microcontroller 8051
Timers and counters of microcontroller 8051
 
UART
UARTUART
UART
 
8086 Interrupts & With DOS and BIOS by vijay
8086 Interrupts &  With DOS and BIOS  by vijay8086 Interrupts &  With DOS and BIOS  by vijay
8086 Interrupts & With DOS and BIOS by vijay
 
Avr timers
Avr timersAvr timers
Avr timers
 
TMS320C5x
TMS320C5xTMS320C5x
TMS320C5x
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)
 
Microcontroller
MicrocontrollerMicrocontroller
Microcontroller
 
8086 – CPU –Pin Diagram.pptx
8086 – CPU –Pin Diagram.pptx8086 – CPU –Pin Diagram.pptx
8086 – CPU –Pin Diagram.pptx
 

Similar to SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2

9 timer programming
9 timer programming9 timer programming
9 timer programming
Channabasappa Kudarihal
 
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 1
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 1SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 1
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 1
KanchanPatil34
 
MICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.pptMICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.ppt
reemasajin1
 
Microcontrollers-MODULE4.pptx
Microcontrollers-MODULE4.pptxMicrocontrollers-MODULE4.pptx
Microcontrollers-MODULE4.pptx
AmoghR3
 
4.Timer_1.ppt
4.Timer_1.ppt4.Timer_1.ppt
4.Timer_1.ppt
YashwanthChandra7
 
Micro c lab7(timers)
Micro c lab7(timers)Micro c lab7(timers)
Micro c lab7(timers)
Mashood
 
Chapter 08
Chapter 08Chapter 08
Chapter 08
Khalid Hasan
 
Timer programming
Timer programming Timer programming
Timer programming vijaydeepakg
 
8051 timers--2
   8051 timers--2   8051 timers--2
8051 timers--2
Syed Basharat Hussain
 
timer counter (1).pptx
timer counter (1).pptxtimer counter (1).pptx
timer counter (1).pptx
SujalKumar73
 
8051 ch9-950217
8051 ch9-9502178051 ch9-950217
8051 ch9-950217
Gopal Krishna Murthy C R
 
L15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 pL15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 p
rsamurti
 
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
Rahultater4
 
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
Mohamed Ali
 
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
Waqas Afzal
 
EC8691 - UNIT 5.pdf
EC8691 - UNIT 5.pdfEC8691 - UNIT 5.pdf
EC8691 - UNIT 5.pdf
SPonmalar1
 
Delay routine
Delay routineDelay routine
Delay routine
Yash Gandhi
 

Similar to SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2 (20)

9 timer programming
9 timer programming9 timer programming
9 timer programming
 
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 1
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 1SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 1
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 1
 
MICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.pptMICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.ppt
 
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
 
Micro c lab7(timers)
Micro c lab7(timers)Micro c lab7(timers)
Micro c lab7(timers)
 
Chapter 08
Chapter 08Chapter 08
Chapter 08
 
Timer programming
Timer programming Timer programming
Timer programming
 
8051 timers--2
   8051 timers--2   8051 timers--2
8051 timers--2
 
timer counter (1).pptx
timer counter (1).pptxtimer counter (1).pptx
timer counter (1).pptx
 
8051 ch9-950217
8051 ch9-9502178051 ch9-950217
8051 ch9-950217
 
A02100104
A02100104A02100104
A02100104
 
L15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 pL15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 p
 
UNIT-5.ppt
UNIT-5.pptUNIT-5.ppt
UNIT-5.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
 
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
 
12 mt06ped007
12 mt06ped007 12 mt06ped007
12 mt06ped007
 
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
 
EC8691 - UNIT 5.pdf
EC8691 - UNIT 5.pdfEC8691 - UNIT 5.pdf
EC8691 - UNIT 5.pdf
 
Delay routine
Delay routineDelay routine
Delay routine
 

More from KanchanPatil34

Unit 2_2 Binary Tree as ADT_General Tree.pdf
Unit 2_2 Binary Tree as ADT_General Tree.pdfUnit 2_2 Binary Tree as ADT_General Tree.pdf
Unit 2_2 Binary Tree as ADT_General Tree.pdf
KanchanPatil34
 
Unit 2_1 Tree.pdf
Unit 2_1 Tree.pdfUnit 2_1 Tree.pdf
Unit 2_1 Tree.pdf
KanchanPatil34
 
Unit 2_3 Binary Tree Traversals.pdf
Unit 2_3 Binary Tree Traversals.pdfUnit 2_3 Binary Tree Traversals.pdf
Unit 2_3 Binary Tree Traversals.pdf
KanchanPatil34
 
Unit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdfUnit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdf
KanchanPatil34
 
Unit 1_Stack and Queue using Linked Organization.pdf
Unit 1_Stack and Queue using Linked Organization.pdfUnit 1_Stack and Queue using Linked Organization.pdf
Unit 1_Stack and Queue using Linked Organization.pdf
KanchanPatil34
 
PAI Unit 3 Paging in 80386 Microporcessor
PAI Unit 3 Paging in 80386 MicroporcessorPAI Unit 3 Paging in 80386 Microporcessor
PAI Unit 3 Paging in 80386 Microporcessor
KanchanPatil34
 
PAI Unit 3 Multitasking in 80386
PAI Unit 3 Multitasking in 80386PAI Unit 3 Multitasking in 80386
PAI Unit 3 Multitasking in 80386
KanchanPatil34
 
PAI Unit 2 Segmentation in 80386 microprocessor
PAI Unit 2 Segmentation in 80386 microprocessorPAI Unit 2 Segmentation in 80386 microprocessor
PAI Unit 2 Segmentation in 80386 microprocessor
KanchanPatil34
 
PAI Unit 2 Protection in 80386 segmentation
PAI Unit 2 Protection in 80386 segmentationPAI Unit 2 Protection in 80386 segmentation
PAI Unit 2 Protection in 80386 segmentation
KanchanPatil34
 
SE PAI Unit 2_Data Structures in 80386 segmentation
SE PAI Unit 2_Data Structures in 80386 segmentationSE PAI Unit 2_Data Structures in 80386 segmentation
SE PAI Unit 2_Data Structures in 80386 segmentation
KanchanPatil34
 
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 2SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 2
KanchanPatil34
 
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 1
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 1SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 1
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 1
KanchanPatil34
 
SE PAI Unit 5_IO programming in 8051
SE PAI Unit 5_IO programming in 8051SE PAI Unit 5_IO programming in 8051
SE PAI Unit 5_IO programming in 8051
KanchanPatil34
 
Unit 5_Interrupt programming in 8051 micro controller - part 2
Unit 5_Interrupt programming in 8051 micro controller - part 2Unit 5_Interrupt programming in 8051 micro controller - part 2
Unit 5_Interrupt programming in 8051 micro controller - part 2
KanchanPatil34
 
Unit 5_interrupt programming_Part 1
Unit 5_interrupt programming_Part 1Unit 5_interrupt programming_Part 1
Unit 5_interrupt programming_Part 1
KanchanPatil34
 
8051 interfacing
8051 interfacing8051 interfacing
8051 interfacing
KanchanPatil34
 
Unit 3 se pai_ivt and idt
Unit 3 se pai_ivt and idtUnit 3 se pai_ivt and idt
Unit 3 se pai_ivt and idt
KanchanPatil34
 
Unit 2 se pai_registers in 80386
Unit 2 se pai_registers in 80386Unit 2 se pai_registers in 80386
Unit 2 se pai_registers in 80386
KanchanPatil34
 
Unit i se pai_dos function calls
Unit i se pai_dos function callsUnit i se pai_dos function calls
Unit i se pai_dos function calls
KanchanPatil34
 
DELD Unit V cpld_fpga
DELD Unit V cpld_fpgaDELD Unit V cpld_fpga
DELD Unit V cpld_fpga
KanchanPatil34
 

More from KanchanPatil34 (20)

Unit 2_2 Binary Tree as ADT_General Tree.pdf
Unit 2_2 Binary Tree as ADT_General Tree.pdfUnit 2_2 Binary Tree as ADT_General Tree.pdf
Unit 2_2 Binary Tree as ADT_General Tree.pdf
 
Unit 2_1 Tree.pdf
Unit 2_1 Tree.pdfUnit 2_1 Tree.pdf
Unit 2_1 Tree.pdf
 
Unit 2_3 Binary Tree Traversals.pdf
Unit 2_3 Binary Tree Traversals.pdfUnit 2_3 Binary Tree Traversals.pdf
Unit 2_3 Binary Tree Traversals.pdf
 
Unit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdfUnit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdf
 
Unit 1_Stack and Queue using Linked Organization.pdf
Unit 1_Stack and Queue using Linked Organization.pdfUnit 1_Stack and Queue using Linked Organization.pdf
Unit 1_Stack and Queue using Linked Organization.pdf
 
PAI Unit 3 Paging in 80386 Microporcessor
PAI Unit 3 Paging in 80386 MicroporcessorPAI Unit 3 Paging in 80386 Microporcessor
PAI Unit 3 Paging in 80386 Microporcessor
 
PAI Unit 3 Multitasking in 80386
PAI Unit 3 Multitasking in 80386PAI Unit 3 Multitasking in 80386
PAI Unit 3 Multitasking in 80386
 
PAI Unit 2 Segmentation in 80386 microprocessor
PAI Unit 2 Segmentation in 80386 microprocessorPAI Unit 2 Segmentation in 80386 microprocessor
PAI Unit 2 Segmentation in 80386 microprocessor
 
PAI Unit 2 Protection in 80386 segmentation
PAI Unit 2 Protection in 80386 segmentationPAI Unit 2 Protection in 80386 segmentation
PAI Unit 2 Protection in 80386 segmentation
 
SE PAI Unit 2_Data Structures in 80386 segmentation
SE PAI Unit 2_Data Structures in 80386 segmentationSE PAI Unit 2_Data Structures in 80386 segmentation
SE PAI Unit 2_Data Structures in 80386 segmentation
 
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 2SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 2
 
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 1
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 1SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 1
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 1
 
SE PAI Unit 5_IO programming in 8051
SE PAI Unit 5_IO programming in 8051SE PAI Unit 5_IO programming in 8051
SE PAI Unit 5_IO programming in 8051
 
Unit 5_Interrupt programming in 8051 micro controller - part 2
Unit 5_Interrupt programming in 8051 micro controller - part 2Unit 5_Interrupt programming in 8051 micro controller - part 2
Unit 5_Interrupt programming in 8051 micro controller - part 2
 
Unit 5_interrupt programming_Part 1
Unit 5_interrupt programming_Part 1Unit 5_interrupt programming_Part 1
Unit 5_interrupt programming_Part 1
 
8051 interfacing
8051 interfacing8051 interfacing
8051 interfacing
 
Unit 3 se pai_ivt and idt
Unit 3 se pai_ivt and idtUnit 3 se pai_ivt and idt
Unit 3 se pai_ivt and idt
 
Unit 2 se pai_registers in 80386
Unit 2 se pai_registers in 80386Unit 2 se pai_registers in 80386
Unit 2 se pai_registers in 80386
 
Unit i se pai_dos function calls
Unit i se pai_dos function callsUnit i se pai_dos function calls
Unit i se pai_dos function calls
 
DELD Unit V cpld_fpga
DELD Unit V cpld_fpgaDELD Unit V cpld_fpga
DELD Unit V cpld_fpga
 

Recently uploaded

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 

Recently uploaded (20)

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 

SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2

  • 1. 1 Unit 5: 8051 Timers/Counters Programming Part 2 Subject: Processor Architecture & Interfacing (SPPU, Pune 2015 Course of Information Technology) Class: SEIT Semester: II Prepared By, Ms. K. D. Patil, Assistant Professor Department of Information Technology (NBA accredited) Sanjivani College of Engineering, Kopargaon-423603. Maharashtra, India. (An Autonomous Institute, Affiliated with SPPU, Pune.) NAAC ‘A’ Grade Accredited, ISO 9001:2015 certified
  • 2. Contents Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon2  Part 1  Introduction  Special Function Registers  Timer 0 Register  Timer 1 Register  TMOD Register  TCON Register  How to UseTimer  Operating Modes ofTimer  Part 2  Timer Programming  Mode 1 Programming  Mode 2 Programming  Introduction to Counters  Counter Programming
  • 3. Steps in Mode 1 Programming Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon3 1. Load theTMOD 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 registersTL andTH with initial count value 3. Start the timer (SETBTRx) 4. Keep monitoring the timer flag (TF) with JNBTFx, target instruction to see if it is raised. Get out of the loop whenTF becomes high 5. Stop the timer (CLRTRx) 6. Clear theTF flag for the next round (CLRTFx) 7. Go back to Step 2 to loadTH andTL again
  • 4. Steps in Mode 2 Programming Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon4 1. Load theTMOD value register indicating which timer (timer 0 or timer 1) is to be used and which timer mode (2) is selected 2. Load registers TH with initial count value 3. Start the timer (SETBTRx) 4. Keep monitoring the timer flag (TF) with JNBTFx, target instruction to see if it is raised. Get out of the loop when TF becomes high 5. Clear theTF flag for the next round (CLRTFx) 6. Go back to Step 4, since mode 2 is auto reloaded
  • 5. Clock Source for Timer Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon5  Timer needs a clock pulse to ticks  if C/T = 0, the crystal frequency attached to the 8051 is the source of the clock for the timer  Size of Crystal frequency also decides the speed at which 8051 timer ticks  8051 based systems have XTAL frequency of 10MHz to 40MHz  XTAL = 11.0592 MHz allows 8051 system to communicate with IBM PC with no errors  The frequency for the timer is always 1/12th the frequency of the crystal attached to the 8051
  • 6. Clock Source for Timer Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon6  When , XTAL =11.0592MHz  1 Machine cycle of 8051 consists of 12 OSC periods (see fig.)  Timer Frequency = (11.0592/12) = 921.6 KHz  Time period of each machine cycle = 1/921.6 KHz= 1.085 us  1 instruction takes 1.085 us for execution
  • 7. Finding Values to be loaded in TH-TL Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon7  If XTAL = 11.0592 MHz  Timer Frequency = (11.0592/12) = 921.6 KHz  We know that each clock hasTime period of each machine cycle = 1/921.6 KHz= 1.085 us 1. To get the number of counts, Divide the desired time delay by 1.085 us, we will get n 2. Perform 65536 – n, where n is the decimal value we got in step 1 3. Convert the result of Step2 to hex, whereYYXX is the initial hex value to be loaded into the timer’s register 4. SetTL = XX andTH =YY
  • 8. Calculate the amount of Time Delay Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon8  If XTAL = 11.0592 MHz  Timer Frequency = (11.0592/12) = 921.6 KHz  We know that each clock hasTime period of each machine cycle = 1/921.6 KHz= 1.085 uSec  Means,Timer 0 counts up each 1.085 us resulting in delay = number of counts × 1.085us.  The number of counts for the roll over is FFFF H –YYXX H = Result  However, we add one to Result because of the extra clock needed when it rolls over from FFFF to 0 and raise theTF flag  This gives (Result + 1) × 1.085us = ___us for half the pulse  For the entire period it isT = 2 × ___us = ___us as the time delay generated by the timer
  • 9. Calculate the amount of Time Delay Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon9  In Hex (FFFF –YYXX + 1) × 1.085 us, whereYYXX areTH,TL initial values respectively.  Notice that value YYXX are in hex. will give you delay for half pulse  In Decimal  ConvertYYXX values of theTH,TL register to decimal to get a NNNN decimal, then (65536 - NNNN) × 1.085 us will give you delay for half pulse
  • 10. Example No.1 (Mode 1 Programming) Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon10 Assume that XTAL = 11.0592 MHz.What value do we need to load the timer’s register if we want to have a time delay of 5 ms (milliseconds)? Show the program for timer 0 to create a pulse width of 5 ms on P2.3. Solution: XTAL = 11.0592 MHz Timer Frequency = (11.0592/12) = 921.6 KHz Each clock hasTime period = 1/921.6 KHz= 1.085 uSec No. of clocks = 5000 us /1.085us = 4608 To achieve that we need to load the values inTH-TL = 65536 – 4608 = 60928 in decimal 60928 D = EE00 H so, TH = EEH and TL = 00H
  • 11. Example No.1 (Mode 1 Programming) Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon11 Program: ORG 00H CLR P2.3 ;Clear P2.3 MOV TMOD,#01 ;Timer 0, 16-bitmode HERE: MOV TL0,#00H ;TL0=00, the low byte MOVTH0,#0EEH ;TH0=EE, the high byte SETB P2.3 ;SET high P2.3 SETBTR0 ;Start timer 0 AGAIN: JNBTF0,AGAIN ;Monitor timer flag 0 CLRTR0 ;Stop the timer 0 CLRTF0 ;Clear timer 0 flag RET END
  • 12. Example No.2 (Mode 1 Programming) Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon12  Assume that XTAL = 11.0592 MHz, write a program to generate a square wave of 2 kHz frequency on pin P1.5. Solution:  T = 1 / f = 1 / 2 kHz = 500 us the period of square wave.  1 / 2 of it for the high and low portion of the pulse is 250 us.  250 us / 1.085 us = 230 and 65536 – 230 = 65306 which in hex is FF1AH.  TL = 1A andTH = FF, all in hex.
  • 13. Example No.2 (Mode 1 Programming) Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon13 Program: ORG 00H MOV P1, # 0000 0000 B MOV TMOD,#0000 0001 B ;Timer 0, 16-bitmode MAIN: SETB P1.5 ; make P1.5 as high ACALL DELAY CLR P1.5 ; make P1.5 as low ACALL DELAY SJMP MAIN ;Reload timer DELAY: MOV TL1,#1AH ;TL1=1A,low byte of timer MOV TH1,#0FFH ;TH1=FF, the high byte SETB TR0 ;Start timer 0 BACK: JNB TF0, BACK ;until timer rolls over CLR TR0 ;Stop the timer 0 CLR TF0 ;Clear timer 0 flag SETB P1.5 RET END
  • 14. Example No.3 (Mode 2 Programming) Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon14  Assume XTAL = 11.0592 MHz, find the frequency of the square wave generated on pin P1.0 in the following program MOVTMOD,#20H ;T1/8-bit/auto reload MOVTH1,#5 ;TH1 = 5 SETBTR1 ;start the timer 1 BACK: JNB TF1,BACK ;till timer rolls over CPL P1.0 ;P1.0 to high, low CLRTF1 ;clearTimer 1 flag SJMP BACK ;mode 2 is auto-reload Solution:  In mode 2 we do not need to reloadTH since it is auto-reload.  Delay = (256 - 05) × 1.085 us = 251 × 1.085 us = 272.33 us is the high portion of the pulse. Since  it is a 50% duty cycle square wave, the periodT is twice that  T = 2 × 272.33 us = 544.67 us  frequency = 1.83597 kHz
  • 15. Example No.4 (Mode 2 Programming) Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon15  Assume that XTAL = 22 MHz, write a program to generate a square wave of 1 kHz frequency on pin P1.2. Solution:  XTAL = 22MHz  Timer Frequency = 22/12 = 1.83 us  Clock Period = 1 / 1.83us = 0.546 us  T = 1 / f = 1 / 1 kHz = 1000 us the period of square wave.  1 / 2 of it for the high and low portion of the pulse is 500 us.  No. of Cycles in 5 ms = 500 us / 0.546 us = 915  No. of Cycles in 1 ms = 915/5 = 183 cycles  Value loaded toTH = FF H – B7H = 48 H
  • 16. Example No.4 (Mode 2 Programming) Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon16 Program: ORG 00H MOV TMOD, #02 H ;Timer 0, mode 2 MAIN: CPL P1.2 ; complement P1.2 MOV R6, #05 ; ; count for multiple delay DELAY: MOV TH0, #48H ;TH0=48 H SETB TR0 ;Start timer 0 BACK: JNB TF0, BACK ;until timer rolls over CLR TR0 ;Stop the timer 0 CLR TF0 ;Clear timer 0 flag DJNZ R0, DELAY ; repeat until R0= 0 SJMP MAIN ; repeat to get train of pulses END
  • 17. Counter Programming Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon17  Timers can also be used as counters counting events happening outside the 8051  When it is used as a counter, it is a pulse outside of the 8051 that increments theTH,TL registers  TMOD andTH,TL registers are the same as for the timer discussed previously  Programming the timer in the last section also applies to programming it as a counter Except the source of the frequency
  • 18. Counter Programming Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon18  The C/T bit in theTMOD registers decides the source of the clock for the timer  When C/T = 1, the timer is used as a counter and gets its pulses from outside the 8051  The counter counts up as pulses are fed from pins 14 and 15, these pins are calledT0 (timer 0 input) andT1 (timer 1 input)
  • 19. Counter Programming Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon19  GATE Bit inTMOD  If GATE = 1, the start and stop of the timer are done externally through pins P3.2 and P3.3 for timers 0 and 1, respectively  This hardware way allows to start or stop the timer externally at any time via a simple switch
  • 20. Example No. 5 (Counter Programming) Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon20  Assuming that clock pulses are fed into pinT1, write a program for counter 1 in mode 2 to count the pulses and display the state of theTL1 count on P2, which connects to 8 LEDs. Solution: MOVTM0D,#01100000B ;counter 1, mode 2, ;C/T=1 external pulses MOVTH1,#0 ;clearTH1 SETB P3.5 ;makeT1 input AGAIN: SETBTR1 ;start the counter BACK: MOV A,TL1 ;get copy ofTL MOV P2,A ;display it on port 2 JNBTF1,Back ;keep doing, ifTF = 0 CLRTR1 ;stop the counter 1 CLRTF1 ;makeTF=0 SJMPAGAIN ;keep doing it
  • 21. Reference Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon21  “The 8051 Microcontroller and Embedded Systems using Assembly and C” , Muhammad Ali Mazidi, Janice Gillispie Mazidi, Rolin D. McKinlay, Second Edition, Pearson publication
  • 22. Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon22