Recently changed pages
Debugging Debugging
mbed NXP LPC1768 mbed NXP LPC1768
Homepage Homepage
Compiler Tour Compiler Tour
Media Media
Serial Serial
Terminals Terminals
From the mbed microcontroller Handbook.

InterruptIn

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

Any of the numbered mbed pins can be used as an InterruptIn, except p19 and p20.

Hello World!

Flip an LED every time we see the rising edge interrupt on a pin

#include "mbed.h"

InterruptIn button(p5);
DigitalOut led(LED1);
DigitalOut flash(LED4);

void flip() {
    led = !led;
}

int main() {
    button.rise(&flip);  // attach the address of the flip function to the rising edge
    while(1) {           // wait around, interrupts will interrupt this!
        flash = !flash;
        wait(0.25);
    }
}

API

API summary

InterruptInA digital interrupt input, used to call a function on a rising or falling edge
Functions
InterruptInCreate an InterruptIn connected to the specified pin
riseAttach a function to call when a rising edge occurs on the input
riseAttach a member function to call when a rising edge occurs on the input
fallAttach a function to call when a falling edge occurs on the input
fallAttach a member function to call when a falling edge occurs on the input
modeSet the input pin mode
class InterruptIn : public Base
A digital interrupt input, used to call a function on a rising or falling edge
InterruptIn(PinName pin,  
const char *name =  NULL)
Create an InterruptIn connected to the specified pin
void rise(void (*fptr)(void))
Attach a function to call when a rising edge occurs on the input
void fall(void (*fptr)(void))
Attach a function to call when a falling edge occurs on the input
void mode(PinMode pull)
Set the input pin mode

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.

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);
    }
}

Related

To read an input, see DigitalIn

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




calendar Page history
Last modified 21 Jul 2010, by user avatar Dan Ros   tag No tags | 0 replies     Share: Digg Tweet This

Please login to post comments.