11 years, 10 months ago.

GPIO Interrupt Troubles

Following the guide I found here (http://anhnvnguyen.blogspot.com/2010/04/lpc17xx-gpio-basic_05.html), I've written a short snippet of code that is supposed to blink LED1 on the mbed LPC1768 on startup and then blink LED2 on any rising or falling edge of input on pin 17 (p0.25). The goal here is just to test the GPIO interrupt. Unfortunately, on startup, LED1 blinks once like it's supposed to, but the interrupt is never called. Is there something simple I am overlooking?

Thanks!

Code:

#include "LPC17xx.h"

#define GPIO_INT (1 << 25)        // GPIO interrupt is on p0.25
#define LED1     (1 << 18)        // LED1 is on p1.18
#define LED2     (1 << 20)        // LED2 is on p1.20
#define LED3     (1 << 21)        // LED3 is on p1.21
#define LED4     (1 << 23)        // LED4 is on p1.23

extern "C" void EINT3_IRQHandler(void);
void delay(void);

int main() {
    LPC_GPIO0->FIODIR &= ~GPIO_INT;                 // GPIO interrupt pin is an INPUT
    LPC_GPIO1->FIODIR |= LED1 | LED2 | LED3| LED4;  // LED pins are OUTPUTS
    
    LPC_GPIO1->FIOSET = LED1;   // Flash LED1 at program start
    delay();
    LPC_GPIO1->FIOCLR = LED1;
    delay();
    
    LPC_GPIOINT->IO0IntEnF |= GPIO_INT;     // Set GPIO falling interrupt
    LPC_GPIOINT->IO0IntEnR |= GPIO_INT;     // Set GPIO rising interrupt
    NVIC_EnableIRQ(EINT3_IRQn);             // Enable interrupt handler
    
    while(1);
}

extern "C" void EINT3_IRQHandler() {   
    LPC_GPIO1->FIOSET = LED2;   // Flash LED1 when interrupt is called
    delay();
    LPC_GPIO1->FIOCLR = LED2;
    delay();
    LPC_GPIOINT->IO0IntClr |= GPIO_INT;     // Clear the interrupt flag
    return;
}

void delay (void) {     // Hardcoded delay for flashing LEDs
    for (uint32_t i = 0; i < 9600000; i++) {}
}

2 Answers

10 years ago.

Clear the interrupt flag in the beginning of the ISR. Not in the end it causes spooky behaviour.

This is working code for LED1 and a button on dip5 or P0.9

title

/*
===============================================================================
 Name        : main.c
 Author      : $(author)
 Version     :
 Copyright   : $(copyright)
 Description : main definition
===============================================================================
*/
#include "LPC17xx.h"
#define GPIO_INT (1 << 9)        // GPIO interrupt is on p0.9
#define LED1     (1 << 18)        // LED1 is on p1.18
#define LED2     (1 << 20)        // LED2 is on p1.20
#define LED3     (1 << 21)        // LED3 is on p1.21
#define LED4     (1 << 23)        // LED4 is on p1.23
extern "C"{
void EINT3_IRQHandler(void){
  LPC_GPIOINT->IO0IntClr=(1 << 9);
if (LPC_GPIO1->FIOPIN & (1 << 18))
	{
		LPC_GPIO1->FIOCLR |= (1 << 18);
	}
	else
	{
		LPC_GPIO1->FIOSET |= (1 << 18);
	}
}
}
int main(void)
{
 LPC_GPIO0->FIODIR &= ~GPIO_INT;
 LPC_GPIO1->FIODIR|= LED1;
 //Enable interrupt
 LPC_GPIOINT->IO0IntEnR |=(1<<9);
 NVIC_EnableIRQ(EINT3_IRQn);
}

Thank you! This helped answer my very similar question. Another thing to check is that you've got your on pin pull-up/down resistors configured correctly; this can be verified by testing the logic in a non-interrupt-based polling loop.

posted by Jason Heeris 14 Jun 2016
10 years ago.

Also remove all the delays in your ISR.

Regards!