SlideShare a Scribd company logo
1 of 35
By/Ramadan Ahmed
What’s Interrupts ?
Basically events that require immediate attention by the
microcontroller.
An interrupt is a signal to the processor emitted by
hardware or software indicating an event that needs
immediate attention.
When an interrupt event occurs, the microcontroller pause
its current task and attend to the interrupt by executing
its corresponding Interrupt Service Routine (ISR) then
microcontroller returns to the task it had pause and
continue its normal operations.
Such an event may be:
 An peripheral device has finished the last task it was given
and is now ready for another. For example Timer may
generate interrupt after reaching the maximum possible
value.
 External Interrupt event on I/O Pin.
 An error from a peripheral.
You can think of it as a hardware-generated function
call.
ISR (Interrupt service routine)
 Piece of code that should be execute when an interrupt is triggered
 Should be deterministic and short as possible, as it usually set a flag
to a specific task indicating a certain event is happened, or save
small data in buffer.
REMEMBER
Global interrupt enabled bit MUST be enabled for
interrupts to be activated in the µC.
To Set global interrupt :
 sei();
To clear global interrupt :
 cli();
Interrupt Flags and Enabled bits in AVR µC
 Each interrupt is associated with two (2) bits, an Interrupt
Flag Bit and an Interrupt Enabled Bit. These bits are
located in the I/O registers associated with the specific
interrupt:
The interrupt enabled bit(IE): is used to enable or
disable a specific interrupt. Basically is tells the µC
whether or not it should respond to the interrupt if it is
triggered.
The interrupt flag bit (IF):is set whenever the
interrupt event occur, whether or not the interrupt is
enabled. And The flag is cleared when the ISR is
executed. So it is also called “Automatic Flag”.
Global interrupt
Interrupt flag
Interrupt enabled
Interrupt Request
RESET interrupt
 It active low
interrupt which
means it triggered
when low voltage (0
volt) is applied on
reset pin (number 9).
Timer is special register that can be 8-
16 bit so capable of holding from 0 to
255-65536
What differ it from any register that it
count up / down automatically at
predefined rate and this is Timer
Clock that doesn’t need CPU
intervention.
Timer Clock could be internal /
external
One of basic condition when Timer overflow( it means
timer counted up to its maximum value and return back to
zero).
In this situation it causes interrupt and if global interrupt
mask is enabled and timer interrupt enable so you have to
write ISR to handle the event of the overflow
Resolution:
 is the smallest period taken by timer to take one count we can
calculate it through formula
Resolution = (1/Frequency)
 If you using 1 MHZ for CPU Clock timer takes to count up till it
overflow 256 us
 So if microcontroller is clocked with 1MHz source, then 8 bit timer
will run with resolution:
Resolution=1/1MHz=1µs
In case you want to increase the time needed for specific
operation we use timer prescalar
Prescalar divide the clock frequency and produce timer
clock
Prescalar modes are
 No prescalar (Timer Clock).
 Clock/8.
 Clock/64.
 Clock/256.
 Clock/512.
 Clock/1024.
 NO clock.
Note
CPU Clock doesn’t affected by
reduction using prescalar.
It remain the same
Example for using prescalar :
 If you using 1 MHZ for CPU Clock without using prescalar timer
takes to count up till it overflow 256 µs
 But when you use prescalar 1024 timer takes to count up till it
overflow 0.26 sec
 It’s easy to know the time timer need to overflow if you know
Resolution.
Resolution = (1/Frequency)
 So if microcontroller is clocked with 1MHz source, then 8 bit timer
without prescaler will run with resolution:
Resolution=1/1MHz=1µs
So if timer take 256 counts to overflow then it takes :
T= Resolution * 256 = 1µs *256 = 256 µs
 And when we use 1024 as prescalar:
Resolution = 1024 / 1 MHZ = 1024µs
T = Resolution * 256 = 1024 * 256 = 0.256s
ATmega16/32 have 3 Timer/counter we have three
Timers/Counters each one with Separate Prescaler.
 Timer 0 - 8 bit timer.
 Timer 1 -16 bit timer.
 Timer 2 - 8 bit timer.
