External Interrupt in ARM MBED LPC1768

Introduction

An interrupt is an event that occurs randomly in the flow of continuity. It is just like a call you have when you are busy with some work and depending upon call priority you decide whether to attend or neglect it.

ARM MBED has interrupt feature on its GPIO pins. In ARM MBED board pins 5 to 30 can be used as an interrupt input, excepting only pins 19 and 20.

We can set rising edge, falling edge, both edges, low level, and high level interrupt modes on GPIO pins of MBED board. It will generate an interrupt when set event (modes) of interrupt occurs.

 

External Interrupt Functions for MBED

We need to use below functions to initialize interrupt and mode of interrupt for specific GPIO pin.

InterruptIn(pin)

Create an InterruptIn Object to create an interrupt of the specified pin.

pin: provide pin name where you want to connect

e.g.

InterruptIn event(p16);

 

disable_irq()

To disable interrupt request

e.g.

event.disable_irq();

 

enable_irq()

To enable interrupt request

e.g.

event.enable_irq();

 

mode (pull)

To set the input pin mode.

Pull: PullUp, PullDown, PullNone

e.g.

event.mode (PullUp);

 

read (void)

To read the interrupt pin respected as 0 or 1.

Return:

0 for logic 0 and 1 for logic 1.

e.g.

event.read();

 

             fall (Callback< void()> func)

Attach interrupt function request when falling edge is occurred

e.g.

event.fall(&function name);

 

             rise (Callback< void()> func)

Attach interrupt function request when rising edge is occurred

e.g.

event.rise(&function name);

 

Example

Let’s write a program which will generate an interrupt when rising edge detected on GPIO (here p16) pin. Here, switch input is used to interrupt MBED. We will print ‘triggered’ on serial window when interrupt occurred.

 

Push Button Interfacing With ARM MBED LPC1768

Push Button interfacing with ARM MBED LPC1768

 

Program​​​​​​

// Flash an LED while waiting for events
#include "mbed.h"
InterruptIn event(p16);	//create interrupt event object for p16 pin
DigitalOut led(LED1);	//create led object for led1

void trigger() {
printf("triggered!\n");	//print triggered! on serial window
}

int main() {
    event.rise(&trigger);	//call trigger function when interrupt event is rise
    while(1) {
        led = !led;	//invert led
 	    wait(0.25);	//wait for 250ms
    }
}

Components Used

ARM mbed 1768 LPC1768 Board
ARM mbed 1768 Board
1
Ad