SlideShare a Scribd company logo
1 of 36
Chapter 6
Working with Time: Interrupts, Counters and Timers
The aims of this chapter are to introduce:
• Why we need interrupts and counter/timers;
• The underlying interrupt hardware structure;
• The 16F84A interrupt structure;
• How to write simple programs with interrupts;
• The underlying microcontroller counter/timer hardware structure;
• The 16F84A Timer 0 structure;
• Simple applications of the counter/timer;
• The Sleep mode.
Designing Embedded Systems
with PIC Microcontrollers:
Principles and Applications
2nd Edition. Tim Wilmshurst
1
An Interrupt Review.
An Interrupt is an external input to the CPU. The interrupt facility
allows the processor to respond rapidly to external changes.
When an Interrupt is detected by the CPU, it:
 completes the current instruction,
 stores the address of the next instruction, and possibly other
key variables (e.g. contents of Accumulator and Condition
Code Register), onto the stack,
 jumps to an interrupt service routine (ISR), whose address is
determined by an "interrupt vector".
2
•Many interrupts can be masked, i.e. disabled, by setting a bit in a
control register. some are not maskable.
•If an Interrupt is masked, then there is a possibility that it will not
be detected.
•Therefore there are also Interrupt Flags, bits in SFRs, which are
set whenever an associated interrupt occurs.
An Interrupt Review
3
•These record the fact that an interrupt has occurred, even if the
CPU is unable to respond to it.
•An Interrupt that has occurred, but has not received CPU
response, is called a Pending Interrupt.
•In the case of several interrupts, one ISR is completed before the
next interrupt is responded to.
An Interrupt Review … Cont.
4
Control SFR(s)
Peripheral
Data Transfer SFR(s)
Microcontroller
Core
"Outside
World"
Interrupt(s)
Microcontroller Interaction with
Peripherals, via SFR and Interrupt
Recalling Interrupt-Related
Points that have already Come up
Interrupt Routine
always starts here
The Reset Vector
5
S Q
Int errupt
Flag*
Inte rrupt X
Int errupt X Enable*
Ot her
G
lobal Int errupt
Non-maskabl e
Inte rrupt
Int erru
input s t
CPU
R
(reset by CPU
or program)
* bit s in a S
pecial Funct ion Regist er
int errupt s
maskable
replicated f
or all other m askable interrupts
A Generic Interrupt Structure
6
SR Flipflop
7
The PIC 16F84A Interrupt Structure
Global Interrupt Enable
External Interrupt
EEPROM
Write Complete
Port B Change
Timer Overflow
Interrupt Flag
Note that the interrupt flags are set by the interrupt action, but
must be cleared (set to “0”) in the program, during the ISR.
What does happen if this isn’t done?
8
9
The PIC 16F84A INTCON Register
The PIC 16F84A INTCON Register
10
The PIC 16F84A Interrupt Structure
RA2
RA3
RA4/T0CKI
MCLR
V
RB0/INT
RB1
RB2
RB3 RB4
RB5
RB6
RB7
RA1
RA0
O S C1/CLKIN
O S C2/CLKO UT
V
DD
SS Sup p ly voltage
Oscillator co
Port A, Bit 0
Port A, Bit 1
Port A, Bit 2
Port A, Bit 3
*Port A, Bit 4
Ground
**Port B, Bit 0
Port B, Bit 1
Port B, Bit 2
Port B, Bit 3
Port B, Bit 7
Port B, Bit 6
Port B, Bit 5
Port B, Bit 4
*also Counter/T imer clock inp ut
**also external Interrup t inp ut
Reset
1
9 10
18
External
Interrupt
input
11
Interrupt Detected
Complete Current Instruction
Save Program Counter on Stack
Reload PC with 0004H
Continue Program Execution
Instruction
is RETFIE?
No
Set GIE to 1
Load PC from Stack
Continue Program Execution
Yes
Clear GIE
ISR execution starts
main program is running
main program continues
The PIC 16 Series Interrupt Response
• Note that this diagram shows what the
PIC microcontroller itself does as an
interrupt occurs.
• The programmer need not worry about
any of these actions, but needs to
know that they’re happening.
Interrupt Detected
Complete Current Instruction
Save Program Counter on Stack
Reload PC with 0004H
Continue Program Execution
Instruction
is RETFIE?
No
Set GIE to 1
Load PC from Stack
Continue Program Execution
Yes
Clear GIE
ISR execution starts
main program is running
main program continues
12
Programming with Single Interrupts
It is easy to write simple programs with just one interrupt. For
success, the essential points to watch are:
• Start the ISR at the Interrupt Vector, location 0004;
• Enable the interrupt that is to be used, by setting enable bits in
the INTCON and/or PIE (Peripheral Interrupt Enable) registers;
• Set the Global Enable bit, GIE;
• Once in the ISR, clear the interrupt flag;
• End the ISR with a retfie instruction;
• Ensure that the interrupt source, for example Port B or Timer 0, is
actually set up to generate interrupts!
13
org 00
goto start
org 04 ;here if interrupt occurs
goto Int_Routine
start
org 0010
;bcf option_reg,intedg
bcf STATUS,RP0 ;select bank 0
bsf INTCON,INTE ;enable external interrupt
bsf INTCON,GIE ;enable global int
wait movlw 0A ;set up initial port output values
movwf porta
nop
movlw 15
movwf porta
goto wait
org 0080
Int_Routine
movlw 00
movwf porta
bcf INTCON,INTF ;clear the interrupt flag
RETFIE
end
A Simple Interrupt
Application
14
The INTCON Register of a PIC 16F84A is set as shown in below:
a) Determine which interrupts are enabled.
b) An interrupt occurs, and the INTCON register is found to have
changed to b).
Which interrupt source has called?
c) Which bit must the user change before end of ISR?
IN
TCON IN
TCON
1
0
1
0
1
0
0
0 1
0
1 1
0 0
0
1
Interrupt Example 1
a) b)
15
Moving to Multiple Interrupts – Identifying the Source
As we have seen, the 16F84A has four interrupt sources, but only
one interrupt vector.
Therefore, if more than one interrupt is enabled, it is not obvious at
the beginning of an ISR which interrupt has occurred. In this case
the programmer must write the ISR so that at its beginning it tests
the flags of all possible interrupts and determines from this which
one has been called, This is shown in the example ISR below.
16
Interrupt
btfsc intcon,0 ;test RBIF
goto portb_int
btfsc intcon,1 ;test external interrupt flag
goto ext_int
btfsc intcon,2 ;test timer overflow flag
goto timer_int
retfie
portb_int
place portb change ISR here ...
bcf intcon,0 ;and clear the interrupt flag
retfie
ext_int
place external interrupt ISR here ...
bcf intcon,1 ;and clear the interrupt flag
retfie
timer_int
place timer overflow ISR goes here ...
bcf intcon,2 ;and clear the interrupt flag
retfie
Moving to Multiple
Interrupts –
Identifying the Source
17
Critical Regions and Masking
In certain program parts we will not want to accept the intrusion of an interrupt
under any circumstances, with or without context saving. We call these critical
regions. We can disable, or mask, the interrupts for their duration, by
manipulating the enable bits in the INTCON register.
Critical regions may include:
1. times when the microcontroller is simply not ready to act on the interrupt
(for example during initialization – hence only enable interrupts after
initialization is complete);
2. time-sensitive activity, including timing loops and multi-instruction setting
of outputs;
3. any calculation made up of a series of instructions where the ISR makes
use of the result. 18
The purpose of the interrupt is to attract the attention of the CPU
quickly, but actually how quickly does this happen? The time
between the interrupt occurring and the CPU responding to it is
called the latency.
This depends on certain aspects of hardware and on the
characteristics of the program running. This timing diagram shows
how the mid-range PIC family responds to an enabled external
interrupt.
Taking Things Further: Interrupt Latency
19
The Digital Counter Reviewed
• It is easy to make a digital counter using flip-flops.
• Counters can be made which count up, count down, which can
be cleared back to zero, pre-loaded to a certain value, and which
by the provision of an overflow output can be cascaded with
other counters. A simple example is shown:
21
The Digital Counter Reviewed
22
The Digital Counter Reviewed
23
The Digital Counter Reviewed
24
Input
Q0
Q1
The Digital Counter Reviewed
25
The Counter as Timer
• If the incoming clock pulses are regular in frequency, the
counter can also measure time.
• In this example time is being measured between two pulses.
Clock
Pulse Input
T
1 2 3 4 5 n
Tc
26
Clock
Pulse Input
T
1 2 3 4 5 n
Tc
The Counter as Timer
If TC is clock period, and n cycles are counted, then the period during which
counting has taken place is nTC .
Example: clock frequency is 1 MHz, clock period is therefore 1us, before
overflow counter can count:
8-bit 255us
16-bit 65535us = 65.5ms
24-bit 16.78 secs
32-bit 4,295 secs = 1hr, 11minutes 27
Timer Prescalar
28
A prescaler is an electronic counting circuit used to reduce a
high frequency electrical signal to a lower frequency by integer
division.
Timer Prescalar
29
Timer Prescalar
30
Timer Prescalar
31
8-bit Counter
Multiplexer
selecting
counting source
Multiplexer
selecting
prescaler
Input edge
select
The 16F84A TIMER0 Module
32
The 16F84A OPTION Register
34
The 16F84A OPTION Register
35
End of Chapter 6
36
37

