SkillAgentSearch skills...

EnableInterrupt

New Arduino interrupt library, designed for Arduino Uno/Mega 2560/Leonardo/Due

Install / Use

/learn @GreyGnome/EnableInterrupt
About this skill

Quality Score

0/100

Category

Design

Supported Platforms

Universal

README

EnableInterrupt

New Arduino interrupt library, designed for all versions of the Arduino.

Functions:

enableInterrupt- Enables interrupt on a selected Arduino pin. disableInterrupt - Disables interrupt on the selected Arduino pin.

*What's New?

  • Wed Sep 4 19:30:45 CDT 2019
    • Version 1.1.0 of the library has been released. We add support for the ATmega2561 and 1281 chips, with pinouts defined from the MCUdude/MegaCore project. Code donations by Kizmit99. Plus, a documentation bugfix from Greg Bowler. Thanks, folks!

The EnableInterrupt library is an Arduino interrupt library, designed for 8-bit versions of the Arduino- at this writing, the Uno (and other ATmega328p-based boards, like the mini), Due, Zero, Leonardo (and other ATmega32u4-based boards, like the Micro), the Mega2560 (and other ATmega2560-based boards, like the MegaADK), and for non-Arduino chips: the 644/1284p (Mighty1284, Sodaq Mbili and EnviroDIY Mayfly) ATtiny 44/84, and ATtiny 45/85 (using DA Mellis' support files). The library enables you to assign an interrupt to pins on your chip that support them, and presents a common interface to all supported chips. This means that on the Arduino Uno and Mega you don't give it an interrupt number, as per http://arduino.cc/en/Reference/attachInterrupt. Rather, your first argument is a pin number of a pin that's supported on that chip (see https://github.com/GreyGnome/EnableInterrupt/wiki/Usage#pin--port-bestiary ).

32-bit support for Due comes only in the form of a macro that enables your code to be shared unchanged between 32- and 8-bit Arduinos. No further support for 32-bit Arduinos is planned.

Download

See the https://github.com/GreyGnome/EnableInterrupt/wiki/Download page to download the library.

More Information

See the Wiki at https://github.com/GreyGnome/EnableInterrupt/wiki/Home . For detailed usage information see https://github.com/GreyGnome/EnableInterrupt/wiki/Usage .

See the examples subdirectory in the distribution or in this Git site for code examples.

See the extras subdirectory in the distribution or in this Git site for License and Release Notes.

For a tutorial on interrupts, see http://www.engblaze.com/we-interrupt-this-program-to-bring-you-a-tutorial-on-arduino-interrupts/ The posting gets into low-level details on interrupts.

IMPORTANT NOTE: In 0.9.2 I discovered a rather pernicious bug, wherein the library was setting the global interrupt enable bit. This could cause a serious and difficult-to-debug race condition, as it is not the job of the library to manage that bit. The chips come with interrupts enabled so existing code should not be affected, but if you were relying on that behavior note that it has changed. My thanks to http://gammon.com.au/interrupts (the 'How are interrupts queued?' section).

ATmega Processor Interrupt Types

Note that the ATmega processor at the heart of the Arduino Uno/Mega2560/Leonardo/ATmega1284 has two different kinds of interrupts: “external”, and “pin change”. For the list of available interrupt pins and their interrupt types, see the PORT / PIN BESTIARY, below.

External Interrupts

There are a varying number of external interrupt pins on the different processors. The Uno supports only 2 and they are mapped to Arduino pins 2 and 3. The 2560 supports 6 usable, the Leonardo supports 5, and the ATmega1284p supports 3. These interrupts can be set to trigger on one of three signal values: RISING, FALLING, or CHANGE (for both), or on LOW level. The triggers are interpreted by hardware, so by the time your user function is running, you know exactly which pin interrupted at the time of the event, and how it changed. On the other hand, as mentioned there are a limited number of these pins.

Pin Change Interrupts

On the Arduino Uno (and again, all 328p-based boards) and 644/1284-based boards, the pin change interrupts can be enabled on any or all of the pins. The two pins 2 and 3 on 328p-based boards, or three pins (2, 10, and 11) on the 1284-based boards support either pin change or external interrupts. On 2560-based Arduinos, there are 18 pin change interrupt pins in addition to the 6 external interrupt pins. On the Leonardo there are 7 pin change interrupt pins in addition to the 5 external interrupt pins. See PIN BESTIARY below for the pin numbers and other details.

Pin Change interrupts trigger on all RISING and FALLING (ie, "CHANGE") signal edges. Furthermore, the processor's pins, and pin change interrupts, are grouped into “port”s, so for example on the Arduino Uno there are three ports and therefore only 3 interrupt vectors (subroutines) available for the entire body of 20 pin change interrupt pins.

The Library and Pin Change Interrupts

The foregoing means that not only do pin change interrupts trigger on all pin transitions, but a number of pins share a single interrupt subroutine. It's the library's function to make pin change interrupts appear that each pin can support RISING, FALLING, or CHANGE, and each pin can support its own user-defined interrupt subroutine.

When an event triggers an interrupt on any interrupt-enabled pin on a port, a library subroutine ("interrupt handler", "interrupt service routine", or "ISR") attached to that pin's port is triggered. It is up to the EnableInterrupt library to set the proper port to receive interrupts for a pin, to determine what happened when an interrupt is triggered (which pin? ...did the signal rise, or fall?), to handle it properly (Did we care if the signal fell? Did we care if it rose?), then to call the programmer's chosen subroutine (ISR). This makes the job of resolving the action on a single pin somewhat complicated. There is a definitive slowdown in the interrupt routine because of this complication. So there is a significant* time between when the interrupt triggers and when the pins are read to determine what actually happened (rising or falling) and which pin changed. So the signal could have changed by the time the pin's status is read, returning a false reading back to your sketch. Therefore, these pins are not suitable for fast changing signals, and under the right conditions such events as a bouncing switch may actually be missed. Caveat Programmer. If you're concerned about this, continue to read the following information and make sure to read the wiki pages; especially see https://github.com/GreyGnome/EnableInterrupt/wiki/Usage#atmega-processor-interrupt-types . For a further review of this issue see https://github.com/GreyGnome/EnableInterrupt/blob/master/Interrupt%20Timing.pdf

USAGE:

Basic Usage

enableInterrupt- Enables interrupt on a selected Arduino pin.

enableInterrupt(uint8_t pinNumber, void (*userFunction)(void), uint8_t mode);
or
enableInterrupt(uint8_t interruptDesignator, void (*userFunction)(void), uint8_t mode);

The arguments are:
* pinNumber - The number of the Arduino pin, such as 3, or A0, or SCK. Note that
these are *not* strings, so when you use A0 for example, do not use quotes.
* interruptDesignator- very much like a pin. See below.
* userFunction - The name of the function you want the interrupt to run. Do not
use a pointer here, just give it the name of your function. See the example code
in the Examples directory.
* mode - What you want the interrupt to interrupt on. For Pin Change Interrupt
pins, the modes supported are RISING, FALLING, or CHANGE.
** RISING - The signal went from "0", or zero volts, to "1", or 5 volts.
** FALLING - The signal went from "1" to "0".
** CHANGE - The signal either rose or fell.

For External Interrupts, the same modes are supported plus the additional mode
of LOW signal level.
** LOW - The signal is at a low level for some time.

Each pin supports only 1 function and 1 mode at a time.

disableInterrupt- Disables interrupt on a selected Arduino pin.

disableInterrupt(uint8_t pinNumber);
or
disableInterrupt(uint8_t interruptDesignator);
  • interruptDesignator: Essentially this is an Arduino pin, and if that's all you want to give the function, it will work just fine. Why is it called an "interruptDesignator", then? Because there's a twist: You can perform a bitwise "and" with the pin number and PINCHANGEINTERRUPT to specify that you want to use a Pin Change Interrupt type of interrupt on those pins that support both Pin Change and External Interrupts. Otherwise, the library will choose whatever interrupt type (External, or Pin Change) normally applies to that pin, with priority to External Interrupt.

  • The complexity is because of pins 2 and 3 on the ATmega328-based Arduinos, and pins 2, 10, and 11 on 1284-based boards. Those are the only pins on the processors supported by this library that can share External or Pin Change Interrupt types. Otherwise, each pin only supports a single type of interrupt and the PINCHANGEINTERRUPT scheme changes nothing. This means you can ignore this whole discussion for ATmega2560, ATmega32U4, or SAM3X8E (Due)-based Arduinos.

It is possible to change the user function assigned to an interrupt after enabling it (if you want). Later in your code simply disable the interrupt and enable it with a different function.

Determine the Pin That Was Interrupted

There is a facility in the library to identify the most recent pin that triggered an interrupt. Set the following definition '''before''' including the EnableInterrupt.h file in your sketch:

 #define EI_ARDUINO_INTERRUPTED_PIN

Then, the ATmega chip will set a variable with every interrupt, and you can query it to find which pin interrupted your sketch. The variable is arduinoInterruptedPin and it is of type uint8_t.

See the https://github.com/GreyGnome/EnableInterrupt/wiki/Usage wiki page for more information.

PIN / PORT BESTIARY

Theoretically pins 0 and 1 (RX and TX) are supported but as these pins have a special purpose on the Arduino, their use in this library has not been tested.

Summary

Arduino Uno/Duemilanove/etc.

Interrupt Type | Pins

View on GitHub
GitHub Stars334
CategoryDesign
Updated27d ago
Forks73

Languages

C++

Security Score

80/100

Audited on Feb 28, 2026

No findings