Craig Evans
/
ELEC2645_TimeTriggeredInterrupt_2021
Ticker example
Diff: main.cpp
- Revision:
- 0:8888b2ad5510
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Dec 09 14:41:51 2020 +0000 @@ -0,0 +1,50 @@ +/* + +2645_Ticker + +Sample code from ELEC2645 + +Demonstrates how to use a ticker to generate a periodic timer interrupt + +(c) Craig A. Evans, University of Leeds, Jan 2016 + +*/ + +#include "mbed.h" + +// Create objects for ticker and red LED +Ticker ticker; +DigitalOut led(LED1); + +// flag - must be volatile as changes within ISR +// g_ prefix makes it easier to distinguish it as global +volatile int g_timer_flag = 0; + +// function prototypes +void timer_isr(); + +int main() +{ + // set-up the ticker so that the ISR it is called every 0.5 seconds + ticker.attach(&timer_isr,0.5); + + while (1) { + + // check if flag is set i.e. interrupt has occured + if (g_timer_flag) { + g_timer_flag = 0; // if it has, clear the flag + printf("Tick \n"); + // DO TASK HERE + } + + // put the MCU to sleep until an interrupt wakes it up + sleep(); + + } +} + +// time-triggered interrupt +void timer_isr() +{ + g_timer_flag = 1; // set flag in ISR +}