More Related Content

Similar to ES-CH6.ppt

NIE2206 Electronic LogbookNamexxxStudent IDUxxxTe.docx
NIE2206 Electronic LogbookNamexxxStudent IDUxxxTe.docxNIE2206 Electronic LogbookNamexxxStudent IDUxxxTe.docx
NIE2206 Electronic LogbookNamexxxStudent IDUxxxTe.docxcurwenmichaela
 
Computer Organization : CPU, Memory and I/O organization
Computer Organization : CPU, Memory and I/O organizationComputer Organization : CPU, Memory and I/O organization
Computer Organization : CPU, Memory and I/O organizationAmrutaMehata
 
conrol_Unit_part_of_computer_architecture.pptx
conrol_Unit_part_of_computer_architecture.pptxconrol_Unit_part_of_computer_architecture.pptx
conrol_Unit_part_of_computer_architecture.pptxjbri1395
 
Interrupts for PIC18
Interrupts for PIC18Interrupts for PIC18
Interrupts for PIC18raosandy11
 
My seminar new 28
My seminar new 28My seminar new 28
My seminar new 28rajeshkvdn
 
Interrupts on 8086 microprocessor by vijay kumar.k
Interrupts on 8086 microprocessor by vijay kumar.kInterrupts on 8086 microprocessor by vijay kumar.k
Interrupts on 8086 microprocessor by vijay kumar.kVijay Kumar
 
