Hello World for InterruptIn
Fork of InterruptIn_HelloWorld by
Use
Interrupts are a way of causing a function to be called when a certain event happens. This example demonstrates calling a function when a button is pressed. Specifically on the rising edge of a button press. This can be observed by LED4 blinking as the program runs and LED1 only changing when the button is pressed.
API
API reference.
Import librarymbed
main.cpp@0:7a20a6aa1f5e, 2013-02-15 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Feb 15 15:13:19 2013 +0000
- Revision:
- 0:7a20a6aa1f5e
- Child:
- 2:dc8472f90484
Hello World for InteruptIn
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:7a20a6aa1f5e | 1 | #include "mbed.h" |
mbed_official | 0:7a20a6aa1f5e | 2 | |
mbed_official | 0:7a20a6aa1f5e | 3 | InterruptIn button(p5); |
mbed_official | 0:7a20a6aa1f5e | 4 | DigitalOut led(LED1); |
mbed_official | 0:7a20a6aa1f5e | 5 | DigitalOut flash(LED4); |
mbed_official | 0:7a20a6aa1f5e | 6 | |
mbed_official | 0:7a20a6aa1f5e | 7 | void flip() { |
mbed_official | 0:7a20a6aa1f5e | 8 | led = !led; |
mbed_official | 0:7a20a6aa1f5e | 9 | } |
mbed_official | 0:7a20a6aa1f5e | 10 | |
mbed_official | 0:7a20a6aa1f5e | 11 | int main() { |
mbed_official | 0:7a20a6aa1f5e | 12 | button.rise(&flip); // attach the address of the flip function to the rising edge |
mbed_official | 0:7a20a6aa1f5e | 13 | while(1) { // wait around, interrupts will interrupt this! |
mbed_official | 0:7a20a6aa1f5e | 14 | flash = !flash; |
mbed_official | 0:7a20a6aa1f5e | 15 | wait(0.25); |
mbed_official | 0:7a20a6aa1f5e | 16 | } |
mbed_official | 0:7a20a6aa1f5e | 17 | } |