(Dr. Öğr. Üyesi Deniz Dal)

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.
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.
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.
The 8051 Microcontroller and Embedded Systems
INTERRUPTS PROGRAMMING
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.
Embedded Systems 7763B Mt Druitt College of TAFE
COMP201 Computer Systems Exceptions and Interrupts.
Slides created by: Professor Ian G. Harris Interrupts  Embedded systems often perform some tasks which are infrequent and possibly unpredictable Hang.
Timer/counter Chapter 12
A Few Words From Dilbert
MCU: Interrupts and Timers Ganesh Pitchiah. What’s an MCU ?
Microprocessors 1 MCS-51 Interrupts.
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
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.
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
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.
Embedded Systems Design 1 Lecture Set 8 MCS-51 Interrupts.
The AVR microcontroller and embedded systems using assembly and c AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi.
Lecture 3 CSE 341 – Microprocessors Lecture 3 Md. Omar Faruqe UB 1228
Mark Neil - Microprocessor Course 1 Timers and Interrupts.
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.
16-Bit Timer/Counter 1 and 3
Timers and Scheduled Interrupts
EET 2261 Unit 13 Enhanced Capture Timer
BM-305 Mikrodenetleyiciler Güz 2016 (8. Sunu)
Outline Introduction to Arduino UNO Programming environment setup GPIO
Interrupts and signals
68HC11 Interrupts & Resets.
Microprocessor Systems Design I
Networked Embedded Systems Sachin Katti
Mon. Oct 2 Announcements Quiz Postponed to Wednesday – still only on 2.a + 2.b Video lecture for 2.a posted Lab 6 experiment extension You must come to.
Lesson Objectives Aims Key Words Interrupt, Buffer, Priority, Stack
Timer and Interrupts.
Computer Architecture
BITS EDU. CAMPUS , VARNAMA
(6. Sunu) (AVR Assembly Örnekleri)
Interrupts In 8085 and 8086.
Interrupts An interrupt is an exception, a change of the normal progression, or interruption in the normal flow of program execution. An interrupt is essentially.
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.
COMP2121: Microprocessors and Interfacing
Timer/Counter Modified from Dr. Lam Phung’s Slides.
ATmega103 Timer0 and Interrupts
* * * * * * * 8051 Interrupts Programming.
Interrupts An interrupt is an exception, a change of the normal progression, or interruption in the normal flow of program execution. An interrupt is essentially.
Interrupts Interrupt is a process where an external device can get the attention of the microprocessor. The process starts from the I/O device The process.
Timers/Counters.
(Dr. Öğr. Üyesi Deniz Dal)
Interrupt Chapter 10.
Interrupt handling Explain how interrupts are used to obtain processor time and how processing of interrupted jobs may later be resumed, (typical.
Wireless Embedded Systems
Digital INPUTS/OUTPUTS and interrupts
Computer System Overview
COMP3221: Microprocessors and Embedded Systems
Presentation transcript:

(Dr. Öğr. Üyesi Deniz Dal) BM-305 Mikrodenetleyiciler Güz 2018 (8. Sunu) (Dr. Öğr. Üyesi Deniz Dal)

Kesmeler (Interrupts)

Are we there yet? Yoklama (Polling) "Are we there yet?" No… https://www.youtube.com/watch?v=18AzodTPG5U Yoklama (Polling) "Are we there yet?" No… Kesme (Interrupt) "Read a book, I’ll tell you when we are there" http://people.duke.edu/~tkb13/courses/ece550/slides/11-interrupts.pdf

Yoklama (Polling) Sabah 6’da işine gitmek isteyen birinin geceyi uyumadan geçirip, acaba saat 6 oldu mu diye sürekli saatini kontrol etmesi Kesme (Interrupt) Sabah 6’da işine gitmek isteyen birinin saatinin alarmını kurduktan sonra uyuması ve alarm çalınca uyanması

Yoklama (Polling) Bir iş mülakatı için önemli bir telefon bekleyen birinin her saniye telefonun ahizesini kaldırıp acaba aradılar mı diye telefonu kontrol etmesi Kesme (Interrupt) Bir iş mülakatı için önemli bir telefon bekleyen birinin telefonun zili çalıncaya kadar mülakatta çıkabilecek sorulara çalışması veya kahvesini içmesi

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

Kesme Denetleyicisi (Interrupt Controller) https://doc.micrium.com/display/osiiidoc/Handling+CPU+Interrupts

Kesme Denetleyicisi (Interrupt Controller) http://embien.com/blog/wp-content/uploads/interrupt-mechanism.png http://embien.com/blog/interrupt-handling-in-embedded-software/

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 TOIE1: 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 second Need TIMER 1 to reach beyond 16 micro seconds prescale by 1024, so frequency is 15625 ticks/sec Set up registers: DDRB: set Digital Pin 13 TCCR1B: set WGM12 (for CTC) TCCR1B: set CS12, CS10 (for prescale by 1024) OCR1A: set 15625 (OCR1AH = 61, OCR1AL = 9) TIMSK1: set OCIE1A (Enable the Associated Interrupt Source) Set I in SREG (Enable CPU to Serve for IRQs) Make ISR function: ISR(TIMER1_COMPA_vect){}

Zamanlayıcı 1 ile CTC Modunda Kesme Sinyali Üretilmesi İçin… SREG saklayıcısı içerisindeki I isimli global kesme bayrağı aktif hale getirilmeli TIMSK1 saklayıcısı içerisindeki OCIE1A bayrağı aktif hale getirilmeli TIFR1 saklayıcısı içerisindeki OCF1A bayrağının aktif hale gelmesi beklenmeli

Example: Compare Match Interrupt-Driven LED Blink #include <avr/io.h> #include <avr/interrupt.h> // cli and sei functions volatile int state=0; // it is volatile since it will be used in ISR int main() { cli(); // turn off (clear) global interrupt enable flag DDRB |= (1<<5); // 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 = 15625; // set L & H bytes to 15625 (1 sec) TIMSK1 |= (1 << OCIE1A); // enable interrupt on OCR1A sei(); // turn on (set) global interrupt enable flag for(;;) } return 0; ISR(TIMER1_COMPA_vect) state++; state %= 2; PORTB = state ? PORTB|(1<<5) : PORTB&(~(1<<5)); volatile prevents compiler from optimizing code Variables shared between ISR functions and normal functions should be declared volatile. This tells the compiler that such variables might change at any time, and thus the compiler must reload the variable whenever you reference it, rather than relying upon a copy it might have in a processor register. http://stackoverflow.com/questions/4437527/why-do-we-use-volatile-keyword-in-c The reason to disable the interrupts prior to updating the 16 bit registers (e.g. TCNT1) is to ensure that nothing else updates the registers in between the two instructions (updating the low and high bytes). Generally speaking it would be a good idea to disable interrupts prior to writing to the 16 bit registers. 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 = 31250; // two seconds for OFF else OCR1A = 15625; // one second for ON state++; state %= 2; PORTB = state ? PORTB|(1<<5) : PORTB&(~(1<<5)); } 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 By placing a jump instruction at each interrupt's address in the table, we can make the AVR processor jump to our ISR which lies elsewhere. .org INT0addr rjmp int0_handle INT0addr ? An interrupt with a lower VectorNo has a higher priority. E.g. INT0 has a higher priority then INT1.

Timer Interrupt in AVR Assembly ;To do a 16-bit write, the high byte must be written before the low byte. ;For a 16-bit read, the low byte must be read before the high byte. .INCLUDE "m328pdef.inc";Header for ATMEGA328P state:.BYTE 2;Reserve Two Bytes (int Data Type) in Data Segment .DSEG;Data SEGment RJMP setup .org 0x0000;Address of the "RESET" Vector .CSEG;Code SEGment RJMP ISR_TIMER1_COMPA;Address of the "Timer/Counter 1 Compare Match Channel A" Vector (CTC Mode) .org 0x0016 setup: ;STEP 1 - Initialize the "state" Variable to 0 (zero) STS LOW(state),R16 STS HIGH(state),R16 LDI R16,0x00 ;STEP 2 - Initialize Stack Pointer LDI R16,LOW(RAMEND) OUT SPH,R16;Stack Pointer High LDI R16,HIGH(RAMEND) OUT SPL,R16;Stack Pointer Low CLI ;STEP 3 - Turn off (Clear) Global Interrupt Enable Flag (I in SREG) IN R16,DDRB ;STEP 4 - Set PORTB.5 (On-board LED) as OUTPUT ORI R16,(1<<5) OUT DDRB,R16 LDS R16,TCCR1B ;STEP 5 - Set Bit for the CTC Mode ORI R16,(1<<WGM12) ;STEP 6 - Set Timer 1 Prescaler as Fcpu/1024 ORI R16,(1<<CS10) ORI R16,(1<<CS12) STS TCCR1B,R16 ;STEP 7 - Update TCCR1B LDI R16,HIGH(15625);Load 0x3D to R16 ;STEP 8 - Set Low & High Bytes of OCR1A to 15625 (1 sec) STS OCR1AH,R16;STore direct to data Space STS OCR1AL,R16;STore direct to data Space LDI R16,LOW(15625);Load 0x09 to R16 LDS R16,TIMSK1 ;STEP 9 - Enable Interrupt on CTC Mode for Channel A STS TIMSK1,R16 ORI R16,(1<<OCIE1A) SEI ;STEP 10 - Turn on (Set) Global Interrupt Enable Flag (I in SREG) RJMP loop;Relative JuMP loop: LDS R30,LOW(state) ;state++ ISR_TIMER1_COMPA: ADIW ZH:ZL,1;ADd Immediate to Word LDS R31,HIGH(state) LDI R31,0x00 ;state %= 2; ANDI R30,0x01;Check Whether the LSB of Low Byte of "state" is 1 (Odd Number) ;PORTB = state ? PORTB|(1<<5) : PORTB&(~(1<<5)); LDI R16,0x00;LoaD Immeadiate value to R16 ifLabel: BRNE elseLabel;BRanch Not Equal OUT PORTB,R16;Turn LED off LDI R16,0x20;LoaD Immeadiate value to R16 elseLabel: RJMP ifEndLabel;Relative JuMP ifEndLabel: OUT PORTB,R16;Turn led on STS HIGH(state),ZH ;Update "state" Variable STS LOW(state),ZL RETI;RETurn Interrupt

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 Global olarak kesme devreye alınmamış görünüyor??? 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 External 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

External Interrupt in AVR C #include <avr/io.h> #include <avr/interrupt.h> int main() { //STEP 1 - Set Digital Pin 13 as Output DDRB |= (1<<5); //STEP 2 - Turn off the LED connected to Digital Pin 13 PORTB &= ~(1<<5); //STEP 3 - Set Digital Pin 2 as Input DDRD &= ~(1<<2); //STEP 4 - Set INT0 to be Triggered on the RISING Edge in Digital Pin 2 EICRA |= (1 << ISC00); EICRA |= (1 << ISC01); //STEP 5 - Enable INT0 Interrupt EIMSK |= (1 << INT0); //STEP 6 - Turn on (Set) Global Interrupt Enable Flag (I in SREG) sei(); while(1) } ISR (INT0_vect) PORTB ^= (1<<5);//Toggle the Output

External Interrupt in AVR Assembly .INCLUDE "m328pdef.inc";Header for ATMEGA328P .org 0x0000;Address of the "RESET" Vector RJMP setup .org 0x0002 RJMP ISR_INT0;Address of the "INT0" Vector setup: ;STEP 1 - Initialize Stack Pointer LDI R16,HIGH(RAMEND) LDI R16,LOW(RAMEND) OUT SPH,R16;Stack Pointer High OUT SPL,R16;Stack Pointer Low ;STEP 2 - Set Digital Pin 13 as Output ORI R16,(1<<5) IN R16,DDRB OUT DDRB,R16 ;STEP 3 - Turn off the LED connected to Digital Pin 13 ANDI R16,~(1<<5) IN R16,PORTB OUT PORTB,R16 ;STEP 4 - Set Digital Pin 2 as Input ANDI R16,~(1<<2) IN R16,DDRD OUT DDRD,R16 ;STEP 5 - Set INT0 to be Triggered on the RISING Edge in Digital Pin 2 ORI R16,(1<<ISC00) LDS R16,EICRA ORI R16,(1<<ISC01) STS EICRA,R16 IN R16,EIMSK ;STEP 6 - Enable INT0 Interrupt ORI R16,(1<<INT0) OUT EIMSK,R16 ;STEP 7 - Turn on (Set) Global Interrupt Enable Flag (I in SREG) SEI loop: RJMP loop;Relative JuMP ISR_INT0: ;Toggle the Output LDI R17,(1<<5) EOR R16,R17 RETI;RETurn Interrupt