by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
main.cpp@0:7a3136735404, 2013-05-24 (annotated)
- Committer:
- robt
- Date:
- Fri May 24 21:48:44 2013 +0000
- Revision:
- 0:7a3136735404
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:7a3136735404 | 1 | /*Program Example 9.7: Demonstrates the use of Timeout and interrupts, to allow response to an event-driven task while a time-driven task continues. |
robt | 0:7a3136735404 | 2 | */ |
robt | 0:7a3136735404 | 3 | #include "mbed.h" |
robt | 0:7a3136735404 | 4 | void blink_end (void); |
robt | 0:7a3136735404 | 5 | void blink (void); |
robt | 0:7a3136735404 | 6 | void ISR1 (void); |
robt | 0:7a3136735404 | 7 | DigitalOut led1(LED1); |
robt | 0:7a3136735404 | 8 | DigitalOut led2(LED2); |
robt | 0:7a3136735404 | 9 | DigitalOut led3(LED3); |
robt | 0:7a3136735404 | 10 | Timeout Response; //create a Timeout, and name it Response |
robt | 0:7a3136735404 | 11 | Timeout Response_duration; //create a Timeout, and name it Response_duration |
robt | 0:7a3136735404 | 12 | InterruptIn button(p5); //create an interrupt input, named button |
robt | 0:7a3136735404 | 13 | |
robt | 0:7a3136735404 | 14 | void blink() { //This function is called when Timeout is complete |
robt | 0:7a3136735404 | 15 | led2=1; |
robt | 0:7a3136735404 | 16 | // set the duration of the led blink, with another timeout, duration 0.1 s |
robt | 0:7a3136735404 | 17 | Response_duration.attach(&blink_end, 1); |
robt | 0:7a3136735404 | 18 | } |
robt | 0:7a3136735404 | 19 | |
robt | 0:7a3136735404 | 20 | void blink_end() { //A function called at the end of Timeout Response_duration |
robt | 0:7a3136735404 | 21 | led2=0; |
robt | 0:7a3136735404 | 22 | } |
robt | 0:7a3136735404 | 23 | |
robt | 0:7a3136735404 | 24 | void ISR1(){ |
robt | 0:7a3136735404 | 25 | led3=1; //shows button is pressed; diagnostic and not central to program |
robt | 0:7a3136735404 | 26 | //attach blink1 function to Response Timeout, to occur after 2 seconds |
robt | 0:7a3136735404 | 27 | Response.attach(&blink, 2.0); |
robt | 0:7a3136735404 | 28 | } |
robt | 0:7a3136735404 | 29 | |
robt | 0:7a3136735404 | 30 | int main() { |
robt | 0:7a3136735404 | 31 | button.rise(&ISR1); //attach the address of ISR1 function to the rising edge |
robt | 0:7a3136735404 | 32 | while(1) { |
robt | 0:7a3136735404 | 33 | led3=0; //clear LED3 |
robt | 0:7a3136735404 | 34 | led1=!led1; |
robt | 0:7a3136735404 | 35 | wait(0.2); |
robt | 0:7a3136735404 | 36 | } |
robt | 0:7a3136735404 | 37 | } |
robt | 0:7a3136735404 | 38 |