SlideShare a Scribd company logo
1 of 24
Download to read offline
1
Unit 5: 8051 Timers/Counters Programming
(Part 1)
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
Learning Outcomes
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon3
 Student will be able to
 IntroduceTimers/Counters of 8051 microcontroller
 Operating modes of 8051Timers
 Special Function Registers and configurations of 8051
Timers/Counters
 Apply knowledge of 8051 assembly language to write a code for
8051Timer
Introduction to Timers
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon4
 The 8051 has two timers/counters.
 Timer 0
 Timer 1
 They can be used
 as Timers to generate a time delay or
 as Event counters to count events happening outside the
microcontroller
 BothTimer 0 andTimer 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
Special Function Registers of Timers
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon5
 Timer 0 Register
 Timer 1 Register
 TMOD Register
 TCON Register
Timer 0 Register
6
 The 16 bit register ofTimer 0 is accessed as low byte and
high byte.
 Low byte register is called asTL0 (Timer 0 low Byte)
 High byte register is called asTH0 (Timer 0 High Byte)
 Can be assessed and read as any register
 Example:
MOVTL0,#4FH ; moves value 4F intoTL0
MOV R5,TH0 ; savesTH0 into R5
TH0 TL0
D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
Timer 1 Register
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon7
 The 16 bit register ofTimer 1 is accessed as low byte and
high byte.
 Low byte register is called asTL1 (Timer 1 low Byte)
 High byte register is called asTH1 (Timer 1 High Byte)
 Can be assessed and read as any register
TH1 TL1
D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
TMOD (Timer Mode) Register
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon8
 Byte addressable
 Both timers 0 and 1 use the same register, called
TMOD (timer mode) to set the various timer
operation modes
 It is a 8-bit register
 The lower 4 bits are forTimer 0
 The upper 4 bits are forTimer 1
 In each case,The lower 2 bits are used to set the timer mode
 The upper 2 bits to specify the operation
TMOD (Timer Mode) Register
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon9
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0
M1 M0 Mode Operating Mode
0 0 0 13-bitTimer mode
8-bit timer/counterTHx withTLx as 5-bit prescaler
0 1 1 16-bitTimer mode
16-bit timer/counterTHx andTLx cascaded. No
prescaler
1 0 2 8-bit auto reload
8-bit auto reload timer/counterTHx holds the value
which is to be reloadedTLx each time it overflows
1 1 3 SplitTimer mode
MSB LSB
TMOD (Timer Mode) Register
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon10
 GATE
 EveryTimer has means of starting and stopping
 Some do this by Hardware or Software
 Some do this by both
 8051 timer have both controls
 When, GATE = 1
 Hardware way of starting and stopping timer
 Timer/counter is enable only while the INTx pin is high and theTRx control pin is
set
 enables and disablesTimer by means of a signal brought to the INT1 pin (P3.3) /
INT0 pin (P3.2)
 When, GATE = 0
 Software way of starting and stopping timer
 Operates regardless of the logic state of INTx bit
 enabled whenever theTRx control bit is set
 To start timer use instruction “SETBTRx”
 To clear timer use instruction “CLRTRx”
TMOD (Timer Mode) Register
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon11
 C/T (Counter/Timer)
OSC 1/12
PinTx (P3.4/P3.5)
f f/12
C/T=0
C/T=1
To timer /counter
TMOD (Timer Mode) Register
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon12
 C/T (Counter/Timer)
 Decides whetherTimer is used as Delay Generator (Timer)
or An Event Counter (Counter)
 When, C/T = 0
 Time delay generator
 input from internal system clock
 When, C/T = 1
 Event counter
 Timer counts pulses brought to theT0 (P3.4) pin orT1 (P3.5)
pin
TCON (Timer Control) Register
 Bit addressable
 8 - bit register
 Upper 4 bits are used to storeTF andTR bits of both timersTimer
