InterruptIn

mbed OS 2 and mbed OS 5

This is the handbook for mbed OS 2. If you’re working with mbed OS 5, please see the new handbook and API References. 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 library

Public Member Functions

  InterruptIn (PinName pin)
  Create an InterruptIn connected to the specified pin.
int  read ()
  Read the input, represented as 0 or 1 (int)
  operator int ()
  An operator shorthand for read()
void  rise (Callback< void()> func)
  Attach a function to call when a rising edge occurs on the input.
template<typename T , typename M >
  MBED_DEPRECATED_SINCE ("mbed-os-5.1","The rise function does not support cv-qualifiers. Replaced by ""rise(callback(obj, method)).") void rise(T *obj
  Attach a member function to call when a rising edge occurs on the input.
void  fall (Callback< void()> func)
  Attach a function to call when a falling edge occurs on the input.
template<typename T , typename M >
  MBED_DEPRECATED_SINCE ("mbed-os-5.1","The fall function does not support cv-qualifiers. Replaced by ""fall(callback(obj, method)).") void fall(T *obj
  Attach a member function to call when a falling edge occurs on the input.
void  enable_irq ()
  Enable IRQ.
void  disable_irq ()
  Disable IRQ.

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)