|
1 |
| -// still working on this file |
| 1 | +:source-highlighter: pygments |
| 2 | +:pygments-style: arduino |
| 3 | +:ext-relative: adoc |
| 4 | + |
| 5 | + |
| 6 | += attachInterrupt() |
| 7 | + |
| 8 | + |
| 9 | +// OVERVIEW SECTION STARTS |
| 10 | +[#overview] |
| 11 | +-- |
| 12 | + |
| 13 | +[float] |
| 14 | +=== Description |
| 15 | +Specifies a named Interrupt Service Routine (ISR) to call when an interrupt occurs. Replaces any previous function that was attached to the interrupt. Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3). The table below shows the available interrupt pins on various boards. |
| 16 | + |
| 17 | +Board int.0 int.1 int.2 int.3 int.4 int.5 |
| 18 | + |
| 19 | +Uno, Ethernet 2 3 |
| 20 | + |
| 21 | +Mega2560 2 3 21 20 19 18 |
| 22 | + |
| 23 | +Leonardo 3 2 0 1 7 |
| 24 | + |
| 25 | +Due (see below) |
| 26 | + |
| 27 | +The Arduino Due board has powerful interrupt capabilities that allows you to attach an interrupt function on all available pins. You can directly specify the pin number in `attachInterrupt()`. |
| 28 | +[%hardbreaks] |
| 29 | + |
| 30 | +=== Notes and Warnings |
| 31 | +Inside the attached function, `delay()` won't work and the value returned by `millis()` will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function. See the section on ISRs below for more information. |
| 32 | +[%hardbreaks] |
| 33 | + |
| 34 | +[float] |
| 35 | +== Using Interrupts |
| 36 | + |
| 37 | +Interrupts are useful for making things happen automatically in microcontroller programs, and can help solve timing problems. Good tasks for using an interrupt may include reading a rotary encoder, or monitoring user input. |
| 38 | + |
| 39 | +If you wanted to insure that a program always caught the pulses from a rotary encoder, so that it never misses a pulse, it would make it very tricky to write a program to do anything else, because the program would need to constantly poll the sensor lines for the encoder, in order to catch pulses when they occurred. Other sensors have a similar interface dynamic too, such as trying to read a sound sensor that is trying to catch a click, or an infrared slot sensor (photo-interrupter) trying to catch a coin drop. In all of these situations, using an interrupt can free the microcontroller to get some other work done while not missing the input. |
| 40 | + |
| 41 | +[float] |
| 42 | +== About Interrupt Service Routines |
| 43 | + |
| 44 | +ISRs are special kinds of functions that have some unique limitations most other functions do not have. An ISR cannot have any parameters, and they shouldn't return anything. |
| 45 | + |
| 46 | +Generally, an ISR should 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. as delay() and millis() both rely on interrupts, they will not work while an ISR is running. `delayMicroseconds()`, which does not rely on interrupts, will work as expected. |
| 47 | + |
| 48 | +Typically global variables are used to pass data between an ISR and the main program. To make sure variables used in an ISR are updated correctly, declare them as volatile. |
| 49 | + |
| 50 | +For more information on interrupts, see http://gammon.com.au/interrupts[Nick Gammon's notes]. |
| 51 | + |
| 52 | +[float] |
| 53 | +=== Syntax |
| 54 | +`attachInterrupt(interrupt, ISR, mode)` + |
| 55 | +`attachInterrupt(pin, ISR, mode)` _(Arduino Due only)_ |
| 56 | + |
| 57 | + |
| 58 | +[float] |
| 59 | +=== Parameters |
| 60 | +`interrupt`: the number of the interrupt (`int`) |
| 61 | +`pin`: the pin number _(Arduino Due only)_ |
| 62 | +`ISR`: the ISR to call when the interrupt occurs; this function must take no parameters and return nothing. This function is sometimes referred to as an interrupt service routine. |
| 63 | +`mode`: defines when the interrupt should be triggered. Four contstants are predefined as valid values: |
| 64 | + |
| 65 | +* *LOW* to trigger the interrupt whenever the pin is low, + |
| 66 | +* *CHANGE* to trigger the interrupt whenever the pin changes value + |
| 67 | +* *RISING* to trigger when the pin goes from low to high, + |
| 68 | +* *FALLING* for when the pin goes from high to low. + |
| 69 | + The Due board allows also: |
| 70 | +* *HIGH* to trigger the interrupt whenever the pin is high. _(Arduino Due only)_ |
| 71 | + |
| 72 | +[float] |
| 73 | +=== Returns |
| 74 | +Nothing |
| 75 | + |
| 76 | +-- |
| 77 | +// OVERVIEW SECTION ENDS |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +// HOW TO USE SECTION STARTS |
| 83 | +[#howtouse] |
| 84 | +-- |
| 85 | + |
| 86 | +[float] |
| 87 | +=== Example Code |
| 88 | +// Describe what the example code is all about and add relevant code ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄ |
| 89 | + |
| 90 | + |
| 91 | +[source,arduino] |
| 92 | +---- |
| 93 | +int pin = 13; |
| 94 | +volatile int state = LOW; |
| 95 | +
|
| 96 | +void setup() |
| 97 | +{ |
| 98 | + pinMode(pin, OUTPUT); |
| 99 | + attachInterrupt(0, blink, CHANGE); |
| 100 | +} |
| 101 | +
|
| 102 | +void loop() |
| 103 | +{ |
| 104 | + digitalWrite(pin, state); |
| 105 | +} |
| 106 | +
|
| 107 | +void blink() |
| 108 | +{ |
| 109 | + state = !state; |
| 110 | +} |
| 111 | +---- |
| 112 | +[%hardbreaks] |
| 113 | + |
| 114 | +[float] |
| 115 | + |
| 116 | + |
| 117 | +[float] |
| 118 | +=== See also |
| 119 | +// Link relevant content by category, such as other Reference terms (please add the tag #LANGUAGE#), |
| 120 | +// definitions (please add the tag #DEFINITION#), and examples of Projects and Tutorials |
| 121 | +// (please add the tag #EXAMPLE#) ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄ |
| 122 | +[role="language"] |
| 123 | +* #LANGUAGE# link:detachInterrupt{ext-relative}[detachInterrupt] |
| 124 | + |
| 125 | + |
| 126 | +-- |
| 127 | +// HOW TO USE SECTION ENDS |
0 commit comments