Recent changes
SerialPC
Creating a program
Downloading a program
Setup guide
Exporting to Code Red
Exporting to uVision
Order
tag order
From the mbed microcontroller Handbook.  

InterruptIn

The InterruptIn interface is used to trigger an event when a digital input pin changes.

Any of the numbered mbed pins can be used as an InterruptIn, except p19 and p20.

Hello World!

Flip an LED every time we see the rising edge interrupt on a pin

#include "mbed.h"

InterruptIn button(p5);
DigitalOut led(LED1);
DigitalOut flash(LED4);

void flip() {
    led = !led;
}

int main() {
    button.rise(&flip);  // attach the address of the flip function to the rising edge
    while(1) {           // wait around, interrupts will interrupt this!
        flash = !flash;
        wait(0.25);
    }
}

API

API summary

InterruptInA digital interrupt input, used to call a function on a rising or falling edge
Functions
InterruptInCreate an InterruptIn connected to the specified pin
riseAttach a function to call when a rising edge occurs on the input
riseAttach a member function to call when a rising edge occurs on the input
fallAttach a function to call when a falling edge occurs on the input
fallAttach a member function to call when a falling edge occurs on the input
modeSet the input pin mode
class InterruptIn : public Base
A digital interrupt input, used to call a function on a rising or falling edge
InterruptIn(PinName pin,  
const char *name =  NULL)
Create an InterruptIn connected to the specified pin
void rise(void (*fptr)(void))
Attach a function to call when a rising edge occurs on the input
void fall(void (*fptr)(void))
Attach a function to call when a falling edge occurs on the input
void mode(PinMode pull)
Set the input pin mode

Details

The pin input will be logic '0' for any voltage on the pin below 0.8v, and '1' for any voltage above 2.0v. By default, the InterruptIn is setup with an internal pull-down resistor.

Examples

An example class for counting rising edges on a pin

#include "mbed.h"

class Counter {
public:
    Counter(PinName pin) : _interrupt(pin) {        // create the InterruptIn on the pin specified to Counter
        _interrupt.rise(this, &Counter::increment); // attach increment function of this counter instance
    }

    void increment() {
        _count++;
    }

    int read() {
        return _count;
    }

private:
    InterruptIn _interrupt;
    volatile int _count;
};

Counter counter(p5);

int main() {
    while(1) {
        printf("Count so far: %d\n", counter.read());
        wait(2);
    }
}

Related

To read an input, see DigitalIn

For timer-based interrupts, see Ticker (repeating interrupt) and Timeout (one-time interrupt)




calendar Page history
Last modified 21 Jul 2010, by   user Dan Ros   tag No tags | 19 comments      

19 comments on InterruptIn:

26 Oct 2010

I don't see the Edit Page in here, but I would like to add the read function, so other people see it too.

Can anyone fix this?

Lerche

30 Oct 2010

Is the first example correct. I can't seem to get it working. Only led4 is blinking.

30 Oct 2010

Well, you need a input of some sort on pin 5 to make the interrupt trigger. Lerche

23 Feb 2011

I have two InterruptIn pins and two different functions when pins are asserted. I noticed that only 1 ISR is executed. Other is not even once executed!!!! Can anyone help me on this????

Nitin

23 Feb 2011

Nitin, check that you are not using p19 or p20. If that's not it, please post the program and more details in the forum.

24 Feb 2011

Hi Igor, I am not using p19,p20. I am trying to interface OV7670 camera. Initialization is done by atmega32. TRIG pin is toggled once to indicate atmega32 to start initialization. After initialization, Atmega32 sets ECHO pin in order to acknowledge the initialization request. After initialization, I am supposed to get 16-bit data from cam. Cam's data lines are 8-bit. So, 16-bit is got when PCLK(CAM) pin toggles twice. There is also a VSYNC pin to indicate the start of frame. So I have 2 ISR as there are 2 InterruptIn pin. Following is the code, Only ISR vsync() gets executed. ISR pclk() is not executed even once. When i comment out ISR vsync(), ISR pclk() gets executed upon interrupt request!!!! And one more problem is that when I try reading the 8-bit data lines in BusIn mode, I get proper data (ISR vsync() commented). But in PortIn mode I get garbage data. Please help me....

Code

#include "mbed.h"
#include "pcf8833.h"                      //Nokia 6100 LCD header

/*
Camera data pins    Mbed pins 
D0                    p26
D1                    p25 
D2                    p24
D3                    p23
D4                    p22
D5                    p21
D6                    p19
D7                    p20
*/


PortIn PORT1(Port1,0xC0000000);           //Camera output data D7,D6
PortIn PORT2(Port2,0x3F);                 //Camera output data D5-D0  

DigitalOut myled(LED1);

DigitalOut OSC_E(p7);                     //Camera oscillation Enable pin 
DigitalOut TRIG(p8);                      //Trigger pin used to request camera initialization(initialization will be done by atmega32)  

DigitalIn ECHO(p9);                       //Echo pin is set by atmega32 in order to indicate that its has completed initializing the camera

InterruptIn VSYNC(p5);                    //Camera VSYNC
InterruptIn PCLK(p14);                    //Camera pixel clk

