by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
main.cpp@0:573e9069e2ea, 2013-05-24 (annotated)
- Committer:
- robt
- Date:
- Fri May 24 21:50:05 2013 +0000
- Revision:
- 0:573e9069e2ea
by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
robt | 0:573e9069e2ea | 1 | /* Program Example 9.12: Event driven LED switching with switch debounce |
robt | 0:573e9069e2ea | 2 | */ |
robt | 0:573e9069e2ea | 3 | #include "mbed.h" |
robt | 0:573e9069e2ea | 4 | InterruptIn button(p18); // Interrupt on digital pushbutton input p18 |
robt | 0:573e9069e2ea | 5 | DigitalOut led1(LED1); // digital out to LED1 |
robt | 0:573e9069e2ea | 6 | Timer debounce; // define debounce timer |
robt | 0:573e9069e2ea | 7 | void toggle(void); // function prototype |
robt | 0:573e9069e2ea | 8 | int main() { |
robt | 0:573e9069e2ea | 9 | debounce.start(); |
robt | 0:573e9069e2ea | 10 | button.rise(&toggle); // attach the address of the toggle |
robt | 0:573e9069e2ea | 11 | } // function to the rising edge |
robt | 0:573e9069e2ea | 12 | void toggle() { |
robt | 0:573e9069e2ea | 13 | if (debounce.read_ms()>10) // only allow toggle if debounce timer |
robt | 0:573e9069e2ea | 14 | led1=!led1; // has passed 10 ms |
robt | 0:573e9069e2ea | 15 | debounce.reset(); // restart timer when the toggle is performed |
robt | 0:573e9069e2ea | 16 | } |
robt | 0:573e9069e2ea | 17 |