Timer also can be operated in 3 modes :
 Normal mode (Overflow).
 Pulse Width Modulation mode(PWM).
 Clear Timer Compare mode(CTC ).
Timer0
 Timer1 is an 16 bit timer/counter which can count from 0 to
0xFF (256).
 In the timer mode this peripheral uses an internal clock
signal.
 In the counter mode an external signal on PIN0 in PORTB.
 The timer can be operated either in the polling mode or in
the interrupt mode.
 Timer/Counter Control Register – TCCR0
 Bit 7 – FOC0: Force Output Compare
 The FOC0 bit is only active when the WGM00 bit specifies a non-
PWM mode.
 Bit 3, 6 – WGM01:0: Waveform Generation Mode
 Bit 5:4 – COM01:00 Compare Match Output Mode
Bit 2:0 – CS02:0: Clock Select
Timer/Counter Register – TCNT0
 This is where the 8-bit counter of the timer resides. The
value of the counter is stored here and it increases
automatically each clock cycle. Data can be both read/written
from this register. The initial value of the counter is set by
writing it.
Output Compare Register – OCR0
 The Output Compare Register contains an 8-bit value that is
continuously compared with the counter value (TCNT0). A match
can be used to generate an output compare interrupt, or to
generate a waveform output on the OC0 pin.
Timer/Counter Interrupt Mask Register –TIMSK
 TOIE0: Timer Overflow Interrupt Enable
 OCIE0: Output Compare Match Interrupt Enable
 Timer/Counter Interrupt Flag Register – TIFR
 TOV0:
 The bit TOV0 is set (one) when an overflow occurs in Timer/Counter0. TOV0 is
cleared by hardware when executing the corresponding interrupt handling
vector. When the SREG I-bit, TOIE0 (Timer/Counter0 Overflow Interrupt
Enable), and TOV0 are set (one), the Timer/Counter0 Overflow interrupt is
executed.
 OCF0
 The OCF0 bit is set (one) when a compare match occurs between the
Timer/Counter0 and the data in OCR0 – Output Compare Register0. OCF0 is
cleared by hardware when executing the corresponding interrupt handling
vector. When the I-bit in SREG, OCIE0 (Timer/Counter0 Compare Match
Interrupt Enable), and OCF0 are set (one), the Timer/Counter0 Compare Match
Interrupt is executed.
Normal Mode (Overflow)
 A timer overflow means that the counter(TCTNx) has counted up
to its maximum value and is reset to zero in the next timer clock
cycle.
 The resolution of the timer determines the maximum value of that
timer.
 The timer overflow event causes the Timer Overflow Flag (TOVx)
to be set in the Timer Interrupt Flag Register (TIFR).
Pulse Width Modulation
 is the technique used to generate analogue signals from a
digital device like a MCU. Almost all modern MCUs have
dedicated hardware for PWM signal generation.
 A digital device, like a microcontroller can only generate two
levels on its output lines, HIGH=5v and LOW=0V.
 But what if we want to generate 2.5v or 3.1v or any voltage
between 0-5 volt output ?
 For these kinds of requirement, instead of generating a
constant DC voltage output we generate a square wave, which
has high = 5V and Low = 0v
 We will use the simplest timer, TIMER0 for PWM generation.
 Pulse Width Modulation
 The period (frequency) depends upon the prescaler settings. Now for PWM
generation from this count sequence we have a OCR0 (Output Compare Register
Zero Register). We can store any value between 0-255 in OCR0, say we store 64 in
OCR0 then it would appear in the graph as follows (the RED line).
 When the TIMER0 is configured for fast PWM mode, while up counting whenever
the value of TIMER0 counter (TCNT0 register) matches OCR0 register an output
PIN (OC0 PIN) is pulled low (0) and when counting sequence begin again from 0
it is SET again (pulled high=VCC).
 Pulse Width Modulation
Note:
to be able to generate
an output from the
PWM in Timer0, the
Output Compare Pin of
Timer0 (OC0) must
be set as output pin.
DDRB |= PB3
Microcontroller Interrupts and Timers Explained in Detail/TITLE

More Related Content

What's hot

