11.07.2015 Views

Interrupts in Arduino - Nuno Alves

Interrupts in Arduino - Nuno Alves

Interrupts in Arduino - Nuno Alves

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Interrupts</strong> <strong>in</strong> Ardu<strong>in</strong>oReference: Ardu<strong>in</strong>oCookbook” (1st ed.)by Michael Margolis.CPE 355 - Real Time Embedded Kernels - Spr<strong>in</strong>g ‘12<strong>Nuno</strong> <strong>Alves</strong> (nalves@wne.edu), College of Eng<strong>in</strong>eer<strong>in</strong>g


Sample Code• You have an IR detectorconnected to p<strong>in</strong> 2.• This program monitors pulseson p<strong>in</strong> 2 and stores the durationof each pulse <strong>in</strong> an array.• When the array has been filledeach duration is displayed on theSerial Monitor.CPE 355 - Real Time Embedded Kernels - Spr<strong>in</strong>g ‘12<strong>Nuno</strong> <strong>Alves</strong> (nalves@wne.edu), College of Eng<strong>in</strong>eer<strong>in</strong>g


VolatileKeyword• Its a variable qualifier; it isused before the datatype of avariable, to modify the way <strong>in</strong>which the compiler andsubsequent program treats thevariable.• Compiler will load the variablefrom RAM and not from a storageregister.• Under certa<strong>in</strong> conditions, suchas through <strong>in</strong>terrupts, the valuefor a variable stored <strong>in</strong> registerscan be <strong>in</strong>accurate.CPE 355 - Real Time Embedded Kernels - Spr<strong>in</strong>g ‘12<strong>Nuno</strong> <strong>Alves</strong> (nalves@wne.edu), College of Eng<strong>in</strong>eer<strong>in</strong>g


attachInterruptfunction•The attachInterrupt(0, analyze,CHANGE); call enables theprogram to handle <strong>in</strong>terrupts.• The first number <strong>in</strong> the callspecifies which <strong>in</strong>terrupt to<strong>in</strong>itialize.• On a standard Ardu<strong>in</strong>o board(such as UNO), two <strong>in</strong>terrupts areavailable: number 0, which uses p<strong>in</strong>2, and number 1 on p<strong>in</strong> 3.• Interrupt 0 and <strong>in</strong>terrupt 1 havethe same priorities (with Wir<strong>in</strong>g).CPE 355 - Real Time Embedded Kernels - Spr<strong>in</strong>g ‘12<strong>Nuno</strong> <strong>Alves</strong> (nalves@wne.edu), College of Eng<strong>in</strong>eer<strong>in</strong>g


A lot more<strong>in</strong>terrupts• Yes, there are a lot of other<strong>in</strong>terrupts, but they are <strong>in</strong>ternal.• We will user the timer<strong>in</strong>terrupts very soon.• Unsurpris<strong>in</strong>gly the RESET<strong>in</strong>terrupt has the highestpriority.• This table is from Russell’sbook (listed on syllabus).CPE 355 - Real Time Embedded Kernels - Spr<strong>in</strong>g ‘12<strong>Nuno</strong> <strong>Alves</strong> (nalves@wne.edu), College of Eng<strong>in</strong>eer<strong>in</strong>g


attachInterruptfunction•The second parameter specifieswhat function to call (<strong>in</strong>terrupthandler) when the <strong>in</strong>terruptevent happens.CPE 355 - Real Time Embedded Kernels - Spr<strong>in</strong>g ‘12<strong>Nuno</strong> <strong>Alves</strong> (nalves@wne.edu), College of Eng<strong>in</strong>eer<strong>in</strong>g• The f<strong>in</strong>al parameter specifieswhat should trigger the <strong>in</strong>terrupt:- CHANGE: whenever the p<strong>in</strong>level changes (low to high orhigh to low).- LOW: when the p<strong>in</strong> is low.- RISING: when the p<strong>in</strong> goesfrom low to high.- FALLING: when the p<strong>in</strong> goesfrom high to low.


