Interrupt problem with old&new programs

10 Apr 2012

Hi all...

I have a problem with the interrupts... now if i make the simplest test with interrupts on it doesn't work. My mbed have been on it's box since a month ago until yesterday that I needed a midi input for some test. I used the USBMIDI lib and the MPR121 touch keypad from sparkfun... but when I press a key the program stop at all.

So, I've started to "clean" the code to find the problem and I've found that the problem was the "attach" of the interrupt. Here's my test program:

#include "mbed.h"

DigitalOut led1(LED1);
DigitalOut led4(LED4);
InterruptIn inte(p20);

void hola(void)
{
    led1 = !led1;
}

int main() 
{
    inte.mode(PullUp);
    //inte.fall(&hola); 
    
    while (1) 
    {
        wait(1);
        led4 = !led4;
    }
}

The program works fine as is, but when I uncomment the "inte.fall(&hola);" the program doesn't work (the led4 isn't blinking)

So, I'm a little confused.... any help would be good. (I have the LPC1768 MBED-005.1)

Thanks in advance.

Rolando.

10 Apr 2012

I wonder if it is related to this statement from the mbed handbook section for InterruptIn since I see you are using pin 20?

Quote:

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

10 Apr 2012

Hi Adam.

Thanks for your fast response... and I have only one thing to say..... LOL!!! XD of all the pins that mbed have I have to choose 1 of the 2 that can't drive the interrupts to do my test hahaha. (my bad... I know, I'm allways telling "read the datasheet" when someone ask me for something about a uC)

Well, I went back to my original code and start to "clean" in a diferent way and found the one line that cause the problem.

Here's my code:

#include "mbed.h"
#include "Mpr121.h"
#include "USBMIDI.h"

DigitalOut myled(LED1);
DigitalOut timer(LED4);
Mpr121  touch( p28, p27, p26, ADD_VSS); 
USBMIDI midi;

void botones(void)
{
    int botones = touch.rd_bot_h();
    botones = (botones<<8)+ touch.rd_bot_l();
    
    if( botones == 1)
    {
        midi.write(MIDIMessage::NoteOn(38));//botones));
//        wait(0.25);
    
        myled = !myled;
    }
}

int main() 
{
    touch.attachInte(botones);

    while(true)
    {
        midi.write(MIDIMessage::NoteOn(38));
        wait(0.25);
        midi.write(MIDIMessage::NoteOff(38));
        wait(0.5);
        timer = !timer;
    }
}

The problem was the "wait(0.25);" inside the interrupt routine... I've commented it and it worked like a charm. Anyway... I'll look why this wait cause a problem in the isr and works fine on the main loop.

Thank you very much again.

Rolando

PD: I wasn't even using the p20 on the first place... XD jsut a bad choice.

10 Apr 2012

When you have a wait(0.25) in the ISR, it will cause all interrupts of the same priority or lower to be blocked while it busy waits for 0.25 seconds. With the mbed SDK, most of the interrupt handlers run at the default priority level of 0, which is the highest. This means that while you are running the wait() call, nothing else in the system will run because that ISR is running at the highest priority.

It is probably better to use a Timeout object from within your above ISR and schedule another ISR routine to fire in 0.25 seconds to flip the LED state.