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
| InterruptIn | A digital interrupt input, used to call a function on a rising or falling edge |
| Functions | |
| InterruptIn | Create an InterruptIn connected to the specified pin |
| rise | Attach a function to call when a rising edge occurs on the input |
| rise | Attach a member function to call when a rising edge occurs on the input |
| fall | Attach a function to call when a falling edge occurs on the input |
| fall | Attach a member function to call when a falling edge occurs on the input |
| mode | 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)
Last modified 21 Jul 2010, by
Dan Ros

No tags
|
19 comments
I don't see the Edit Page in here, but I would like to add the read function, so other people see it too.
Can anyone fix this?
Lerche