What is Interrupt ?
As in simple language is any continues process is going on and some this come in between to distract is called interrupt. This same things also come in microcontroller world. So right here we are talking about interrupt with microcontroller using Arduino IDE and Arduino UNO board.
So in microcontroller world, Basically interrupt is an event which come between continues system flow. as example
Normally microcontroller execute instructions one after another as you define in main when interrupt occur , MCU holds execution of instruction in your main program, save its current state so it can reload it later and then procced to interrupt service routine appropriate for that interrupt occurred. when entire interrupt routine executed, it then exits the ISR (Interrupt Service Routine) ,Load back to the state of the main program it saved earlier and the continues the execution of main program
How it works?
Interrupt is a mechanism through which the processor is informed to stop the current execution of instructions, execute a special event or process and then return back to the original execution.
The main advantage of Interrupts in Arduino (or any microcontroller or processor on that note) is that the processor doesn’t have to continuously poll with the devices whether they need any attention. The device itself “Interrupts” the processor whenever a service is required until which, the processor can perform some other tasks.
If you take the button example once again, originally, the controller was continuously checking for the status of the button. But introducing the concept of Interrupts into this situation, the controller can do whatever it wants (I mean anything other than checking for the status of the button, as per your code) and whenever the button is pressed, it will automatically stop the current execution and puts its full attention to the button press event.
Upon interrupt, I said that the processor executes a special process and this special process is nothing but a set of instructions called Interrupt Service Routine. The structure of an Interrupt Service Routine or ISR is similar to that of any user defined void function.
Types of Interrupts
Hardware Interrupts − They occur in response to an external event, such as an external interrupt pin going high or low.
Interrupts in Arduino
Arduino has three different sources of Interrupts:
- Timer Interrupts
- External Interrupts
- Pin-Change Interrupts
Arduino Statement Syntax
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.
The following three 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.
FALLING whenever the pin goes from high to low.
const int ledPin = 13;
const int buttonPin = 2;
int ledToggle = LOW;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(buttonPin), button_ISR, CHANGE);
}
void loop()
{
delay(300);
}
void button_ISR()
{
ledToggle = !ledToggle;
digitalWrite(ledPin, ledToggle);
}
Comments
Post a Comment