Interrupts
Update time:2018-04-13 Views:6080
Operating Environment
Operating system : windows 7 Ultimate 64-bit
IDE : Arduino1.6.6
Interrupts()
Description
Re-enables interrupts (after they've been disabled by noInterrupts()). Interrupts allow certain important tasks to happen in the background and are enabled by default. Some functions will not work while interrupts are disabled, and incoming communication may be ignored. Interrupts can slightly disrupt the timing of code, however, and may be disabled for particularly critical sections of code.
Syntax
Interrupts()
Parameters
None
Returns
None
Example
void setup() {}void loop(){ noInterrupts(); // critical, time-sensitive code here interrupts(); // other code here}
noInterrupts()
Description
Disables interrupts (you can re-enable them with interrupts()). Interrupts allow certain important tasks to happen in the background and are enabled by default. Some functions will not work while interrupts are disabled, and incoming communication may be ignored. Interrupts can slightly disrupt the timing of code, however, and may be disabled for particularly critical sections of code.
Syntax
noInterrupts()
Parameters
None
Returns
None
Example
void setup() {}void loop(){ noInterrupts(); // critical, time-sensitive code here interrupts(); // other code here}
attachInterrupt()
Description
The first parameter to attachInterrupt is an interrupt number. Normally you should use digitalPinToInterrupt(pin) to translate the actual digital pin to the specific interrupt number. For example, if you connect to pin 3, use digitalPinToInterrupt(3) as the first parameter to attachInterrupt.
nterrupts 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.
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 executed after the current one finishes in an order that depends on the priority they have. millis() relies on interrupts to count, so it will never increment inside an ISR. Since delay() requires interrupts to work, it will not work if called inside an ISR. micros() works initially, but will start behaving erratically after 1-2 ms. delayMicroseconds() does not use any counter, so it will work as normal.
Syntax
attachInterrupt(pin, function, mode)
Parameter
pin: the pin number (Arduino Due, Zero, MKR1000 only) function : 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. Four constants are predefined as valid values: LOW to trigger the interrupt whenever the pin is low, 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.
Returns
None
Note
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.
Example
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;}
detachInterrupt()
Description
Turns off the given interrupt.
Syntax
detachInterrupt(interrupt)
Parameters
interrupt:the pin number of the interrupt to disable (Arduino Due only)
Returns
None