Ma<strong>in</strong> loop• The ma<strong>in</strong> loop just checks the<strong>in</strong>dex variable to see if all theentries have been set by the<strong>in</strong>terrupt handler.•... And it will pr<strong>in</strong>t the contents ofthe array results only once.• Noth<strong>in</strong>g <strong>in</strong> loop changes thevalue of <strong>in</strong>dex. The <strong>in</strong>dex ischanged <strong>in</strong>side the analyzefunction when the <strong>in</strong>terruptcondition occurs.CPE 355 - Real Time Embedded Kernels - Spr<strong>in</strong>g ‘12<strong>Nuno</strong> <strong>Alves</strong> (nalves@wne.edu), College of Eng<strong>in</strong>eer<strong>in</strong>g


micros()function• The micros() function returnsthe number of micro-secondss<strong>in</strong>ce the Ardu<strong>in</strong>o began runn<strong>in</strong>gthe current program.•This number will overflow afterThis number will overflow (goback to zero), after approximately70 m<strong>in</strong>utes.CPE 355 - Real Time Embedded Kernels - Spr<strong>in</strong>g ‘12<strong>Nuno</strong> <strong>Alves</strong> (nalves@wne.edu), College of Eng<strong>in</strong>eer<strong>in</strong>g• On 16 MHz Ardu<strong>in</strong>o boards(e.g. UNO), this function has aresolution of four microseconds(i.e. the value returned is always amultiple of four).


Chang<strong>in</strong>gvariables• The variables that are changed <strong>in</strong>an <strong>in</strong>terrupt function are declaredas volatile.• This lets the compiler know thatthe values could change at anytime (by an <strong>in</strong>terrupt handler).• Without us<strong>in</strong>g the volatilekeyword, the compiler wouldth<strong>in</strong>k these variables are not be<strong>in</strong>gchanged by any code gett<strong>in</strong>g calledand would replace these variableswith constant values.CPE 355 - Real Time Embedded Kernels - Spr<strong>in</strong>g ‘12<strong>Nuno</strong> <strong>Alves</strong> (nalves@wne.edu), College of Eng<strong>in</strong>eer<strong>in</strong>g


What’s thecode do<strong>in</strong>g?• Each time an <strong>in</strong>terrupt istriggered, <strong>in</strong>dex is <strong>in</strong>crementedand the current time is saved.• The time difference is calculatedand saved <strong>in</strong> the array (except forthe first time the <strong>in</strong>terrupt istriggered, when <strong>in</strong>dex is 0).• When the maximum number ofentries has occurred, the <strong>in</strong>nerblock <strong>in</strong> loop runs, and it pr<strong>in</strong>tsout all the values to the serialport.CPE 355 - Real Time Embedded Kernels - Spr<strong>in</strong>g ‘12<strong>Nuno</strong> <strong>Alves</strong> (nalves@wne.edu), College of Eng<strong>in</strong>eer<strong>in</strong>g


Another sample code<strong>in</strong>t p<strong>in</strong> = 13;volatile <strong>in</strong>t state = LOW;void setup(){p<strong>in</strong>Mode(p<strong>in</strong>, OUTPUT);attachInterrupt(0, bl<strong>in</strong>k, CHANGE);}void loop(){digitalWrite(p<strong>in</strong>, state);}void bl<strong>in</strong>k(){state = !state;}• Attach someth<strong>in</strong>g that willtrigger an <strong>in</strong>terrupt <strong>in</strong> digitalp<strong>in</strong> #2 (e.g. resistive sensor,button, ...)• Make sure its either pulled-upor pull-down, as shown on theright.• Attach a LED to digital p<strong>in</strong> 13. Whenever,the <strong>in</strong>terrupt is triggered (through p<strong>in</strong> #2changes), the LED will change state.CPE 355 - Real Time Embedded Kernels - Spr<strong>in</strong>g ‘12<strong>Nuno</strong> <strong>Alves</strong> (nalves@wne.edu), College of Eng<strong>in</strong>eer<strong>in</strong>g


