interrupt routine not working

11 Feb 2011

Hi,

Strange issue: got some code working. Placed it in a function. In the main loop i've placed a interrupt to call upon that code. But somehow it will immediatly go to the function.

#include "mbed.h"
#include "SCP1000.h"


# define SAMPLENRS 7000
# define SAMPLES_SEC 161364
# define SAMPLERATE 9600

Serial pc(USBTX, USBRX);
SCP1000 scp1000(p5,p6,p7,p8);
Serial lcd (p28, p27);
InterruptIn Potmeter(p5);
DigitalOut led(LED1);

void clear(void);
void Measure(void);
unsigned int samples[SAMPLENRS];
unsigned int average;
unsigned int sum;
Timer t;

int main() {
    Potmeter.rise(&Measure); //if P5 high, then measure
    while (1) {
        led = !led;
        wait(0.25);
        pc.printf("say something");
    }
}

void Measure() {
// here are we measuring values coming in from de ADC
}

I can't see what im doing wrong. if i remove the line of code with the interrupt i see the LED blinking, and i got text through realterm.

What am i doing wrong ?

marcel

11 Feb 2011

There was a recent known issue that made InterruptIn fire on program start. Ir was resolved with a new Mbed library release.

In the compiler select the mbed library in the program workspace on the left. Then look in the summary on the right. If there is a button there called "update to teh latest version" click it to upgrade. Then recompile your program and see if it fires at startup again.

Let us know how you get on.

15 Feb 2011

Hi,

I've tried the option Andy said, but no difference. Another strange thing is that i can use only pin5 for the interrupt. If i use another pin, change the code correct afcourse, the whole interrupt doesn't seem to work. Even de small example doesn't work.

example: (here i changed button(p5) to P20) Hooked up a switch to p20. Nothing happens. If i change everything back to p5, then everything works)

#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);
    }
}
15 Feb 2011

Please read the handbook page:-

Quote:

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

15 Feb 2011

Can't believe i looked over that line. Oke. Thanks!, that problem is solved. Now further with the interrupt problem itself. Can, using a microswitch, causing any problems?

15 Feb 2011

you haven't really told us how p5 is connected. Any mechanical switch can cause problems with InterruptIn. Try using PinDetect instead which is much more suitable for mechanically switched inputs.

You haven't shown us what's in void Measure(void);

Rather than give us snippets to work with, publish the entire program and give us a link to it so we can see the full context.

16 Feb 2011

Andy,

I will clean up the code, and then i will publish it. First i have other things to do, because the mbed is the prototype for a commercial project. Today or tomorrow i will publish it. Thanks for all the feedback so far

18 Feb 2011

Hi,

I published the program here: http://mbed.org/users/marcelvandekamp/programs/ADS8320V3_example/lmsvyu

with sincere greetings,

Marcel

18 Feb 2011

Hi Marcel,

You might want to try adding these two lines before you attach the .rise() callback. This just makes sure any pending interrupts are cleared.

int main() {
    LPC_GPIOINT->IO0IntClr = (LPC_GPIOINT->IO0IntStatR | LPC_GPIOINT->IO0IntStatF);
    LPC_GPIOINT->IO2IntClr = (LPC_GPIOINT->IO2IntStatR | LPC_GPIOINT->IO2IntStatF);
    Potmeter.rise(&Measure); //if P5 high, then measure
    while (1) {
        led = !led;
        wait(0.25);
        pc.printf("say something");
    }
}

Having had a look at your program may I suggest you have a read of my article about interrupts and callbacks.

regards, Andy

21 Feb 2011

Hi andy,

It looks like it does the job, i've read the article, very interesting. What does it exactly do?

marcel

21 Feb 2011

Hi Marcel,

This is odd. Those two lines clear any potential pending GPIO interrupt before you attach the .rise() function. This was a known bug but was fixed in a library update. Can you make sure you are using the latest version of the Mbed library. Those lines really shouldn't be needed if you are using version27 or beyond (see this forum post for more details).

21 Feb 2011

Hi,

I just checked it, i'm using version 28. But i'm seeing now that i use these lines in in another code, and that i'm using a new microswitch. I will check on both things, and let you know how its going.

21 Feb 2011

Hi,

Do you have something like a pull-down or pull-up resistor connected to guarantee a defined level on the input pin you use to generate the interrupt with?

22 Feb 2011

Jeroen,

I did not, but i attached one now, and its seem to work fine.