0 andTimer 1
 The lower 4 bits are reserved for controlling the interrupts bits
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon13
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
T1 T0 INT 1 INT 0
MSB LSB
TCON (Timer Control) Register
14
TCON Bit Function
TF1 TCON.7 Timer 1 Overflow Flag. set by hardware whenT/C 1 overflows.
Cleared by hardware
TR1 TCON.6 Timer 1 run control Bit. Set or cleared by software to turn
timer on/off
TF0 TCON.5 Timer 0 Overflow Flag. set by hardware whenT/C 0 overflows.
Cleared by hardware
TR0 TCON.4 Timer 0 run control Bit. Set or cleared by software to turn
timer on/off
IE1
IE0
TCON.3
TCON.1
Keep track of edge-triggered interrupt only
External Interrupt 1 edge flag bit (High to Low)
External Interrupt 0 edge flag bit (High to Low)
IT1
IT0
TCON.2
TCON.0
Set/cleared by software to specify falling edge/low level triggered
interrupt
Interrupt 1 type control bit (Low to High)
Interrupt 0 type control bit (Low to High)
Equivalent Instructions for TCON
15
ForTimer 0
SETB TR0 SETB TCON.4
CLR TR0 CLR TCON.4
SETB TF0 SETB TCON.5
CLR TF0 CLR TCON.5
ForTimer 1
SETB TR1 SETB TCON.6
CLR TR1 CLR TCON.6
SETB TF1 SETB TCON.7
CLR TF1 CLR TCON.7
How to Use Timer 0?
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon16
Operating Modes of Timer
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon17
M1 M0 Mode Operating Mode
0 0 0 13-bitTimer mode
8-bit timer/counterTHx withTLx as 5-bit prescaler
0 1 0 16-bitTimer mode
16-bit timer/counterTHx andTLx cascaded. No prescaler
1 0 2 8-bit auto reload
8-bit auto reload timer/counterTHx holds the value
which is to be reloadedTLx each time it overflows
1 1 3 SplitTimer mode
Mode 1
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon18
 It is a 16-bit timer
 Hence, it allows value of 0000 to FFFFH to be loaded into the
timer’s registerTL andTH
 AfterTH andTL are loaded with a 16-bit initial value, the
timer must be started.
 This is done by SETB TR0 for timer 0 and SETBTR1 for timer 1
 After the timer is started, it starts to count up. It counts up
until it reaches its limit of FFFFH
Mode 1
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon19
 When it rolls over from FFFFH to 0000, it sets high a flag bit calledTF
(timer flag)
 Each timer has its own timer flag:TF0 for timer 0, andTF1 for timer 1
 This timer flag can be monitored by using the instruction JNBTFx, target
(polling)
 When this timer flag is raised, one option would be to stop the timer with the
instructions CLRTR0 or CLRTR1, for timer 0 and timer 1, respectively
 After the timer reaches its limit and rolls over, in order to repeat the
process
 TH andTL must be reloaded with the original value, andTF must be
reloaded to 0
Mode 0
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon20
 Same like mode 1
 It is 13 bitTimer instead of 16-bit
 Can hold the values between 0000 H to 1FFF H inTH-TL
 Timer reaches its maximum of 1FFF, it rolls over to 0000
andTF raised
Mode 2 (Auto reload Timer)
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon21
 It is an 8-bit timer
 It allows only values of 00 to FFH to be loaded into the timer’s
registerTH
 AfterTH is loaded with the 8-bit value, the 8051 gives a copy of
it toTL
 Then the timer must be started
 This is done by the instruction SETB TR0 for timer 0 and SETB TR1
for timer 1
 After the timer is started, it starts to count up by incrementing
theTL register
 It counts up until it reaches its limit of FFH
 When it rolls over from FFH to 00, it sets high theTF (timer flag)
 ForTimer 0,TF0 goes high and forTimer 1,TF1 goes high
Mode 2 (Auto reload Timer)
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon22
 When theTL register rolls from FFH to 00H andTF is set to
1,TL is reloaded automatically with the original value kept
by theTH register
 To repeat the process, we must simply clearTF and let it go