Using Ready-for-PIC and SDR Libraries
Using Ready-for-PIC and SDR LibrariesUsing Ready-for-PIC and SDR Libraries
Using Ready-for-PIC and SDR LibrariesCorrado Santoro
 
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdfDEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdfAkritiPradhan2
 
Chp4 introduction to the pic microcontroller copy
Chp4 introduction to the pic microcontroller   copyChp4 introduction to the pic microcontroller   copy
Chp4 introduction to the pic microcontroller copymkazree
 
Xcs 234 microprocessors
Xcs 234 microprocessorsXcs 234 microprocessors
Xcs 234 microprocessorssweta suman
 
Design of an Embedded Micro controller
Design of an Embedded Micro controllerDesign of an Embedded Micro controller
Design of an Embedded Micro controllerDaniel Gomez-Prado
 

Similar to ES-CH6.ppt (20)

NIE2206 Electronic LogbookNamexxxStudent IDUxxxTe.docx
NIE2206 Electronic LogbookNamexxxStudent IDUxxxTe.docxNIE2206 Electronic LogbookNamexxxStudent IDUxxxTe.docx
NIE2206 Electronic LogbookNamexxxStudent IDUxxxTe.docx
 
Pic full note
Pic full notePic full note
Pic full note
 
Computer Organization : CPU, Memory and I/O organization
Computer Organization : CPU, Memory and I/O organizationComputer Organization : CPU, Memory and I/O organization
Computer Organization : CPU, Memory and I/O organization
 
