Craig Evans
/
ELEC2645_EventTriggeredInterrupt_2021
InterruptIn example for LPC1768 board
Diff: main.cpp
- Revision:
- 0:11b9454d32eb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Dec 09 13:25:08 2020 +0000 @@ -0,0 +1,57 @@ +/* + +2645_InterruptIn + +Sample code from ELEC2645 + +Demonstrates how to use InterruptIn to generate an event-triggered interrupt + +(c) Craig A. Evans, University of Leeds, Dec 2020 + +*/ + +#include "mbed.h" + +// Create objects for button A and LED1 +InterruptIn buttonA(p29); +DigitalOut led(LED1); + +// flag - must be volatile as changes within ISR +// g_ prefix makes it easier to distinguish it as global +volatile int g_buttonA_flag = 0; + +// function prototypes +void buttonA_isr(); + +int main() +{ + // Button A has a pull-down resistor, so the pin will be at 0 V by default + // and rise to 3.3 V when pressed. We therefore need to look for a rising edge + // on the pin to fire the interrupt + buttonA.rise(&buttonA_isr); + // since Button A has an external pull-down, we should disable to internal pull-down + // resistor that is enabled by default using InterruptIn + buttonA.mode(PullNone); + + while (1) { + + // check if flag i.e. interrupt has occured + if (g_buttonA_flag) { + g_buttonA_flag = 0; // if it has, clear the flag + + // send message over serial port - can observe in CoolTerm etc. + printf("Execute task \n"); + // DO TASK HERE + } + + // put the MCU to sleep until an interrupt wakes it up + sleep(); + + } +} + +// Button A event-triggered interrupt +void buttonA_isr() +{ + g_buttonA_flag = 1; // set flag in ISR +}