by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"

Dependencies:   mbed

Committer:
robt
Date:
Fri May 24 21:47:49 2013 +0000
Revision:
0:063d9f9ab154
by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"

Who changed what in which revision?

UserRevisionLine numberNew contents of line
robt 0:063d9f9ab154 1 /*Program Example 9.4: Program which runs two time-based tasks
robt 0:063d9f9ab154 2 */
robt 0:063d9f9ab154 3 #include "mbed.h"
robt 0:063d9f9ab154 4 Timer timer_fast; // define Timer with name "timer_fast"
robt 0:063d9f9ab154 5 Timer timer_slow; // define Timer with name "timer_slow"
robt 0:063d9f9ab154 6 DigitalOut ledA(LED1);
robt 0:063d9f9ab154 7 DigitalOut ledB(LED4);
robt 0:063d9f9ab154 8
robt 0:063d9f9ab154 9 void task_fast(void); //function prototypes
robt 0:063d9f9ab154 10 void task_slow(void);
robt 0:063d9f9ab154 11
robt 0:063d9f9ab154 12 int main() {
robt 0:063d9f9ab154 13 timer_fast.start(); //start the Timers
robt 0:063d9f9ab154 14 timer_slow.start();
robt 0:063d9f9ab154 15 while (1){
robt 0:063d9f9ab154 16 if (timer_fast.read()>0.2){ //test Timer value
robt 0:063d9f9ab154 17 task_fast(); //call the task if trigger time is reached
robt 0:063d9f9ab154 18 timer_fast.reset(); //and reset the Timer
robt 0:063d9f9ab154 19 }
robt 0:063d9f9ab154 20 if (timer_slow.read()>1){ //test Timer value
robt 0:063d9f9ab154 21 task_slow();
robt 0:063d9f9ab154 22 timer_slow.reset();
robt 0:063d9f9ab154 23 }
robt 0:063d9f9ab154 24 }
robt 0:063d9f9ab154 25 }
robt 0:063d9f9ab154 26
robt 0:063d9f9ab154 27 void task_fast(void){ //”Fast” Task
robt 0:063d9f9ab154 28 ledA = !ledA;
robt 0:063d9f9ab154 29 }
robt 0:063d9f9ab154 30 void task_slow(void){ //”Slow” Task
robt 0:063d9f9ab154 31 ledB = !ledB;
robt 0:063d9f9ab154 32 }
robt 0:063d9f9ab154 33