conrol_Unit_part_of_computer_architecture.pptx
conrol_Unit_part_of_computer_architecture.pptxconrol_Unit_part_of_computer_architecture.pptx
conrol_Unit_part_of_computer_architecture.pptx
 
Arm
ArmArm
Arm
 
Interrupts for PIC18
Interrupts for PIC18Interrupts for PIC18
Interrupts for PIC18
 
Microcontroller part 2
Microcontroller part 2Microcontroller part 2
Microcontroller part 2
 
W10: Interrupts
W10: InterruptsW10: Interrupts
W10: Interrupts
 
Pic16f84
Pic16f84Pic16f84
Pic16f84
 
My seminar new 28
My seminar new 28My seminar new 28
My seminar new 28
 
Interrupts on 8086 microprocessor by vijay kumar.k
Interrupts on 8086 microprocessor by vijay kumar.kInterrupts on 8086 microprocessor by vijay kumar.k
Interrupts on 8086 microprocessor by vijay kumar.k
 
Real Time Embedded System
Real Time Embedded SystemReal Time Embedded System
Real Time Embedded System
 
Presentation
PresentationPresentation
Presentation
 
Introduction to PIC.pptx
Introduction to PIC.pptxIntroduction to PIC.pptx
Introduction to PIC.pptx
 
Using Ready-for-PIC and SDR Libraries
Using Ready-for-PIC and SDR LibrariesUsing Ready-for-PIC and SDR Libraries
Using Ready-for-PIC and SDR Libraries
 
ES-CH5.ppt
ES-CH5.pptES-CH5.ppt
ES-CH5.ppt
 
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdfDEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
 
Chp4 introduction to the pic microcontroller copy
Chp4 introduction to the pic microcontroller   copyChp4 introduction to the pic microcontroller   copy
Chp4 introduction to the pic microcontroller copy
 
Xcs 234 microprocessors
Xcs 234 microprocessorsXcs 234 microprocessors
Xcs 234 microprocessors
 
Design of an Embedded Micro controller
Design of an Embedded Micro controllerDesign of an Embedded Micro controller
Design of an Embedded Micro controller
 

Recently uploaded

College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 

Recently uploaded (20)

College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 

ES-CH6.ppt

  • 1. Chapter 6 Working with Time: Interrupts, Counters and Timers The aims of this chapter are to introduce: • Why we need interrupts and counter/timers; • The underlying interrupt hardware structure; • The 16F84A interrupt structure; • How to write simple programs with interrupts; • The underlying microcontroller counter/timer hardware structure; • The 16F84A Timer 0 structure; • Simple applications of the counter/timer; • The Sleep mode. Designing Embedded Systems with PIC Microcontrollers: Principles and Applications 2nd Edition. Tim Wilmshurst 1
  • 2. An Interrupt Review. An Interrupt is an external input to the CPU. The interrupt facility allows the processor to respond rapidly to external changes. When an Interrupt is detected by the CPU, it:  completes the current instruction,  stores the address of the next instruction, and possibly other key variables (e.g. contents of Accumulator and Condition Code Register), onto the stack,  jumps to an interrupt service routine (ISR), whose address is determined by an "interrupt vector". 2
  • 3. •Many interrupts can be masked, i.e. disabled, by setting a bit in a control register. some are not maskable. •If an Interrupt is masked, then there is a possibility that it will not be detected. •Therefore there are also Interrupt Flags, bits in SFRs, which are set whenever an associated interrupt occurs. An Interrupt Review 3
  • 4. •These record the fact that an interrupt has occurred, even if the CPU is unable to respond to it. •An Interrupt that has occurred, but has not received CPU response, is called a Pending Interrupt. •In the case of several interrupts, one ISR is completed before the next interrupt is responded to. An Interrupt Review … Cont. 4
  • 5. Control SFR(s) Peripheral Data Transfer SFR(s) Microcontroller Core "Outside World" Interrupt(s) Microcontroller Interaction with Peripherals, via SFR and Interrupt Recalling Interrupt-Related Points that have already Come up Interrupt Routine always starts here The Reset Vector 5
  • 6. S Q Int errupt Flag* Inte rrupt X Int errupt X Enable* Ot her G lobal Int errupt Non-maskabl e Inte rrupt Int erru input s t CPU R (reset by CPU or program) * bit s in a S pecial Funct ion Regist er int errupt s maskable replicated f or all other m askable interrupts A Generic Interrupt Structure 6
  • 8. The PIC 16F84A Interrupt Structure Global Interrupt Enable External Interrupt EEPROM Write Complete Port B Change Timer Overflow Interrupt Flag Note that the interrupt flags are set by the interrupt action, but must be cleared (set to “0”) in the program, during the ISR. What does happen if this isn’t done? 8
  • 9. 9 The PIC 16F84A INTCON Register
  • 10. The PIC 16F84A INTCON Register 10
  • 11. The PIC 16F84A Interrupt Structure RA2 RA3 RA4/T0CKI MCLR V RB0/INT RB1 RB2 RB3 RB4 RB5 RB6 RB7 RA1 RA0 O S C1/CLKIN O S C2/CLKO UT V DD SS Sup p ly voltage Oscillator co Port A, Bit 0 Port A, Bit 1 Port A, Bit 2 Port A, Bit 3 *Port A, Bit 4 Ground **Port B, Bit 0 Port B, Bit 1 Port B, Bit 2 Port B, Bit 3 Port B, Bit 7 Port B, Bit 6 Port B, Bit 5 Port B, Bit 4 *also Counter/T imer clock inp ut **also external Interrup t inp ut Reset 1 9 10 18 External Interrupt input 11
  • 12. Interrupt Detected Complete Current Instruction Save Program Counter on Stack Reload PC with 0004H Continue Program Execution Instruction is RETFIE? No Set GIE to 1 Load PC from Stack Continue Program Execution Yes Clear GIE ISR execution starts main program is running main program continues The PIC 16 Series Interrupt Response • Note that this diagram shows what the PIC microcontroller itself does as an interrupt occurs. • The programmer need not worry about any of these actions, but needs to know that they’re happening. Interrupt Detected Complete Current Instruction Save Program Counter on Stack Reload PC with 0004H Continue Program Execution Instruction is RETFIE? No Set GIE to 1 Load PC from Stack Continue Program Execution Yes Clear GIE ISR execution starts main program is running main program continues 12
  • 13. Programming with Single Interrupts It is easy to write simple programs with just one interrupt. For success, the essential points to watch are: • Start the ISR at the Interrupt Vector, location 0004; • Enable the interrupt that is to be used, by setting enable bits in the INTCON and/or PIE (Peripheral Interrupt Enable) registers; • Set the Global Enable bit, GIE; • Once in the ISR, clear the interrupt flag; • End the ISR with a retfie instruction; • Ensure that the interrupt source, for example Port B or Timer 0, is actually set up to generate interrupts! 13
  • 14. org 00 goto start org 04 ;here if interrupt occurs goto Int_Routine start org 0010 ;bcf option_reg,intedg bcf STATUS,RP0 ;select bank 0 bsf INTCON,INTE ;enable external interrupt bsf INTCON,GIE ;enable global int wait movlw 0A ;set up initial port output values movwf porta nop movlw 15 movwf porta goto wait org 0080 Int_Routine movlw 00 movwf porta bcf INTCON,INTF ;clear the interrupt flag RETFIE end A Simple Interrupt Application 14
  • 15. The INTCON Register of a PIC 16F84A is set as shown in below: a) Determine which interrupts are enabled. b) An interrupt occurs, and the INTCON register is found to have changed to b). Which interrupt source has called? c) Which bit must the user change before end of ISR? IN TCON IN TCON 1 0 1 0 1 0 0 0 1 0 1 1 0 0 0 1 Interrupt Example 1 a) b) 15
  • 16. Moving to Multiple Interrupts – Identifying the Source As we have seen, the 16F84A has four interrupt sources, but only one interrupt vector. Therefore, if more than one interrupt is enabled, it is not obvious at the beginning of an ISR which interrupt has occurred. In this case the programmer must write the ISR so that at its beginning it tests the flags of all possible interrupts and determines from this which one has been called, This is shown in the example ISR below. 16
  • 17. Interrupt btfsc intcon,0 ;test RBIF goto portb_int btfsc intcon,1 ;test external interrupt flag goto ext_int btfsc intcon,2 ;test timer overflow flag goto timer_int retfie portb_int place portb change ISR here ... bcf intcon,0 ;and clear the interrupt flag retfie ext_int place external interrupt ISR here ... bcf intcon,1 ;and clear the interrupt flag retfie timer_int place timer overflow ISR goes here ... bcf intcon,2 ;and clear the interrupt flag retfie Moving to Multiple Interrupts – Identifying the Source 17
  • 18. Critical Regions and Masking In certain program parts we will not want to accept the intrusion of an interrupt under any circumstances, with or without context saving. We call these critical regions. We can disable, or mask, the interrupts for their duration, by manipulating the enable bits in the INTCON register. Critical regions may include: 1. times when the microcontroller is simply not ready to act on the interrupt (for example during initialization – hence only enable interrupts after initialization is complete); 2. time-sensitive activity, including timing loops and multi-instruction setting of outputs; 3. any calculation made up of a series of instructions where the ISR makes use of the result. 18
  • 19. The purpose of the interrupt is to attract the attention of the CPU quickly, but actually how quickly does this happen? The time between the interrupt occurring and the CPU responding to it is called the latency. This depends on certain aspects of hardware and on the characteristics of the program running. This timing diagram shows how the mid-range PIC family responds to an enabled external interrupt. Taking Things Further: Interrupt Latency 19
  • 20. The Digital Counter Reviewed • It is easy to make a digital counter using flip-flops. • Counters can be made which count up, count down, which can be cleared back to zero, pre-loaded to a certain value, and which by the provision of an overflow output can be cascaded with other counters. A simple example is shown: 21
  • 21. The Digital Counter Reviewed 22
  • 22. The Digital Counter Reviewed 23
  • 23. The Digital Counter Reviewed 24 Input Q0 Q1
  • 24. The Digital Counter Reviewed 25
  • 25. The Counter as Timer • If the incoming clock pulses are regular in frequency, the counter can also measure time. • In this example time is being measured between two pulses. Clock Pulse Input T 1 2 3 4 5 n Tc 26
  • 26. Clock Pulse Input T 1 2 3 4 5 n Tc The Counter as Timer If TC is clock period, and n cycles are counted, then the period during which counting has taken place is nTC . Example: clock frequency is 1 MHz, clock period is therefore 1us, before overflow counter can count: 8-bit 255us 16-bit 65535us = 65.5ms 24-bit 16.78 secs 32-bit 4,295 secs = 1hr, 11minutes 27
  • 27. Timer Prescalar 28 A prescaler is an electronic counting circuit used to reduce a high frequency electrical signal to a lower frequency by integer division.
  • 32.
  • 33. The 16F84A OPTION Register 34
  • 34. The 16F84A OPTION Register 35
  • 36. 37