Juan Angel García
/
9_4
Dos temporizadores
main.cpp@0:333da935657e, 2017-03-09 (annotated)
- Committer:
- jangelgm
- Date:
- Thu Mar 09 21:42:16 2017 +0000
- Revision:
- 0:333da935657e
Dos temporizadores
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jangelgm | 0:333da935657e | 1 | /*Program Example 9.4: Program which runs two time-based tasks |
jangelgm | 0:333da935657e | 2 | */ |
jangelgm | 0:333da935657e | 3 | #include "mbed.h" |
jangelgm | 0:333da935657e | 4 | Timer timer_fast; // define Timer with name "timer_fast" |
jangelgm | 0:333da935657e | 5 | Timer timer_slow; // define Timer with name "timer_slow" |
jangelgm | 0:333da935657e | 6 | |
jangelgm | 0:333da935657e | 7 | DigitalOut ledA(LED1); |
jangelgm | 0:333da935657e | 8 | DigitalOut ledB(LED4); |
jangelgm | 0:333da935657e | 9 | |
jangelgm | 0:333da935657e | 10 | void task_fast(void); //function prototypes |
jangelgm | 0:333da935657e | 11 | void task_slow(void); |
jangelgm | 0:333da935657e | 12 | |
jangelgm | 0:333da935657e | 13 | int main() |
jangelgm | 0:333da935657e | 14 | { |
jangelgm | 0:333da935657e | 15 | timer_fast.start(); //start the Timers |
jangelgm | 0:333da935657e | 16 | timer_slow.start(); |
jangelgm | 0:333da935657e | 17 | while (1) { |
jangelgm | 0:333da935657e | 18 | if (timer_fast.read()>0.2) { //test Timer value |
jangelgm | 0:333da935657e | 19 | task_fast(); //call the task if trigger time is reached |
jangelgm | 0:333da935657e | 20 | timer_fast.reset(); //and reset the Timer |
jangelgm | 0:333da935657e | 21 | } |
jangelgm | 0:333da935657e | 22 | if (timer_slow.read()>1) { //test Timer value |
jangelgm | 0:333da935657e | 23 | task_slow(); |
jangelgm | 0:333da935657e | 24 | timer_slow.reset(); |
jangelgm | 0:333da935657e | 25 | } |
jangelgm | 0:333da935657e | 26 | } |
jangelgm | 0:333da935657e | 27 | } |
jangelgm | 0:333da935657e | 28 | void task_fast(void) //”Fast” Task |
jangelgm | 0:333da935657e | 29 | { |
jangelgm | 0:333da935657e | 30 | ledA = !ledA; |
jangelgm | 0:333da935657e | 31 | } |
jangelgm | 0:333da935657e | 32 | void task_slow(void) //”Slow” Task |
jangelgm | 0:333da935657e | 33 | { |
jangelgm | 0:333da935657e | 34 | ledB = !ledB; |
jangelgm | 0:333da935657e | 35 | } |