[External Interrupts]
説明
Specifies a named Interrupt Service Routine (ISR) to call when an interrupt occurs. Replace any previous function that was attached to the interrupt. Most 86Duino boards have three external interrupts: numbers 0 (on digital pin 42), 1 (on digital pin 43), and 2 (on digital pin 44). The following table below shows the available interrupt pins on various 86Duino boards.
Pins for int.0~5 :
Board | int.0 | int.1 | int.2 | int.3 | int.4 | int.5 |
Zero | 42 | 43 | 44 | |||
One | 42 | 43 | 44 | 18 | 19 | 20 |
EduCake | 42 | 43 | 44 | 18 | 19 | 20 |
QEC 7 / QEC 9 / QEC 15 | 2 | 4 | 7 |
Pins for int.6~11 :
Board | int.6 | int.7 | int.8 | int.9 | int.10 | int.11 |
Zero | ||||||
One | 33 | 34 | 35 | 36 | 37 | 38 |
EduCake |
Note
Interrupts should be handled as soon as possible, and so it isn’t a good programming style to use delay()
in the attached function.
Serial data received while in the attached 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.
Using Interrupts
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.
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.
About Interrupt Service Routines (ISRs)
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.
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.
Unlike Arduino, the delay()
と millis()
functions of 86Duino don’t rely on interrupts and therefore will work in ISRs as expected. But since this isn’t a good programming style, you should always avoid calling delay()
in ISRs.
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
.
Syntax
attachInterrupt(interrupt, ISR, mode)
Parameters
interrupt
: the number of the interrupt (int)
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.
mode
: defines when the interrupt should be triggered. Three constants are predefined as valid values:
CHANGE
to trigger the interrupt whenever the pin changes value,RISING
to trigger when the pin goes from low to high,FALLING
for when the pin goes from high to low.HIGH
to trigger the interrupt whenever the pin is high.LOW
to trigger the interrupt whenever the pin is low.
Returns
none
Example Code
int pin = 13; volatile int state = LOW; void setup() { pinMode(pin, OUTPUT); attachInterrupt(0, blink, CHANGE); } void loop() { digitalWrite(pin, state); } void blink() { state = !state; }
See also
- [Language] detachInterrupt()
Language Reference Home
86Duino のリファレンスのテキストは Arduino レファレンス を編集したもので、 Creative Commons Attribution-ShareAlike 3.0 License下でライセンスされています。リファレンス内のコードサンプルはパブリックドメインとして公開されています。