Introduction
In programming, an interrupt is a signal that a hardware or software sends to the processor alerting it of an event that needs immediate attention. An interrupt alerts the processor to a high-priority condition requiring the interruption of the current code the processor is executing. The processor responds by stopping all of its current activities, saving its state, and executing a function called an interrupt handler (or an interrupt service routine, ISR) to deal with the event. This interruption is temporary, and, after the interrupt handler finishes, the processor resumes its normal activities.
evive has 6 external interrupt pins:
- 2 (interrupt 0)
- 3 (interrupt 1)
- 18 (interrupt 5)
- 19 (interrupt 4)
- 20 (interrupt 3)
- 21 (interrupt 2)
These pins can be configured to trigger an interrupt on a LOW level, a rising or falling edge of a PWM signal, or a change in level.
attachInterrupt()
In Arduino IDE environment you can attach interrupts using the attachInterrupt() function. There are two different approaches with which you can initialise an interrupt pin:
attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);
AND
attachInterrupt(interrupt, ISR, mode);
where
- interrupt is the number of the interrupt pin (from 0-5)
- pin is the pin number
- ISR is the function that you call when interrupt occurs (this function do not take parameters, and returns nothing. This function is also called an interrupt service routine)
- mode defines how the interrupt occurs.
There exist several modes, as shown in the table below:
Trigger Type | Trigger Action |
---|---|
LOW | Trigger the interrupt whenever the pin is low |
CHANGE | Trigger the interrupt whenever the pin value changes |
RISING | Trigger when the pin goes from low to high |
FALLING | Trigger when the pin goes from high to low |
detachInterrupt()
When we don’t need any kind of interrupt we can stop it, or override it by using the detachInterrupt() function. There are two ways to use it:
detachInterrupt(interrupt);
AND
detachInterrupt(digitalPinToInterrupt(pin));
Example
This example demonstrates the use of interrupts in evive. For this, you must connect digital pins 2 and 7 using a jumper wire. In this program, we will control the Pin 13 LED using evive’s push button 1, i.e. tactile switch 1, and interrupt pin.
Below is the Arduino IDE sketch: