May 15, 2021 Arduino
Interrupt stops Arduino's current work so that some other work can be done.
Suppose you're sitting at home talking to someone. /b10> Suddenly the telephone rang. /b11>You stop chatting, pick up the phone and talk to the caller. /b12> When you're done talking on the phone, you go back and talk to the person before the phone rings.
Similarly, you can think of the main program as chatting with someone, and the ringtone stops you from chatting. /b10> An interrupt program is the process of talking on a phone. /b11> When the call is over, you go back to the main program you chatted with. T his example accurately explains how interrupts enable the processor to perform operations.
The main program runs in the circuit and performs some functions. /b10> However, when an interrupt occurs, the main program stops when another program executes. /b11> When the program ends, the processor returns to the main program again.
Here are some important features about interrupts:
Interruptions can come from a variety of sources. /b10> In this case, we are using a hardware interrupt triggered by a state change on the digital pin.
Most Arduino designs have two hardware interrupts (called "interrupt0" and "interrupt1") that are hard-connected to digital I/O pins 2 and 3, respectively.
The Arduino Mega has six hardware interrupts, including additional interrupts on pins 21, 20, 19, and 18 ("interrupt2" to "interrupt5").
You can define a program using a special function called a Interrupt Service Program, commonly known as an ISR.
You can define the program and specify the conditions for the rising, falling, or both. /b10> Under these specific conditions, interrupts are processed.
The function can be executed automatically each time an event occurs on the input pin.
There are two types of interrupts:
Hardware interrupts - They occur in response to external events, such as external interrupt pins becoming high or low.
Software outages - They occur in response to instructions sent in the software. /b10> The only type of interrupt supported by the Arduino language is the atachInterrupt() function.
Interrupts are useful in Arduino programs because they help resolve timing issues. /b10> A good application for interrupts is to read the rotary encoder or observe user input. I n general, ISRs should be as short and fast as possible. /b12> If your sketch uses more than one ISR, you can only run one at a time. /b13> Other interrupts will be executed after the current completion, depending on their priority.
Typically, global variables are used to pass data between the ISR and the main program. T o ensure that variables shared between the ISR and the main program are updated correctly, declare them volatile.
In Arduino, there are mainly clock interrupts and external interrupts, which in this article refer to external interrupts. E xternal interrupts in Arduino are usually triggered by a change in the level of the Pin port (digital Pin, not analog port). The Arduino version of each model has several Pins that can be used to register interrupts, as follows:
Development board | Can be used to register a broken Pin port |
---|---|
Uno, Nano, Mini, other 328-based | 2, 3 |
Uno WiFi Rev.2 | All numeric mouths |
Mega, Mega2560, MegaADK | 2, 3, 18, 19, 20, 21 |
Micro, Leonardo, other 32u4-based | 0, 1, 2, 3, 7 |
Zero | All numeric mouths except port 4 |
MKR Family boards | 0, 1, 4, 5, 6, 7, 8, 9, A1, A2 |
Due | All numeric mouths |
101 |
All digital port (only 2, 5, 7, 8, 10, 11, 12, 13 digital port can
CHANGE
interrupt, interrupt type is described below)
|
Registration interruptions are
attachInterrupt()
function, which is prototyped as:
void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode);
Development board | Interruption number 0 | Interruption Number 1 | Interruption Number 2 | Interruption Number 3 | Interruption Number 4 | Interruption number 5 |
Uno, Ethernet | PIN 2 | PIN 3 | ||||
Mega2560 | PIN 2 | PIN 3 | PIN 21 | PIN 20 | PIN 19 | PIN 18 |
32u4-based development boards such as Leonardo, Micro | PIN 3 | PIN 2 | PIN 0 | PIN 1 | PIN 7 |
attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);
This way to register the interrupt number.
Example
int pin = 2; //define interrupt pin to 2 volatile int state = LOW; // To make sure variables shared between an ISR //the main program are updated correctly,declare them as volatile. void setup() { pinMode(13, OUTPUT); //set pin 13 as output attachInterrupt(digitalPinToInterrupt(pin), blink, CHANGE); //interrupt at pin 2 blink ISR when pin to change the value } void loop() { digitalWrite(13, state); //pin 13 equal the state value } void blink() { //ISR function state = !state; //toggle the state when the interrupt occurs }
attachInterrupt statement syntax
attachInterrupt(digitalPinToInterrupt(pin),ISR,mode);//recommended for arduino board attachInterrupt(pin, ISR, mode) ; //recommended Arduino Due, Zero only //argument pin: the pin number //argument 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. //argument mode: defines when the interrupt should be triggered.