Architecture of 8051 microcontroller))
Architecture of 8051 microcontroller))Architecture of 8051 microcontroller))
Architecture of 8051 microcontroller))Ganesh Ram
 
Contamination delay
Contamination delayContamination delay
Contamination delayNima Afraz
 
Reset Metastability Issues.pptx
Reset Metastability Issues.pptxReset Metastability Issues.pptx
Reset Metastability Issues.pptxssuserfb39fe
 
SYBSC IT SEM IV EMBEDDED SYSTEMS UNIT IV Designing Embedded System with 8051...
SYBSC IT SEM IV EMBEDDED SYSTEMS UNIT IV  Designing Embedded System with 8051...SYBSC IT SEM IV EMBEDDED SYSTEMS UNIT IV  Designing Embedded System with 8051...
SYBSC IT SEM IV EMBEDDED SYSTEMS UNIT IV Designing Embedded System with 8051...Arti Parab Academics
 
Serial Peripheral Interface
Serial Peripheral InterfaceSerial Peripheral Interface
Serial Peripheral InterfaceAnurag Tomar
 
Real Time OS For Embedded Systems
Real Time OS For Embedded SystemsReal Time OS For Embedded Systems
Real Time OS For Embedded SystemsHimanshu Ghetia
 
System verilog control flow
System verilog control flowSystem verilog control flow
System verilog control flowPushpa Yakkala
 
System verilog verification building blocks
System verilog verification building blocksSystem verilog verification building blocks
System verilog verification building blocksNirav Desai
 
8051 microcontroller
8051 microcontroller 8051 microcontroller
8051 microcontroller Gaurav Verma
 
8096 microcontrollers notes
8096 microcontrollers notes8096 microcontrollers notes
8096 microcontrollers notesDr.YNM
 
SystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features SummarySystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features SummaryAmal Khailtash
 

What's hot (20)

Architecture of 8051 microcontroller))
Architecture of 8051 microcontroller))Architecture of 8051 microcontroller))
Architecture of 8051 microcontroller))
 
Contamination delay
Contamination delayContamination delay
Contamination delay
 
Reset Metastability Issues.pptx
Reset Metastability Issues.pptxReset Metastability Issues.pptx
Reset Metastability Issues.pptx
 
SYBSC IT SEM IV EMBEDDED SYSTEMS UNIT IV Designing Embedded System with 8051...
SYBSC IT SEM IV EMBEDDED SYSTEMS UNIT IV  Designing Embedded System with 8051...SYBSC IT SEM IV EMBEDDED SYSTEMS UNIT IV  Designing Embedded System with 8051...
SYBSC IT SEM IV EMBEDDED SYSTEMS UNIT IV Designing Embedded System with 8051...
 
UVM TUTORIAL;
UVM TUTORIAL;UVM TUTORIAL;
UVM TUTORIAL;
 
Timers
TimersTimers
Timers
 
Serial Peripheral Interface
Serial Peripheral InterfaceSerial Peripheral Interface
Serial Peripheral Interface
 
Embedded C - Lecture 4
Embedded C - Lecture 4Embedded C - Lecture 4
Embedded C - Lecture 4
 
Real Time OS For Embedded Systems
Real Time OS For Embedded SystemsReal Time OS For Embedded Systems
Real Time OS For Embedded Systems
 
axi protocol
axi protocolaxi protocol
axi protocol
 
AVRTIMER.pptx
AVRTIMER.pptxAVRTIMER.pptx
AVRTIMER.pptx
 
Embedded C - Day 1
Embedded C - Day 1Embedded C - Day 1
Embedded C - Day 1
 
System verilog control flow
System verilog control flowSystem verilog control flow
System verilog control flow
 
System verilog verification building blocks
System verilog verification building blocksSystem verilog verification building blocks
System verilog verification building blocks
 
Interrupts of microprocessor 8085
Interrupts of microprocessor  8085Interrupts of microprocessor  8085
Interrupts of microprocessor 8085
 
Interrupts in 8051
Interrupts in 8051Interrupts in 8051
Interrupts in 8051
 
8051 microcontroller
8051 microcontroller 8051 microcontroller
8051 microcontroller
 
