InterruptIn

This content relates to a deprecated version of Mbed

Mbed 2 is now deprecated. For the latest version please see the Mbed OS documentation.

For the latest InterruptIn API, please see InterruptIn.

The InterruptIn interface is used to trigger an event when a digital input pin changes.

Hello World!

Import program

00001 #include "mbed.h"
00002  
00003 InterruptIn button(p5);
00004 DigitalOut led(LED1);
00005 DigitalOut flash(LED4);
00006  
00007 void flip() {
00008     led = !led;
00009 }
00010  
00011 int main() {
00012     button.rise(&flip);  // attach the address of the flip function to the rising edge
00013     while(1) {           // wait around, interrupts will interrupt this!
00014         flash = !flash;
00015         wait(0.25);
00016     }
00017 }

API

API summary

Import librarymbed

No documentation found.

Interface

Certain pins cannot be used for InterruptIn

  • mbed NXP LPC1768: Any of the numbered mbed pins can be used as an InterruptIn, except p19 and p20.
  • mbed FRDM KL25Z: Only the pins of port A and D can be used. (PTA[0-31] and PTD[0-31]).
/media/uploads/chris/pinout-thumbnails.jpg
See the Pinout page for more details

The pin input will be logic '0' for any voltage on the pin below 0.8v, and '1' for any voltage above 2.0v. By default, the InterruptIn is setup with an internal pull-down resistor.

No blocking code in ISR

In ISR you should avoid any call to wait, infinitive while loop, or blocking calls in general.

No printf, malloc, or new in ISR

In ISR you should avoid any call to bulky library functions. In particular, certain library functions (like printf, malloc and new) are non re-entrant and their behaviour could be corrupted when called from an ISR.

Examples

An example class for counting rising edges on a pin

#include "mbed.h"

class Counter {
public:
    Counter(PinName pin) : _interrupt(pin) {        // create the InterruptIn on the pin specified to Counter
        _interrupt.rise(this, &Counter::increment); // attach increment function of this counter instance
    }

    void increment() {
        _count++;
    }

    int read() {
        return _count;
    }

private:
    InterruptIn _interrupt;
    volatile int _count;
};

Counter counter(p5);

int main() {
    while(1) {
        printf("Count so far: %d\n", counter.read());
        wait(2);
    }
}

To read an input, see DigitalIn

For timer-based interrupts, see Ticker (repeating interrupt) and Timeout (one-time interrupt)


All wikipages