You are viewing an older revision! See the latest version
Timers interrupts and tasks
In the following exercises you will learn to use Timer
, Timeout
, Ticker
and InterruptIn
classes from the standard mbed library. The program codes are from the following excellent book: Toulson, R. & Wilmshurst, T. (2012). Fast and Effective Embedded Systems Design - Applying the ARM mbed, Newnes, Oxford, ISBN: 9780080977683.
Exercise 1: Using the mbed Timer
¶
Examine the following code and try to figure out what will happen with the LEDs. Then test the program on an actual mbed and see if your predictions were correct.
#include "mbed.h" Timer timer_fast; Timer timer_slow; DigitalOut ledA(LED1); DigitalOut ledB(LED4); void task_fast(void); void task_slow(void); int main() { timer_fast.start(); timer_slow.start(); while (1){ if (timer_fast.read() > 0.2) { task_fast(); timer_fast.reset(); } if (timer_slow.read() > 1) { task_slow(); timer_slow.reset(); } } } void task_fast(void) { ledA = !ledA; } void task_slow(void) { ledB = !ledB; }