8096 microcontrollers notes
8096 microcontrollers notes8096 microcontrollers notes
8096 microcontrollers notes
 
Avr microcontroller
Avr microcontrollerAvr microcontroller
Avr microcontroller
 
SystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features SummarySystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features Summary
 

Viewers also liked

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 programmingMohamed Ali
 
Timers in Unix/Linux
Timers in Unix/LinuxTimers in Unix/Linux
Timers in Unix/Linuxgeeksrik
 
Developing an avr microcontroller system
Developing an avr microcontroller systemDeveloping an avr microcontroller system
Developing an avr microcontroller systemnugnugmacmac
 
OpenPump: open-source hardware for medical devices
OpenPump: open-source hardware  for medical devicesOpenPump: open-source hardware  for medical devices
OpenPump: open-source hardware for medical devicesSwansea University
 
project report on embedded system
project report on embedded systemproject report on embedded system
project report on embedded systemram avtar
 
Mp3 player project presentation
Mp3 player project presentationMp3 player project presentation
Mp3 player project presentationAntonio Mondragon
 
Persistence of Vision Display
Persistence of Vision DisplayPersistence of Vision Display
Persistence of Vision DisplayUday Wankar
 
Điều khiển động cơ sử dụng atmega16
Điều khiển động cơ sử dụng atmega16Điều khiển động cơ sử dụng atmega16
Điều khiển động cơ sử dụng atmega16Pham Hoang
 
Designing of media player
Designing of media playerDesigning of media player
Designing of media playerNur Islam
 
L12 c-language-programming-of-atmega328 p
L12 c-language-programming-of-atmega328 pL12 c-language-programming-of-atmega328 p
L12 c-language-programming-of-atmega328 prsamurti
 
L15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 pL15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 prsamurti
 
Rfid based toll tax collection system 3 (repaired)
Rfid based toll tax collection system 3 (repaired)Rfid based toll tax collection system 3 (repaired)
Rfid based toll tax collection system 3 (repaired)KrishNa Patel
 

Viewers also liked (20)

Avr timers
Avr timersAvr timers
Avr timers
 
Timer
TimerTimer
Timer
 
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
 
Timers in Unix/Linux
Timers in Unix/LinuxTimers in Unix/Linux
Timers in Unix/Linux
 
Developing an avr microcontroller system
Developing an avr microcontroller systemDeveloping an avr microcontroller system
Developing an avr microcontroller system
 
OpenPump: open-source hardware for medical devices
OpenPump: open-source hardware  for medical devicesOpenPump: open-source hardware  for medical devices
OpenPump: open-source hardware for medical devices
 
digital clock atmega16
digital clock atmega16digital clock atmega16
digital clock atmega16
 
project report on embedded system
project report on embedded systemproject report on embedded system
project report on embedded system
 
JAVA Media Player
JAVA Media PlayerJAVA Media Player
JAVA Media Player
 
Mp3 player project presentation
Mp3 player project presentationMp3 player project presentation
Mp3 player project presentation
 
Persistence of Vision Display
Persistence of Vision DisplayPersistence of Vision Display
Persistence of Vision Display
 
Android mp3 player
Android mp3 playerAndroid mp3 player
Android mp3 player
 
Điều khiển động cơ sử dụng atmega16
Điều khiển động cơ sử dụng atmega16Điều khiển động cơ sử dụng atmega16
Điều khiển động cơ sử dụng atmega16
 
Android Media player
Android Media playerAndroid Media player
Android Media player
 
Designing of media player
Designing of media playerDesigning of media player
Designing of media player
 
L12 c-language-programming-of-atmega328 p
L12 c-language-programming-of-atmega328 pL12 c-language-programming-of-atmega328 p
L12 c-language-programming-of-atmega328 p
 
Arduino introduction
Arduino introductionArduino introduction
Arduino introduction
 
L15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 pL15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 p
 
final report
final reportfinal report
final report
 
Rfid based toll tax collection system 3 (repaired)
Rfid based toll tax collection system 3 (repaired)Rfid based toll tax collection system 3 (repaired)
Rfid based toll tax collection system 3 (repaired)
 

Similar to Microcontroller Interrupts and Timers Explained in Detail/TITLE