without any need by the programmer to reload the original
value
 This makes mode 2 an auto-reload, in contrast with
mode 1 in which the programmer has to reloadTH andTL
TF goes high
when FF -> 00
Reference
Prepared By: Ms. K. D. Patil, Dept. of Information
Technology, Sanjivani COE, Kopargaon23
 “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, Kopargaon24

More Related Content

What's hot (20)

12 mt06ped019
12 mt06ped019 12 mt06ped019
12 mt06ped019
 
Interrupt programming with 8051 microcontroller
Interrupt programming with 8051  microcontrollerInterrupt programming with 8051  microcontroller
Interrupt programming with 8051 microcontroller
 
Interrupt programming
Interrupt programming Interrupt programming
Interrupt programming
 
3 jump, loop and call instructions
3 jump, loop and call instructions3 jump, loop and call instructions
3 jump, loop and call instructions
 
Timer And Counter in 8051 Microcontroller
Timer And Counter in 8051 MicrocontrollerTimer And Counter in 8051 Microcontroller
Timer And Counter in 8051 Microcontroller
 
8051 assembly programming
8051 assembly programming8051 assembly programming
8051 assembly programming
 
8051 Timer
8051 Timer8051 Timer
8051 Timer
 
Class9
Class9Class9
Class9
 
Serial Communication in 8051
Serial Communication in 8051Serial Communication in 8051
Serial Communication in 8051
 
8051 ch9
8051 ch98051 ch9
8051 ch9
 
8051 ch9-950217
8051 ch9-9502178051 ch9-950217
8051 ch9-950217
 
Subroutine in 8051 microcontroller
Subroutine in 8051 microcontrollerSubroutine in 8051 microcontroller
Subroutine in 8051 microcontroller
 
Programming 8051 Timers
Programming 8051 Timers Programming 8051 Timers
Programming 8051 Timers
 
8051 timers--2
   8051 timers--2   8051 timers--2
8051 timers--2
 
8051 Presentation
8051 Presentation8051 Presentation
8051 Presentation
 
Lab5 s2
Lab5 s2Lab5 s2
Lab5 s2
 
Lab5 s1
Lab5 s1Lab5 s1
Lab5 s1
 
8051-mazidi-solution
8051-mazidi-solution8051-mazidi-solution
8051-mazidi-solution
 
Introduction to Microcontrollers
Introduction to MicrocontrollersIntroduction to Microcontrollers
Introduction to Microcontrollers
 
I o ports and timers of 8051
I o ports and timers of 8051I o ports and timers of 8051
I o ports and timers of 8051
 

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

Timer programming for 8051 using embedded c
Timer programming for 8051 using embedded cTimer programming for 8051 using embedded c
Timer programming for 8051 using embedded cVikas Dongre
 
lecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.pptlecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.pptHebaEng
 
8051 timer counter
8051 timer counter8051 timer counter
8051 timer counterankit3991
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and CountersShreyans Pathak
 
Micro c lab7(timers)
Micro c lab7(timers)Micro c lab7(timers)
Micro c lab7(timers)Mashood
 
UNIT 5 Interfacing and Mixed Signal Controller.pptx
UNIT 5 Interfacing and Mixed Signal Controller.pptxUNIT 5 Interfacing and Mixed Signal Controller.pptx
UNIT 5 Interfacing and Mixed Signal Controller.pptxGowrishankar C
 
8051 Timers / Counters
8051 Timers / Counters8051 Timers / Counters
8051 Timers / CountersPatricio Lima
 
timer counter (1).pptx
timer counter (1).pptxtimer counter (1).pptx
timer counter (1).pptxSujalKumar73
 
Microprocessor 8051
Microprocessor 8051Microprocessor 8051
Microprocessor 8051Anil Maurya
 
Unit 5_interrupt programming_Part 1
Unit 5_interrupt programming_Part 1Unit 5_interrupt programming_Part 1
Unit 5_interrupt programming_Part 1KanchanPatil34
 

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

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
 
