cancel
Showing results for 
Search instead for 
Did you mean: 

I cannot get my EXTI to function properly with a flashing LED

james99maher
Associate II
Posted on October 23, 2016 at 16:12

Hi there. I am using the STM32VLDISCOVERY board with Atollic Studio.

I'm pretty new to programming, so getting any code to run can be a bit of a challenge for me! I am trying to implement a program which will cause an LED to flash when I press a button, and then stop flashing when I press the button again.  I can get the LED to flash when I press the button, but I cannot stop it flashing when I press the button again. Previously, I was able to get this program to work when I only wanted to turn the LED on and off, so I figure that the loop I'm using won't detect the change in status of the push button.

I know there isn't any issue with the initialization of the push button and LED, so I'll just post the code I'm using in the EXTI block. 

The following is located in stm32f1xx_it.c 

void EXTI0_IRQHandler(void)

{

if(EXTI_GetITStatus(EXTI_Line0) != RESET)

{

if (GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_9)==0)

{

for(;;)

{

/* Turn ON LED */

GPIO_SetBits(GPIOC, GPIO_Pin_9);

/* Delay */

for(int i = 0; i<=200000 ; i++);

/* Turn OFF LED */

GPIO_ResetBits(GPIOC, GPIO_Pin_9);

/* Delay */

for(int i = 0; i<=200000 ; i++);

/* Clear the User Button EXTI line pending bit */

EXTI_ClearITPendingBit(EXTI_Line0);

}

}

}

else

{

/* Turn OFF LED */

GPIO_ResetBits(GPIOC, GPIO_Pin_9);

/* Clear the User Button EXTI line pending bit */

EXTI_ClearITPendingBit(EXTI_Line0);

}

}

I've tried implementing using a few other methods, but nothing seems to be working for me. Any small help would be greatly appreciated. Thanks!

#exti #button #stm32 #interrupt
3 REPLIES 3
Posted on October 23, 2016 at 16:43

How's it supposed to get down the ELSE path to turn off the LED?

It won't clear the interrupt if PC9 is high. Clear the interrupt immediately after you have confirmed it is the source.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
james99maher
Associate II
Posted on October 23, 2016 at 17:02

I don't fully understand... Do I put

EXTI_ClearITPendingBit(EXTI_Line0); 

directly after 

if (GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_9)==0)

Or does it go somewhere else?

Posted on October 24, 2016 at 00:03

Your IF/THEN/ELSE bracing doesn't do what you want it to do, review.

I'd propose the following form, where you put you LED logic INSIDE this compound statement

void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET) /* Validate source */
{
/* Clear the User Button EXTI line pending bit */
EXTI_ClearITPendingBit(EXTI_Line0);
// ... GPIO code inside here
}
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..