timer counter (1).pptx
timer counter (1).pptxtimer counter (1).pptx
timer counter (1).pptxSujalKumar73
 
Unit 3 timer and counter and there application .pptx
Unit 3 timer and counter and there application .pptxUnit 3 timer and counter and there application .pptx
Unit 3 timer and counter and there application .pptxnaveen088888
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Aarav Soni
 
unit 2 lect 6 AND 7 8254.pptx
unit 2 lect 6 AND 7 8254.pptxunit 2 lect 6 AND 7 8254.pptx
unit 2 lect 6 AND 7 8254.pptxDrVikasMahor
 
8051 training an interactive tutorial
8051 training an interactive tutorial8051 training an interactive tutorial
8051 training an interactive tutorialFutura infotech
 
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
 
Best-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbaiBest-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbaiUnmesh Baile
 
Microprocessor & Interfacing (Part-2) By Er. Swapnil V. Kaware
Microprocessor & Interfacing (Part-2) By Er. Swapnil V. KawareMicroprocessor & Interfacing (Part-2) By Er. Swapnil V. Kaware
Microprocessor & Interfacing (Part-2) By Er. Swapnil V. KawareProf. Swapnil V. Kaware
 
MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50
MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50
MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50Ruthvik Vaila
 
Output compare
Output compareOutput compare
Output comparevusonbkhn
 

Similar to Microcontroller Interrupts and Timers Explained in Detail/TITLE (20)

timer counter (1).pptx
timer counter (1).pptxtimer counter (1).pptx
timer counter (1).pptx
 
Unit 3 timer and counter and there application .pptx
Unit 3 timer and counter and there application .pptxUnit 3 timer and counter and there application .pptx
Unit 3 timer and counter and there application .pptx
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)
 
Timers
TimersTimers
Timers
 
unit 2 lect 6 AND 7 8254.pptx
unit 2 lect 6 AND 7 8254.pptxunit 2 lect 6 AND 7 8254.pptx
unit 2 lect 6 AND 7 8254.pptx
 
8051 training an interactive tutorial
8051 training an interactive tutorial8051 training an interactive tutorial
8051 training an interactive tutorial
 
Lpc 1768 timers
Lpc 1768 timersLpc 1768 timers
Lpc 1768 timers
 
Timers in Arduino
Timers in ArduinoTimers in Arduino
Timers in Arduino
 
Microcontroller part 5
Microcontroller part 5Microcontroller part 5
Microcontroller part 5
 
Microcontroller part 3
Microcontroller part 3Microcontroller part 3
Microcontroller part 3
 
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...
 
8051 Timers
8051 Timers8051 Timers
8051 Timers
 
Arduino 101
Arduino 101Arduino 101
Arduino 101
 
Best-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbaiBest-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbai
 
Timers
TimersTimers
Timers
 
Peripheral 8245,16550&8237 dma controller
Peripheral 8245,16550&8237 dma controllerPeripheral 8245,16550&8237 dma controller
Peripheral 8245,16550&8237 dma controller
 
Microprocessor & Interfacing (Part-2) By Er. Swapnil V. Kaware
Microprocessor & Interfacing (Part-2) By Er. Swapnil V. KawareMicroprocessor & Interfacing (Part-2) By Er. Swapnil V. Kaware
Microprocessor & Interfacing (Part-2) By Er. Swapnil V. Kaware
 
Lecture7
Lecture7Lecture7
Lecture7
 
MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50
MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50
MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50
 
Output compare
Output compareOutput compare
Output compare
 

Recently uploaded

What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
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
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
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
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
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
 

Recently uploaded (20)

What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
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
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
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
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
🔝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...
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.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
 