Timers
TimersTimers
Timers
 
lecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.pptlecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.ppt
 
8051 timer counter
8051 timer counter8051 timer counter
8051 timer counter
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and Counters
 
Micro c lab7(timers)
Micro c lab7(timers)Micro c lab7(timers)
Micro c lab7(timers)
 
UNIT 5 Interfacing and Mixed Signal Controller.pptx
UNIT 5 Interfacing and Mixed Signal Controller.pptxUNIT 5 Interfacing and Mixed Signal Controller.pptx
UNIT 5 Interfacing and Mixed Signal Controller.pptx
 
4.Timer_1.ppt
4.Timer_1.ppt4.Timer_1.ppt
4.Timer_1.ppt
 
TIMERS.pptx
TIMERS.pptxTIMERS.pptx
TIMERS.pptx
 
8051 Timers / Counters
8051 Timers / Counters8051 Timers / Counters
8051 Timers / Counters
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 
timer counter (1).pptx
timer counter (1).pptxtimer counter (1).pptx
timer counter (1).pptx
 
UNIT-5.ppt
UNIT-5.pptUNIT-5.ppt
UNIT-5.ppt
 
Timers of 8051
Timers of 8051Timers of 8051
Timers of 8051
 
8051 timer counter
8051 timer counter8051 timer counter
8051 timer counter
 
Timers
TimersTimers
Timers
 
8051e
8051e8051e
8051e
 
Microprocessor 8051
Microprocessor 8051Microprocessor 8051
Microprocessor 8051
 
Unit 5_interrupt programming_Part 1
Unit 5_interrupt programming_Part 1Unit 5_interrupt programming_Part 1
Unit 5_interrupt programming_Part 1
 
8051 timers
8051 timers8051 timers
8051 timers
 

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.pdfKanchanPatil34
 
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.pdfKanchanPatil34
 
Unit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdfUnit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdfKanchanPatil34
 
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.pdfKanchanPatil34
 
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 MicroporcessorKanchanPatil34
 
PAI Unit 3 Multitasking in 80386
PAI Unit 3 Multitasking in 80386PAI Unit 3 Multitasking in 80386
PAI Unit 3 Multitasking in 80386KanchanPatil34
 
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 microprocessorKanchanPatil34
 
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 segmentationKanchanPatil34
 
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 segmentationKanchanPatil34
 
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 2KanchanPatil34
 
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 1KanchanPatil34
 
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 idtKanchanPatil34
 
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 80386KanchanPatil34
 
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 callsKanchanPatil34
 
DELD Unit IV ring and twisted ring counter
DELD Unit IV ring and twisted ring counterDELD Unit IV ring and twisted ring counter
DELD Unit IV ring and twisted ring counterKanchanPatil34
 
DELD Unit IV operation modes of shift register
DELD Unit IV operation modes of shift registerDELD Unit IV operation modes of shift register
DELD Unit IV operation modes of shift registerKanchanPatil34
 

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
 
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
 
DELD Unit IV ring and twisted ring counter
DELD Unit IV ring and twisted ring counterDELD Unit IV ring and twisted ring counter
DELD Unit IV ring and twisted ring counter
 
DELD Unit IV operation modes of shift register
DELD Unit IV operation modes of shift registerDELD Unit IV operation modes of shift register
DELD Unit IV operation modes of shift register
 
DELD Unit 1 BJT
DELD Unit 1 BJTDELD Unit 1 BJT
DELD Unit 1 BJT
 

Recently uploaded

Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 

Recently uploaded (20)

Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 

SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 1

  • 1. 1 Unit 5: 8051 Timers/Counters Programming (Part 1) 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. Learning Outcomes Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon3  Student will be able to  IntroduceTimers/Counters of 8051 microcontroller  Operating modes of 8051Timers  Special Function Registers and configurations of 8051 Timers/Counters  Apply knowledge of 8051 assembly language to write a code for 8051Timer
  • 4. Introduction to Timers Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon4  The 8051 has two timers/counters.  Timer 0  Timer 1  They can be used  as Timers to generate a time delay or  as Event counters to count events happening outside the microcontroller  BothTimer 0 andTimer 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
  • 5. Special Function Registers of Timers Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon5  Timer 0 Register  Timer 1 Register  TMOD Register  TCON Register
  • 6. Timer 0 Register 6  The 16 bit register ofTimer 0 is accessed as low byte and high byte.  Low byte register is called asTL0 (Timer 0 low Byte)  High byte register is called asTH0 (Timer 0 High Byte)  Can be assessed and read as any register  Example: MOVTL0,#4FH ; moves value 4F intoTL0 MOV R5,TH0 ; savesTH0 into R5 TH0 TL0 D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
  • 7. Timer 1 Register Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon7  The 16 bit register ofTimer 1 is accessed as low byte and high byte.  Low byte register is called asTL1 (Timer 1 low Byte)  High byte register is called asTH1 (Timer 1 High Byte)  Can be assessed and read as any register TH1 TL1 D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
  • 8. TMOD (Timer Mode) Register Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon8  Byte addressable  Both timers 0 and 1 use the same register, called TMOD (timer mode) to set the various timer operation modes  It is a 8-bit register  The lower 4 bits are forTimer 0  The upper 4 bits are forTimer 1  In each case,The lower 2 bits are used to set the timer mode  The upper 2 bits to specify the operation
  • 9. TMOD (Timer Mode) Register Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon9 GATE C/T M1 M0 GATE C/T M1 M0 Timer 1 Timer 0 M1 M0 Mode Operating Mode 0 0 0 13-bitTimer mode 8-bit timer/counterTHx withTLx as 5-bit prescaler 0 1 1 16-bitTimer mode 16-bit timer/counterTHx andTLx cascaded. No prescaler 1 0 2 8-bit auto reload 8-bit auto reload timer/counterTHx holds the value which is to be reloadedTLx each time it overflows 1 1 3 SplitTimer mode MSB LSB
  • 10. TMOD (Timer Mode) Register Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon10  GATE  EveryTimer has means of starting and stopping  Some do this by Hardware or Software  Some do this by both  8051 timer have both controls  When, GATE = 1  Hardware way of starting and stopping timer  Timer/counter is enable only while the INTx pin is high and theTRx control pin is set  enables and disablesTimer by means of a signal brought to the INT1 pin (P3.3) / INT0 pin (P3.2)  When, GATE = 0  Software way of starting and stopping timer  Operates regardless of the logic state of INTx bit  enabled whenever theTRx control bit is set  To start timer use instruction “SETBTRx”  To clear timer use instruction “CLRTRx”
  • 11. TMOD (Timer Mode) Register Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon11  C/T (Counter/Timer) OSC 1/12 PinTx (P3.4/P3.5) f f/12 C/T=0 C/T=1 To timer /counter
  • 12. TMOD (Timer Mode) Register Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon12  C/T (Counter/Timer)  Decides whetherTimer is used as Delay Generator (Timer) or An Event Counter (Counter)  When, C/T = 0  Time delay generator  input from internal system clock  When, C/T = 1  Event counter  Timer counts pulses brought to theT0 (P3.4) pin orT1 (P3.5) pin
  • 13. TCON (Timer Control) Register  Bit addressable  8 - bit register  Upper 4 bits are used to storeTF andTR bits of both timersTimer 0 andTimer 1  The lower 4 bits are reserved for controlling the interrupts bits Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon13 TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 T1 T0 INT 1 INT 0 MSB LSB
  • 14. TCON (Timer Control) Register 14 TCON Bit Function TF1 TCON.7 Timer 1 Overflow Flag. set by hardware whenT/C 1 overflows. Cleared by hardware TR1 TCON.6 Timer 1 run control Bit. Set or cleared by software to turn timer on/off TF0 TCON.5 Timer 0 Overflow Flag. set by hardware whenT/C 0 overflows. Cleared by hardware TR0 TCON.4 Timer 0 run control Bit. Set or cleared by software to turn timer on/off IE1 IE0 TCON.3 TCON.1 Keep track of edge-triggered interrupt only External Interrupt 1 edge flag bit (High to Low) External Interrupt 0 edge flag bit (High to Low) IT1 IT0 TCON.2 TCON.0 Set/cleared by software to specify falling edge/low level triggered interrupt Interrupt 1 type control bit (Low to High) Interrupt 0 type control bit (Low to High)
  • 15. Equivalent Instructions for TCON 15 ForTimer 0 SETB TR0 SETB TCON.4 CLR TR0 CLR TCON.4 SETB TF0 SETB TCON.5 CLR TF0 CLR TCON.5 ForTimer 1 SETB TR1 SETB TCON.6 CLR TR1 CLR TCON.6 SETB TF1 SETB TCON.7 CLR TF1 CLR TCON.7
  • 16. How to Use Timer 0? Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon16
  • 17. Operating Modes of Timer Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon17 M1 M0 Mode Operating Mode 0 0 0 13-bitTimer mode 8-bit timer/counterTHx withTLx as 5-bit prescaler 0 1 0 16-bitTimer mode 16-bit timer/counterTHx andTLx cascaded. No prescaler 1 0 2 8-bit auto reload 8-bit auto reload timer/counterTHx holds the value which is to be reloadedTLx each time it overflows 1 1 3 SplitTimer mode
  • 18. Mode 1 Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon18  It is a 16-bit timer  Hence, it allows value of 0000 to FFFFH to be loaded into the timer’s registerTL andTH  AfterTH andTL are loaded with a 16-bit initial value, the timer must be started.  This is done by SETB TR0 for timer 0 and SETBTR1 for timer 1  After the timer is started, it starts to count up. It counts up until it reaches its limit of FFFFH
  • 19. Mode 1 Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon19  When it rolls over from FFFFH to 0000, it sets high a flag bit calledTF (timer flag)  Each timer has its own timer flag:TF0 for timer 0, andTF1 for timer 1  This timer flag can be monitored by using the instruction JNBTFx, target (polling)  When this timer flag is raised, one option would be to stop the timer with the instructions CLRTR0 or CLRTR1, for timer 0 and timer 1, respectively  After the timer reaches its limit and rolls over, in order to repeat the process  TH andTL must be reloaded with the original value, andTF must be reloaded to 0
  • 20. Mode 0 Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon20  Same like mode 1  It is 13 bitTimer instead of 16-bit  Can hold the values between 0000 H to 1FFF H inTH-TL  Timer reaches its maximum of 1FFF, it rolls over to 0000 andTF raised
  • 21. Mode 2 (Auto reload Timer) Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon21  It is an 8-bit timer  It allows only values of 00 to FFH to be loaded into the timer’s registerTH  AfterTH is loaded with the 8-bit value, the 8051 gives a copy of it toTL  Then the timer must be started  This is done by the instruction SETB TR0 for timer 0 and SETB TR1 for timer 1  After the timer is started, it starts to count up by incrementing theTL register  It counts up until it reaches its limit of FFH  When it rolls over from FFH to 00, it sets high theTF (timer flag)  ForTimer 0,TF0 goes high and forTimer 1,TF1 goes high
  • 22. Mode 2 (Auto reload Timer) Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon22  When theTL register rolls from FFH to 00H andTF is set to 1,TL is reloaded automatically with the original value kept by theTH register  To repeat the process, we must simply clearTF and let it go without any need by the programmer to reload the original value  This makes mode 2 an auto-reload, in contrast with mode 1 in which the programmer has to reloadTH andTL TF goes high when FF -> 00
  • 23. Reference Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon23  “The 8051 Microcontroller and Embedded Systems using Assembly and C” , Muhammad Ali Mazidi, Janice Gillispie Mazidi, Rolin D. McKinlay, Second Edition, Pearson publication
  • 24. Prepared By: Ms. K. D. Patil, Dept. of Information Technology, Sanjivani COE, Kopargaon24