attachInterrupt()

[External Interrupts]

描述

Specifies a named Interrupt Service Routine (ISR) to call when an interrupt occurs. Replaces 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 :

Boardint.0int.1int.2int.3int.4int.5
Zero424344
One424344181920
EduCake424344181920
PLC0256

pins for int.6~11 :

Boardint.6int.7int.8int.9int.10int.11
Zero
One333435363738
EduCake
PLC

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.

語法

attachInterrupt(interrupt, ISR, mode)

參數

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.

回傳

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;
}

參考


語法參考主頁面

86Duino 參考的文本是根據 Creative Commons Attribution-ShareAlike 3.0 License,部分文本是從 the Arduino reference 修改的。 參考中的代碼示例已發佈到公共領域。

發表評論

上部へスクロール