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@3:f729f0421740, 2017-01-19 (annotated)
- Committer:
- mab5449
- Date:
- Thu Jan 19 10:41:34 2017 -0600
- Revision:
- 3:f729f0421740
- Parent:
- 2:dc8472f90484
Ported mbed OS 2 to mbed OS 5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 2:dc8472f90484 | 1 | /* mbed Example Program |
mbedAustin | 2:dc8472f90484 | 2 | * Copyright (c) 2006-2014 ARM Limited |
mbedAustin | 2:dc8472f90484 | 3 | * |
mbedAustin | 2:dc8472f90484 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbedAustin | 2:dc8472f90484 | 5 | * you may not use this file except in compliance with the License. |
mbedAustin | 2:dc8472f90484 | 6 | * You may obtain a copy of the License at |
mbedAustin | 2:dc8472f90484 | 7 | * |
mbedAustin | 2:dc8472f90484 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbedAustin | 2:dc8472f90484 | 9 | * |
mbedAustin | 2:dc8472f90484 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbedAustin | 2:dc8472f90484 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbedAustin | 2:dc8472f90484 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbedAustin | 2:dc8472f90484 | 13 | * See the License for the specific language governing permissions and |
mbedAustin | 2:dc8472f90484 | 14 | * limitations under the License. |
mbedAustin | 2:dc8472f90484 | 15 | */ |
mbed_official | 0:7a20a6aa1f5e | 16 | #include "mbed.h" |
mbed_official | 0:7a20a6aa1f5e | 17 | |
mab5449 | 3:f729f0421740 | 18 | InterruptIn button(SW2); |
mbed_official | 0:7a20a6aa1f5e | 19 | DigitalOut led(LED1); |
mbed_official | 0:7a20a6aa1f5e | 20 | DigitalOut flash(LED4); |
mbed_official | 0:7a20a6aa1f5e | 21 | |
mbed_official | 0:7a20a6aa1f5e | 22 | void flip() { |
mbed_official | 0:7a20a6aa1f5e | 23 | led = !led; |
mbed_official | 0:7a20a6aa1f5e | 24 | } |
mbed_official | 0:7a20a6aa1f5e | 25 | |
mbed_official | 0:7a20a6aa1f5e | 26 | int main() { |
mbed_official | 0:7a20a6aa1f5e | 27 | button.rise(&flip); // attach the address of the flip function to the rising edge |
mbed_official | 0:7a20a6aa1f5e | 28 | while(1) { // wait around, interrupts will interrupt this! |
mbed_official | 0:7a20a6aa1f5e | 29 | flash = !flash; |
mbed_official | 0:7a20a6aa1f5e | 30 | wait(0.25); |
mbed_official | 0:7a20a6aa1f5e | 31 | } |
mbed_official | 0:7a20a6aa1f5e | 32 | } |