Ardu<strong>in</strong>o wir<strong>in</strong>g C limitations<strong>in</strong>t p<strong>in</strong> = 13;volatile <strong>in</strong>t state = LOW;void setup(){p<strong>in</strong>Mode(p<strong>in</strong>, OUTPUT);attachInterrupt(0, bl<strong>in</strong>k, CHANGE);}void loop(){digitalWrite(p<strong>in</strong>, state);}void bl<strong>in</strong>k(){state = !state;}• Inside the <strong>in</strong>terrupt function, delay() won'twork• Values returned by the millis() and micros()functions will not <strong>in</strong>crement.•Serial communications while <strong>in</strong> thefunction may be lost!• You should declare as volatile any variablesthat you modify with<strong>in</strong> the attachedfunction.• By default, <strong>in</strong>terrupts are atomic.CPE 355 - Real Time Embedded Kernels - Spr<strong>in</strong>g ‘12<strong>Nuno</strong> <strong>Alves</strong> (nalves@wne.edu), College of Eng<strong>in</strong>eer<strong>in</strong>g


Atomic sections <strong>in</strong> Ardu<strong>in</strong>o• We can enable/disable <strong>in</strong>terrupts on certa<strong>in</strong>sections of code with <strong>in</strong>terrupts() andno<strong>Interrupts</strong>().• <strong>Interrupts</strong> <strong>in</strong> Ardu<strong>in</strong>o are enabled by default.• Some functions will not work while<strong>in</strong>terrupts are disabled, and <strong>in</strong>com<strong>in</strong>gcommunication may be ignored. <strong>Interrupts</strong>can slightly disrupt the tim<strong>in</strong>g of code,however, and may be disabled for particularlycritical sections of code.CPE 355 - Real Time Embedded Kernels - Spr<strong>in</strong>g ‘12<strong>Nuno</strong> <strong>Alves</strong> (nalves@wne.edu), College of Eng<strong>in</strong>eer<strong>in</strong>g


<strong>Interrupts</strong> default• By default you can not have <strong>in</strong>terrupts <strong>in</strong>side <strong>in</strong>terrupts.• With C-Wir<strong>in</strong>g both <strong>in</strong>terrupt 0 and <strong>in</strong>terrupt 1 are assigned the samepriority.• If you have a way to have <strong>in</strong>terrupts <strong>in</strong>side <strong>in</strong>terrupts, then when an<strong>in</strong>terrupt is issued, you immediately leave the current <strong>in</strong>terrupt andexecute the new <strong>in</strong>terrupt.• Enabl<strong>in</strong>g <strong>in</strong>terrupts <strong>in</strong>side <strong>in</strong>terrupts is a “hack” : Is not recommended asit raises all sorts of issues with preserv<strong>in</strong>g the state of the mach<strong>in</strong>e beforethe <strong>in</strong>terrupt<strong>in</strong>g <strong>in</strong>terrupt is serviced.CPE 355 - Real Time Embedded Kernels - Spr<strong>in</strong>g ‘12<strong>Nuno</strong> <strong>Alves</strong> (nalves@wne.edu), College of Eng<strong>in</strong>eer<strong>in</strong>g


Enabl<strong>in</strong>g <strong>in</strong>terrupts <strong>in</strong>side <strong>in</strong>terruptsvolatile <strong>in</strong>t i,j,z;void artificialdelay(){for (i=0; i


Operation scenariosCPE 355 - Real Time Embedded Kernels - Spr<strong>in</strong>g ‘12<strong>Nuno</strong> <strong>Alves</strong> (nalves@wne.edu), College of Eng<strong>in</strong>eer<strong>in</strong>g


What happens if you trigger<strong>in</strong>terrupt0 when <strong>in</strong>side <strong>in</strong>terrupt0 ?volatile <strong>in</strong>t i,j,z;void artificialdelay(){for (i=0; i


What happens if you trigger<strong>in</strong>terrupt1 when <strong>in</strong>side <strong>in</strong>terrupt0 ?volatile <strong>in</strong>t i,j,z;void artificialdelay(){for (i=0; i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!