Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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.

2 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.

3 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.

4 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.

5 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

6 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.

7 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)

8 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

9 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.

10 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.

11 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 ticks/sec thus 1.5 seconds corresponds to ticks Set up registers: TCCR1B: set WGM12 (for CTC), CS12, CS10 OCR1A to (OCR1AH = 91, OCR1AL to 141) TIMSK1: set OCIE1A Make ISR function: ISR(TIMER1_COMPA_vect){} Lecture 11

12 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 (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.

13 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

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

15 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.)

16 Example: External Interrupt-Driven LED Blink
Arduino tarafından ISR(INT0_vect) şekline dönüştürülür. Inside the attached function, delay() won't work and the value returned by millis() will not increment.

17 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.

18 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

19 detachInterrupt Function
Interrupts can also be removed by using the detachInterrupt(interrupt_number) method.

20 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().


Download ppt "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."

Similar presentations


Ads by Google