Juan Angel García
/
9_7
Timeout e interrupciones
main.cpp@0:00e36fb49aa5, 2017-03-09 (annotated)
- Committer:
- jangelgm
- Date:
- Thu Mar 09 21:45:17 2017 +0000
- Revision:
- 0:00e36fb49aa5
Timeout e interrupciones
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jangelgm | 0:00e36fb49aa5 | 1 | /*Program Example 9.7: Demonstrates the use of Timeout and interrupts, to allow response |
jangelgm | 0:00e36fb49aa5 | 2 | to an event-driven task while a time-driven task continues. |
jangelgm | 0:00e36fb49aa5 | 3 | */ |
jangelgm | 0:00e36fb49aa5 | 4 | #include "mbed.h" |
jangelgm | 0:00e36fb49aa5 | 5 | void blink_end (void); |
jangelgm | 0:00e36fb49aa5 | 6 | void blink (void); |
jangelgm | 0:00e36fb49aa5 | 7 | void ISR1 (void); |
jangelgm | 0:00e36fb49aa5 | 8 | DigitalOut led1(LED1); |
jangelgm | 0:00e36fb49aa5 | 9 | DigitalOut led2(LED2); |
jangelgm | 0:00e36fb49aa5 | 10 | DigitalOut led3(LED3); |
jangelgm | 0:00e36fb49aa5 | 11 | Timeout Response; //create a Timeout, and name it Response |
jangelgm | 0:00e36fb49aa5 | 12 | Timeout Response_duration; //create a Timeout, and name it Response_duration |
jangelgm | 0:00e36fb49aa5 | 13 | InterruptIn button(p5); //create an interrupt input, named button |
jangelgm | 0:00e36fb49aa5 | 14 | void blink() //This function is called when Timeout is complete |
jangelgm | 0:00e36fb49aa5 | 15 | { |
jangelgm | 0:00e36fb49aa5 | 16 | led2=1; |
jangelgm | 0:00e36fb49aa5 | 17 | // set the duration of the led blink, with another timeout, duration 1 s |
jangelgm | 0:00e36fb49aa5 | 18 | Response_duration.attach(&blink_end, 1); |
jangelgm | 0:00e36fb49aa5 | 19 | } |
jangelgm | 0:00e36fb49aa5 | 20 | void blink_end() //A function called at the end of Timeout Response_duration |
jangelgm | 0:00e36fb49aa5 | 21 | { |
jangelgm | 0:00e36fb49aa5 | 22 | led2=0; |
jangelgm | 0:00e36fb49aa5 | 23 | } |
jangelgm | 0:00e36fb49aa5 | 24 | void ISR1() |
jangelgm | 0:00e36fb49aa5 | 25 | { |
jangelgm | 0:00e36fb49aa5 | 26 | led3=1; //shows button is pressed; diagnostic and not central to program |
jangelgm | 0:00e36fb49aa5 | 27 | //attach blink1 function to Response Timeout, to occur after 2 seconds |
jangelgm | 0:00e36fb49aa5 | 28 | Response.attach(&blink, 2.0); |
jangelgm | 0:00e36fb49aa5 | 29 | } |
jangelgm | 0:00e36fb49aa5 | 30 | int main() |
jangelgm | 0:00e36fb49aa5 | 31 | { |
jangelgm | 0:00e36fb49aa5 | 32 | button.rise(&ISR1); //attach the address of ISR1 function to the rising edge |
jangelgm | 0:00e36fb49aa5 | 33 | while(1) { |
jangelgm | 0:00e36fb49aa5 | 34 | led3=0; //clear LED3 |
jangelgm | 0:00e36fb49aa5 | 35 | led1=!led1; |
jangelgm | 0:00e36fb49aa5 | 36 | wait(0.2); |
jangelgm | 0:00e36fb49aa5 | 37 | } |
jangelgm | 0:00e36fb49aa5 | 38 | } |