Interrupt On a very basic level, an interrupt is a signal that interrupts the current processor activity. It may be triggered by an external event (change.

Slides:



Advertisements
Similar presentations
Interrupts, Low Power Modes and Timer A (Chapters 6 & 8)
Advertisements

More fun with Timer/Counters
A look at interrupts What are interrupts and why are they needed.
I/O Unit.
EET 2261 Unit 10 Enhanced Capture Timer  Read Almy, Chapter 20.  Homework #10 and Lab #10 due next week.  Quiz next week.
68HC11 Polling and Interrupts
EET 2261 Unit 9 Interrupts  Read Almy, Chapters 17 – 19.  Homework #9 and Lab #9 due next week.  Quiz next week.
Mark Neil - Microprocessor Course 1 Timers and Interrupts.
Interrupts What is an interrupt? What does an interrupt do to the “flow of control” Interrupts used to overlap computation & I/O – Examples would be console.
A look at interrupts What are interrupts and why are they needed in an embedded system? Equally as important – how are these ideas handled on the Blackfin.
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
A look at interrupts What are interrupts and why are they needed.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
INPUT/OUTPUT ORGANIZATION INTERRUPTS CS147 Summer 2001 Professor: Sin-Min Lee Presented by: Jing Chen.
The 8051 Microcontroller and Embedded Systems
INTERRUPTS PROGRAMMING
Timers and Interrupts Shivendu Bhushan Sonu Agarwal.
Arduino John Marcoux Christopher Lesch Thomas Dodge Unless otherwise noted, all information and pictures came from:
16-Bit Timer/Counter 1 and 3 Counter/Timer 1,3 (TCNT1, TCNT3) are identical in function. Three separate comparison registers exist. Thus, three separate.
1 Timing System Timing System Applications. 2 Timing System components Counting mechanisms Input capture mechanisms Output capture mechanisms.
COMP201 Computer Systems Exceptions and Interrupts.
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
Slides created by: Professor Ian G. Harris Interrupts  Embedded systems often perform some tasks which are infrequent and possibly unpredictable Hang.
ARM Timers.
System Clocks.
1 Computer System Overview Chapter 1. 2 n An Operating System makes the computing power available to users by controlling the hardware n Let us review.
A Few Words From Dilbert
MICROPROCESSOR INPUT/OUTPUT
MCU: Interrupts and Timers Ganesh Pitchiah. What’s an MCU ?
Interrupt.
Microprocessors 1 MCS-51 Interrupts.
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
1 © Unitec New Zealand Interrupt Lecture 6 Date: - 20 Sept, 2011 Embedded Hardware ETEC 6416.
CS-280 Dr. Mark L. Hornick 1 Atmel Timer/Counter System Most microcontrollers include some type of timer system Facilitates real-time monitoring and control.
1 Interrupts, Resets Today: First Hour: Interrupts –Section 5.2 of Huang’s Textbook –In-class Activity #1 Second Hour: More Interrupts Section 5.2 of Huang’s.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
Interrupts  An interrupt is any service request that causes the CPU to stop its current execution stream and to execute an instruction stream that services.
Timers and Interrupts Mark Neil - Microprocessor Course.
Timers and Scheduled Interrupts
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
TIMERS AND INTERRUPTS AVI SINGH KEVIN JOSE PIYUSH AWASTHI.
Embedded Systems Design 1 Lecture Set 8 MCS-51 Interrupts.
Lecture 3 CSE 341 – Microprocessors Lecture 3 Md. Omar Faruqe UB 1228
CS-280 Dr. Mark L. Hornick 1 Sequential Execution Normally, CPU sequentially executes instructions in a program Subroutine calls are synchronous to the.
Embedded Programming and Robotics Lesson 11 Arduino Interrupts 1.
Networked Embedded Systems Pengyu Zhang & Sachin Katti EE107 Spring 2016 Lecture 4 Timers and Interrupts.
Introduction to Exceptions 1 Introduction to Exceptions ARM Advanced RISC Machines.
Timers and Scheduled Interrupts
BM-305 Mikrodenetleyiciler Güz 2016 (8. Sunu)
Outline Introduction to Arduino UNO Programming environment setup GPIO
68HC11 Interrupts & Resets.
Microprocessor Systems Design I
Networked Embedded Systems Sachin Katti
Timer and Interrupts.
BITS EDU. CAMPUS , VARNAMA
Interrupts In 8085 and 8086.
AVR Addressing Modes Subject: Microcontoller & Interfacing
BVM Engineering College Electrical Engineering Department : Microprocessor and Microcontroller Interfacing Interrupts of 8051 Prepared by:
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
Timer/Counter Modified from Dr. Lam Phung’s Slides.
ECE 3430 – Intro to Microcomputer Systems
ATmega103 Timer0 and Interrupts
* * * * * * * 8051 Interrupts Programming.
Interrupts, Tasks and Timers
Wireless Embedded Systems
ECE 3430 – Intro to Microcomputer Systems
Digital INPUTS/OUTPUTS and interrupts
Computer System Overview
Presentation transcript:

Interrupt On a very basic level, an interrupt is a signal that interrupts the current processor activity. It may be triggered by an external event (change in pin state) or an internal event (a timer or a software signal).  Once triggered, an interrupt pauses the current activity and causes the program to execute a different function.  This function is called an Interrupt Handler or an Interrupt Service Routine (ISR). Once the function is completed, the program returns to what it was doing before the interrupt was triggered. Interrupts are asynchronous.  An asynchronous event is something that occurs outside of the regular flow of our program – it can happen at any time, no matter what our code is crunching on at the moment.  This means that rather than manually checking whether your desired event has happened, you can let your AVR do the checking for you. http://www.engblaze.com/we-interrupt-this-program-to-bring-you-a-tutorial-on-arduino-interrupts/

A Real World Example Imagine you are sitting on your couch, enjoying a frosty brew and watching a movie after a long day.  Life is good.  There is only one problem: you are waiting for an incredibly important package to arrive, and you need it as soon as possible.  If you were a normal AVR program or Arduino sketch, you would have to repeatedly stop your movie, get up, and go check the mailbox every 5 minutes to make sure you knew when the package was there. Instead, imagine if the package was sent Fedex or UPS with delivery confirmation.  Now, the delivery man will go to your front door and ring the doorbell as soon as he arrives. That is your interrupt trigger.  Once you get the trigger, you can pause your movie and go deal with the package.  That is your Interrupt Service Routine.  As soon as you are done, you can pick up the film where you left off, with no extra time wasted. That is the power of interrupts. http://www.engblaze.com/we-interrupt-this-program-to-bring-you-a-tutorial-on-arduino-interrupts/

Why are Interrupts Important? The AVR chips used in most Arduinos are not capable of parallel processing, i.e. they can’t do multiple things at once.  Using asynchronous processing via interrupts enables us to maximize the efficiency of our code, so we don’t waste any precious clock cycles on polling loops or waiting for things to occur.  Interrupts are also good for applications that require precise timing, because we know we’ll catch our event the moment it occurs, and won’t accidentally miss anything. http://www.engblaze.com/we-interrupt-this-program-to-bring-you-a-tutorial-on-arduino-interrupts/

Polling vs Interrupt Interrupt Polling instruction k instruction k+1 normal_execution interrupt signal interrupt_ service_ routine runs Interrupt while(1) { get_device_status; if(service_required) service_routine; normal_execution; } Using polling, the CPU must continually check the device’s status. Using interrupt: A device will send an interrupt signal when needed. In response, the CPU will perform an Interrupt Service Routine (ISR), and then resume its normal execution. Compared to polling, interrupt is a more efficient approach for the CPU to handle peripheral devices. E.g. serial port, external switches, timers, PWM and ADC.

Interrupt Execution Sequence 1. A device issues an interrupt 2. CPU finishes the current instruction 3. CPU acknowledges the interrupt 4. CPU saves its states and PC onto stack 5. CPU loads the address of ISR onto PC 6. CPU executes the ISR 7. CPU retrieves its states and PC from stack 8. Normal execution resumes

Timer Interrupts Really handy to have timed action, despite whatever loop() is doing could check for serial input on a regular basis could read analog signal for regular sampling could produce custom signal at specific frequency Idea is to set up timer so when it reaches a specified count, it creates an interrupt and also resets counter to zero so cycle begins a new turn Interrupt Service Routine (ISR) must be as short and fast as possible. If your sketch uses multiple ISRs, only one can run at a time, other interrupts will be ignored (turned off) until the current one is finished. Since delay() and millis() both rely on interrupts, they will not work while an ISR is running.

CAUTION Messing with timer configurations can compromise other timer-based functions like PWM outputs: analogWrite() (diff. pins  diff. timers) delay() (uses timer 0, depends on counter wrap) millis() and micros() (uses timer 0, depends on wrap) Servo library (uses timer 1) tone() (uses timer 2)

TIMSKn (Timer Interrupt MaSK) Register TIMSK1 controls what generates interrupts for Timer 1 TOIE: Timer Overflow Interrupt Enable (When TCNT1 wraps) OCIE1A: Output Compare Match Interrupt Enable for Channel A of Timer 1 (When the TCNT1 reaches OCR1A) OCIE1B: Output Compare Match Interrupt Enable for Channel B of Timer 1 (When the TCNT1 reaches OCR1B) ICIE1: Input Capture Interrupt Enable for Timer 1

Timer Overflow Interrupt Timer overflow means the timer has reached is limit value. When a timer overflow interrupt occurs, the timer overflow bit TOVx will be set in the interrupt flag register TIFRx. When the timer overflow interrupt enable bit TOIEx in the interrupt mask register TIMSKx is set, the timer overflow interrupt service routine ISR(TIMERx_OVF_vect) will be called. http://arduino-info.wikispaces.com/Timers-Arduino

Output Compare Match Interrupt When an output compare match interrupt occurs, the OCFxy flag will be set in the interrupt flag register TIFRx. When the output compare interrupt enable bit OCIExy in the interrupt mask register TIMSKx is set, the output compare match interrupt service routine ISR(TIMERx_COMPy_vect) will be called. When an interrupt occurs, a flag in the interrupt flag register TIFRx is set. This interrupt will be automatically cleared when entering the ISR or by manually clearing the bit in the interrupt flag register. http://arduino-info.wikispaces.com/Timers-Arduino

What Do We Do with this Power? Let’s set up a compare match timer interrupt to change the state of an LED every 1.5 seconds Need TIMER 1 to reach beyond 16 ms prescale by 1024, so frequency is 15625 ticks/sec thus 1.5 seconds corresponds to 23437 ticks Set up registers: TCCR1B: set WGM12 (for CTC), CS12, CS10 OCR1A to 23437 (OCR1AH = 91, OCR1AL to 141) TIMSK1: set OCIE1A Make ISR function: ISR(TIMER1_COMPA_vect){} Lecture 11

Example: Compare Match Interrupt-Driven LED Blink #include <avr/io.h> #include <avr/interrupt.h> const int LED=13; // use on-board LED volatile int state=0; // it is volatile since it will be used in ISR int main() { cli(); // turn off global interrupt enable flag pinMode(LED,OUTPUT); // set up on-board LED for OUTPUT TCCR1B |= (1 << WGM12); // set bit for CTC mode TCCR1B |= (1 << CS12); // set bit 2 of prescaler for 1024x TCCR1B |= (1 << CS10); // set bit 0 of prescaler for 1024x OCR1A = 23437; // set L & H bytes to 23437 (1.5 sec) TIMSK1 |= (1 << OCIE1A); // enable interrupt on OCR1A sei(); // turn on global interrupt enable flag for(;;) } return 0; ISR(TIMER1_COMPA_vect) state++; state %= 2; // toggle state 1 --> 0; 0 --> 1 digitalWrite(LED,state); // export value to pin Inside the ISR, delay() won't work and the value returned by millis() will not increment.

A Custom PWM When time is up: ISR(TIMER1_COMPA_vect) { if (state) OCR1A = 31248; // two seconds for OFF else OCR1A = 15624; // one second for ON state++; state %= 2; digitalWrite(LED,state); } When time is up: if state == 1 (LED ON), set compare register to 2 seconds otherwise (LED OFF), set compare register to 1 second In this way, you can customize a PWM-like signal arbitrarily

Interrupt Vectors in ATMega328P An interrupt with a lower VectorNo has a higher priority. E.g. INT0 has a higher priority then INT1.

External Interrupts On Arduino Uno (ATMega328P), there are 2 external interrupts available. These are located on Digital Pin 2 (0 - INT0) and Digital Pin 3 (1 - INT1) Let us use a pushbutton to trigger an external interrupt on Digital Pin 2 that controls (changes the state of) the on-board LED (Digital Pin 13). (The Pin 2 is supposed to be OFF while the pushbutton is not pressed and ON when it is pressed.) http://www.dave-auld.net/?option=com_content&view=article&id=107:arduino-interrupts&catid=53:arduino-input-output-basics&Itemid=107

Example: External Interrupt-Driven LED Blink Arduino tarafından ISR(INT0_vect) şekline dönüştürülür. http://www.dave-auld.net/?option=com_content&view=article&id=107:arduino-interrupts&catid=53:arduino-input-output-basics&Itemid=107 Inside the attached function, delay() won't work and the value returned by millis() will not increment.

attachInterrupt Function The attachInterrupt function requires 3 parameters. attachInterrupt(param1, param2, param3) These are; param1 = Which interrupt to listen for. This is the Interrupt Number not the Digital Pin number. param2 = Which function to call, this must be a method that takes no parameters and returns no value. param3 = Which condition to watch for. http://www.dave-auld.net/?option=com_content&view=article&id=107:arduino-interrupts&catid=53:arduino-input-output-basics&Itemid=107

attachInterrupt Conditions (Trigger Modes) The Arduino can listen for 4 types of condition changes. These are; LOW = The input is at a LOW state RISING = The input state changes from LOW to HIGH FALLING = the input state changes from HIGH to LOW CHANGE = The input state changes from HIGH to LOW or LOW to HIGH, i.e. changes its state http://www.dave-auld.net/?option=com_content&view=article&id=107:arduino-interrupts&catid=53:arduino-input-output-basics&Itemid=107

detachInterrupt Function Interrupts can also be removed by using the detachInterrupt(interrupt_number) method. http://www.dave-auld.net/?option=com_content&view=article&id=107:arduino-interrupts&catid=53:arduino-input-output-basics&Itemid=107

interrupts and noInterrupts Functions The Arduino also has the ability to temporarily ignore all the interrupts. You may want to do this if you have some sensitive code that must be executed without interruption. In this case you would issue a noInterrupts() call. Once your sensitive code block has completed, interrupts can be restarted by calling interrupts(). http://www.dave-auld.net/?option=com_content&view=article&id=107:arduino-interrupts&catid=53:arduino-input-output-basics&Itemid=107