You are viewing an older revision! See the latest version
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¶
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)