interrupts using pushbutton

29 Oct 2010

Hello,

I have a question about interrupts. I have a pushbutton connected at p6

and set it so that it lights up led1 whenever I press it. The program compiles fine since the status led (LED4)

is on. However the interrupt routine doesn’t work. Can somebody point what am I missing.

#include "mbed.h"

#define LED1	(1 << 18)
//#define LED2	(1 << 20)
//#define LED3	(1 << 21)
#define LED4	(1 << 23)

#define BUTTON	(1 << 8)

int main(void) {

//set led 1 and 2 pins as output
    LPC_GPIO1->FIODIR |= LED1 | LED4;
//set button as input
    LPC_GPIO0->FIODIR &= ~BUTTON;

    LPC_GPIO1->FIOSET |= LED4;
    while (1) {

    }
}


// Setup External Interrupt
void EINT3_init (void) {
    // Set PINSEL0 ( p6) for P0.8 as gpio
    LPC_PINCON->PINSEL0 &= ~(1 << 8);

    //set the interrupt for 0.8 for falling edge
    LPC_GPIOINT->IO0IntEnF |= (1<<8);

    NVIC_EnableIRQ(EINT3_IRQn );
}

//EINT3 Handler

void EINT3_IRQHandler(void) {
    //clear interrupt
    LPC_GPIOINT->IO0IntClr = (1<<8);

    //toggle  led
    LPC_GPIO1->FIOSET |= LED1;

}

29 Oct 2010

Define the interrupt function thus:-

void EINT3_IRQHandler(void) __irq {

It's location doesn't matter so long as it's called "EINT3_IRQHandler", the linker sorts that out. What I don't see is you calling EINT3_init() to actually enable the interrupt.

 

29 Oct 2010

whoops, I mean

extern "C" void EINT3_IRQHandler(void) __irq {
29 Oct 2010

Oh, and one more thing. You'll soon find that with mechanical switches you'll get "bounce", rather more interrupts than you may like.

Take a look at DebounceIn. It's wasy to modify it to do what you want. In the class there is a "shadow int". When that gets set, switch your LED on there

29 Oct 2010

Christian,

I place my extern "C" ISR functions at the end of my CPP files, outside my libraries namespace. Works every time.

29 Oct 2010

Thanks, but still it doesn't work. I mean it compiles fine. I have connected the pushbutton to vout(3.3) and pin 6. Led1 should go of if i push on it but nothing happens. I 've been messing for the past two hours with this.

Dimiter

29 Oct 2010

The inputs are "pullup" by default. Connect the button to ground and look for a falling edge. Or remove the pullup input mode.

29 Oct 2010

Funny,  It still doesn't work. I connected it to ground.

#include "mbed.h"

#define LED1	(1 << 18)
//#define LED2	(1 << 20)
//#define LED3	(1 << 21)
#define LED4	(1 << 23)

#define BUTTON	(1 << 8)

// Setup External Interrupt
void EINT3_init (void) {
    // Set PINSEL0 ( p6) for P0.8 as gpio
    LPC_PINCON->PINSEL0 &= ~(1 << 8);

    //set the interrupt for 0.8 for falling edge
    LPC_GPIOINT->IO0IntEnF |= (1<<8);

    NVIC_EnableIRQ(EINT3_IRQn );
}

//EINT3 Handler

extern "C" void EINT3_IRQHandler(void) __irq {

    //clear interrupt
    LPC_GPIOINT->IO0IntClr = (1<<8);

    //toggle  led
    LPC_GPIO1->FIOSET ^= LED1;

}



int main(void) {

//set led 1 and 2 pins as output
    LPC_GPIO1->FIODIR |= LED1 | LED4;
//set button as input
    LPC_GPIO0->FIODIR &= ~BUTTON;

    LPC_GPIO1->FIOSET |= LED4;
    while (1) {

    }
}
This is the current state of the program, button connected at p6.

29 Oct 2010

I still dont see you calling EINT_init() in main() before you hit teh while loop. If you don't enable them they won't happen by magic!

 

int main(void) {

//set led 1 and 2 pins as output
    LPC_GPIO1->FIODIR |= LED1 | LED4;
//set button as input
    LPC_GPIO0->FIODIR &= ~BUTTON;

    LPC_GPIO1->FIOSET |= LED4;      
 EINT3_init(); // <------ put this in!
    while (1) {

    }
}
29 Oct 2010 . Edited: 29 Oct 2010

Ok noobie mistake, I got it now .

thanks for the help

Dimiter

29 Oct 2010

Btw, if you are planning on doing things the LPC17xx.h way rather than using the Mbed libraries then you may find this useful

http://mbed.org/users/AjK/libraries/SimpleIOMacros

Makes reading, setting and clearing GPIO pins a lot simpler.

29 Oct 2010

Awsome , that is really helpful.

I am assuming that if you write an app class for a keyboard for example the overhead from using these macros would be less than using the mbed libraries.

29 Oct 2010

My FastIO classes provide interface similar to mbed's but expand to single-instruction code at compile time.

29 Oct 2010

Swings and roundabouts, it depends how you are going to build your app. If it's really simple app then macros are simple to use. But as you move on and start to build your own libraries you find macros harder to use. I tend to use my macro library for "quick and dirty debugging" or knocking up simple test programs.

But when building "parts" and "components", I tend to use my own libs and/or the Mbed ones.

If you are are new to Mbed I'd suggest playing around with the Mbed libs first. They are pretty simple to get most things going quickly.

30 Oct 2010

Hi All,

Just for future reference in this thread and to avoid complexity confusion for other readers, I thought i'd also post how you achieve the same thing using the mbed libraries:

// example of using InterruptIn
// - connect p6 to 3.3v to trigger an interrupt

#include "mbed.h"

DigitalOut led1(LED1);
DigitalOut led4(LED4);
InterruptIn button(p6);

void button_handler() {
    led1 = !led1;
}

int main(void) {
    button.rise(&button_handler);
    while (1) {
        led4 = !led4;
        wait(0.25);
    }
} 

The full details at on the InterruptIn handbook page.

You could also mix and match; use the DigitalOut of the mbed library to control the LEDs, and the register-poking to setup and handle the interrupt. All depends on what you are trying to achieve.

Glad you got it working!

Simon