InterruptIn example for LPC1768 board

main.cpp

Committer:
eencae
Date:
2020-12-09
Revision:
0:11b9454d32eb

File content as of revision 0:11b9454d32eb:

/* 

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
}