Microcontroller Interrupts and Timers Explained in Detail/TITLE

  • 2.
  • 4.
  • 5. Basically events that require immediate attention by the microcontroller. An interrupt is a signal to the processor emitted by hardware or software indicating an event that needs immediate attention. When an interrupt event occurs, the microcontroller pause its current task and attend to the interrupt by executing its corresponding Interrupt Service Routine (ISR) then microcontroller returns to the task it had pause and continue its normal operations.
  • 6. Such an event may be:  An peripheral device has finished the last task it was given and is now ready for another. For example Timer may generate interrupt after reaching the maximum possible value.  External Interrupt event on I/O Pin.  An error from a peripheral. You can think of it as a hardware-generated function call.
  • 7. ISR (Interrupt service routine)  Piece of code that should be execute when an interrupt is triggered  Should be deterministic and short as possible, as it usually set a flag to a specific task indicating a certain event is happened, or save small data in buffer.
  • 8.
  • 9. REMEMBER Global interrupt enabled bit MUST be enabled for interrupts to be activated in the µC. To Set global interrupt :  sei(); To clear global interrupt :  cli();
  • 10. Interrupt Flags and Enabled bits in AVR µC  Each interrupt is associated with two (2) bits, an Interrupt Flag Bit and an Interrupt Enabled Bit. These bits are located in the I/O registers associated with the specific interrupt: The interrupt enabled bit(IE): is used to enable or disable a specific interrupt. Basically is tells the µC whether or not it should respond to the interrupt if it is triggered. The interrupt flag bit (IF):is set whenever the interrupt event occur, whether or not the interrupt is enabled. And The flag is cleared when the ISR is executed. So it is also called “Automatic Flag”.
  • 11. Global interrupt Interrupt flag Interrupt enabled Interrupt Request
  • 12. RESET interrupt  It active low interrupt which means it triggered when low voltage (0 volt) is applied on reset pin (number 9).
  • 13.
  • 14. Timer is special register that can be 8- 16 bit so capable of holding from 0 to 255-65536 What differ it from any register that it count up / down automatically at predefined rate and this is Timer Clock that doesn’t need CPU intervention. Timer Clock could be internal / external
  • 15.
  • 16. One of basic condition when Timer overflow( it means timer counted up to its maximum value and return back to zero). In this situation it causes interrupt and if global interrupt mask is enabled and timer interrupt enable so you have to write ISR to handle the event of the overflow
  • 17. Resolution:  is the smallest period taken by timer to take one count we can calculate it through formula Resolution = (1/Frequency)  If you using 1 MHZ for CPU Clock timer takes to count up till it overflow 256 us  So if microcontroller is clocked with 1MHz source, then 8 bit timer will run with resolution: Resolution=1/1MHz=1µs
  • 18. In case you want to increase the time needed for specific operation we use timer prescalar Prescalar divide the clock frequency and produce timer clock Prescalar modes are  No prescalar (Timer Clock).  Clock/8.  Clock/64.  Clock/256.  Clock/512.  Clock/1024.  NO clock. Note CPU Clock doesn’t affected by reduction using prescalar. It remain the same
  • 19. Example for using prescalar :  If you using 1 MHZ for CPU Clock without using prescalar timer takes to count up till it overflow 256 µs  But when you use prescalar 1024 timer takes to count up till it overflow 0.26 sec  It’s easy to know the time timer need to overflow if you know Resolution. Resolution = (1/Frequency)  So if microcontroller is clocked with 1MHz source, then 8 bit timer without prescaler will run with resolution: Resolution=1/1MHz=1µs
  • 20. So if timer take 256 counts to overflow then it takes : T= Resolution * 256 = 1µs *256 = 256 µs  And when we use 1024 as prescalar: Resolution = 1024 / 1 MHZ = 1024µs T = Resolution * 256 = 1024 * 256 = 0.256s
  • 21. ATmega16/32 have 3 Timer/counter we have three Timers/Counters each one with Separate Prescaler.  Timer 0 - 8 bit timer.  Timer 1 -16 bit timer.  Timer 2 - 8 bit timer. Timer also can be operated in 3 modes :  Normal mode (Overflow).  Pulse Width Modulation mode(PWM).  Clear Timer Compare mode(CTC ).
  • 22. Timer0  Timer1 is an 16 bit timer/counter which can count from 0 to 0xFF (256).  In the timer mode this peripheral uses an internal clock signal.  In the counter mode an external signal on PIN0 in PORTB.  The timer can be operated either in the polling mode or in the interrupt mode.
  • 23.  Timer/Counter Control Register – TCCR0  Bit 7 – FOC0: Force Output Compare  The FOC0 bit is only active when the WGM00 bit specifies a non- PWM mode.  Bit 3, 6 – WGM01:0: Waveform Generation Mode
  • 24.  Bit 5:4 – COM01:00 Compare Match Output Mode
  • 25. Bit 2:0 – CS02:0: Clock Select
  • 26. Timer/Counter Register – TCNT0  This is where the 8-bit counter of the timer resides. The value of the counter is stored here and it increases automatically each clock cycle. Data can be both read/written from this register. The initial value of the counter is set by writing it.
  • 27. Output Compare Register – OCR0  The Output Compare Register contains an 8-bit value that is continuously compared with the counter value (TCNT0). A match can be used to generate an output compare interrupt, or to generate a waveform output on the OC0 pin.
  • 28. Timer/Counter Interrupt Mask Register –TIMSK  TOIE0: Timer Overflow Interrupt Enable  OCIE0: Output Compare Match Interrupt Enable
  • 29.  Timer/Counter Interrupt Flag Register – TIFR  TOV0:  The bit TOV0 is set (one) when an overflow occurs in Timer/Counter0. TOV0 is cleared by hardware when executing the corresponding interrupt handling vector. When the SREG I-bit, TOIE0 (Timer/Counter0 Overflow Interrupt Enable), and TOV0 are set (one), the Timer/Counter0 Overflow interrupt is executed.  OCF0  The OCF0 bit is set (one) when a compare match occurs between the Timer/Counter0 and the data in OCR0 – Output Compare Register0. OCF0 is cleared by hardware when executing the corresponding interrupt handling vector. When the I-bit in SREG, OCIE0 (Timer/Counter0 Compare Match Interrupt Enable), and OCF0 are set (one), the Timer/Counter0 Compare Match Interrupt is executed.
  • 30. Normal Mode (Overflow)  A timer overflow means that the counter(TCTNx) has counted up to its maximum value and is reset to zero in the next timer clock cycle.  The resolution of the timer determines the maximum value of that timer.  The timer overflow event causes the Timer Overflow Flag (TOVx) to be set in the Timer Interrupt Flag Register (TIFR).
  • 31. Pulse Width Modulation  is the technique used to generate analogue signals from a digital device like a MCU. Almost all modern MCUs have dedicated hardware for PWM signal generation.  A digital device, like a microcontroller can only generate two levels on its output lines, HIGH=5v and LOW=0V.  But what if we want to generate 2.5v or 3.1v or any voltage between 0-5 volt output ?  For these kinds of requirement, instead of generating a constant DC voltage output we generate a square wave, which has high = 5V and Low = 0v  We will use the simplest timer, TIMER0 for PWM generation.
  • 32.
  • 33.  Pulse Width Modulation  The period (frequency) depends upon the prescaler settings. Now for PWM generation from this count sequence we have a OCR0 (Output Compare Register Zero Register). We can store any value between 0-255 in OCR0, say we store 64 in OCR0 then it would appear in the graph as follows (the RED line).  When the TIMER0 is configured for fast PWM mode, while up counting whenever the value of TIMER0 counter (TCNT0 register) matches OCR0 register an output PIN (OC0 PIN) is pulled low (0) and when counting sequence begin again from 0 it is SET again (pulled high=VCC).
  • 34.  Pulse Width Modulation Note: to be able to generate an output from the PWM in Timer0, the Output Compare Pin of Timer0 (OC0) must be set as output pin. DDRB |= PB3

Editor's Notes

  1. What happens when an interrupt occurs in AVR Microcontrollers? 1. The microcontroller completes the execution of the current instruction, clears the I bit(Global interrupt enable bit in the SREG status register) and stores the address of the next instruction that should have been executed (the content of the PC), processor status register, And all the CPU registers are pushed onto the stack. 2. The interrupt vector of the triggered interrupt (ISR start address of this interrupt) is then loaded in the PC(program counter) from the interrupt vector table and the microcontroller starts execution from that point up until reaches the end of the ISR. 3. The address that was stored on the stack in step 1 is reloaded in the PC register, Status register and all the CPU registers are popped from the stack and the I bit is re-enabled. 4. The micro-controller then continue executing the program.