unsigned int count,count1;
void pclk(void)
{
    OSC_E=0;                             //stop camera oscillation
    WriteSpiData(((PORT1.read()>>24)&0xC0)|(PORT2.read()&0x3F));  //read 8-bit data on display on Nokia LCD
    OSC_E=1;                             //start camera oscillation
    
}
void vsync(void)
{
    OSC_E=0;
    WriteSpiCommand(PASET);              //To set LCD cursor position at 0,0
    WriteSpiData(0);
    WriteSpiData(131);

    WriteSpiCommand(CASET);
    WriteSpiData(0);
    WriteSpiData(131);
    WriteSpiCommand(RAMWR);
    OSC_E=1;
}
int main() {
    wait(1);
    InitLcd();
    LCDClearScreen(Red);
    LCDClearScreen(Green);
    OSC_E=1;
    TRIG=1;
    TRIG=0;
    while(!ECHO);
    PCLK.rise(&pclk);
    VSYNC.fall(&vsync);
    while(1) {
    }
}

Thanks Nitin

02 Jun 2011

Hello,

Does InterruptIn have priority over rest of the code? What i am trying to do is to increase the value of a variable even when the code is stuck with an other line of the code. (Writing in a file in my case)

thanks Levi

08 Jul 2011

Basically 'yes'. Daniel

28 Aug 2011

@Nitin

I'm about to use multiple interrupts too so I've made some tests. It turn out that when I wired two pins, with different ISRs, to the same interrupt source, sometimes (!) only one routine get to execute. Triggered separately they both work as expected. To be fair it's a breadboard test with some dirty wiring and no debouncing that may cause loss of a flank to one of the interrupts. A quick (i.e not very computed) lowpass filter does not eliminate the problem but makes it occur less frequent.

My vote is on the electrical.

/Per

22 Sep 2011

Hello,

once created an InterruptIn and .rise attached to it, how to stop them?

I use p17, p18. I tried to delete the entire InterruptIn object, but they still keep responding.

Bug or feature? Or did I understand something wrong regarding deleting objects.

Thanks Ju

26 Sep 2011

Hi there.

This function is working fine for me with one exception.

How can I initialize the state? Depending on when I boot up my sensor is either HIGH or LOW and I need to display that on my screen. Any ideas how to read the state of the pin and then 'force' the appropriate interrupt so I have the correct startup state displayed?

Since this is an interrupt function, there is no way to read the initial value.

26 Sep 2011

I have a question about the digital inputs on the mbed. I read the tutorial posted on the mbed website that gives some basic programming guides and examples.

What I'm interested in is looking at one of the digital inputs for a certain period of time. For example, if the input is high for 5 seconds continuously without fluctuations, then and only then send the desired output.

I know that I can implement this using some conditional programming and IF conditions, but I don't know how to tell the mbed to look at the input for a specific amount of time.

Can anyone please help me with this?

27 Sep 2011

Eric:

There are a few ways depending on your system and how fast this input may change.

1) Stay in a loop for 5 seconds and continually poll the input. Exit on a timer or a change in pin level. 2) Set up a timer interrupt. (Ticker) and have an interrupt occur every 100ms. Check the state of the pin every 100ms (on the Ticker interrupt) and count up to 5 seconds. When the 5 seconds is up, disable the timer. 3) Start a timer and check that timer when an interrupt on that pin finally occurs. If it is past the 5 seconds, then the data is valid, if it is before the 5 seconds, then the ISR simply ignores the data.

There are a few more but this will at least get you started.

06 Oct 2011

I just made a measuremement using mbed2368 in an oscilloscope to see how fast my InterruptIn responds? I found it is 10uS. Do you people think my measurement is correct, if yes Is there any way the 10uS latency can be reduced say towards 1uS?

code I tested: with osciloscope channel1 connected to clk (square pulse generated in main p21) and second channel of CRO one to output(p22). and saw the delay between the rising edges to be 10uS.

  1. include "mbed.h"

DigitalOut clk(p21); InterruptIn button(p26); DigitalOut output(p22); DigitalOut flash(LED1);

void flip() { output = !output; }

int main() { button.rise(&flip); attach the address of the flip function to the rising edge while(1) { wait around, interrupts will interrupt this! flash = !flash; clk = !clk; wait(0.25); } }

pl. comment.

3 weeks, 2 days ago

Hi:

In the InterruptIn example above, if the interrupt pin is set to p20 for mbed LPC1768, it will not work.

Code

#include "mbed.h"

InterruptIn button(p20);
DigitalOut led(LED1);
DigitalOut flash(LED4);

void flip() {
    led = !led;
}

int main() {
    button.rise(&flip);  // attach the address of the flip function to the rising edge
    while(1) {           // wait around, interrupts will interrupt this!
        flash = !flash;
        wait(0.25);
    }
}

From PinName.h, for LPC1768

p20 = P0_22

LED1 = P1_8

LED2 = P1_9

LED3 = P1_10

LED4 = P1_11

Any clue?

3 weeks, 2 days ago

second row from this page says: "Any of the numbered mbed pins can be used as an InterruptIn, except p19 and p20. "

3 weeks, 1 day ago

Perhaps some kind of ASSERT is implemented in the source code to flag it as error.

3 weeks, 1 day ago

p19 and p20 are on port1

http://mbed.org/users/Lerche/notebook/lpc1768-pin-functions/

In pinname.h it says (following your link):

Quote:

57 , p19 = P1_30

58 , p20 = P1_31

1 week, 2 days ago

Quote:

Any of the numbered mbed pins can be used as an InterruptIn, except p19 and p20.

Is this true for the Cortex-M0 mbed too?

Please login to post comments.