cancel
Showing results for 
Search instead for 
Did you mean: 

toggle led with delay inside exti interruption

khouja_houssem
Associate II
Posted on March 18, 2013 at 20:53

Hi,

I am trying to toggle a led inside an EXTI interruption with Delay. The delay function is based on the systick intrruption. 

Everything works fine seperately but when I try to use the delay inside the EXTI interruption, nothing works anymore. I think the problem is on the perioritys of the interruptions. Here is the EXTI code, I hope someone can help me through:

void EXTI0_IRQHandler(void)

{

  if (EXTI_GetITStatus(EXTI_Line0) != RESET)

  {

    /* Toggle LED3; we suggest to use  function */

GPIO_ToggleBits  ( GPIOD,GPIO_Pin_12);

Delay(4000);  //4 seconds

GPIO_ToggleBits  ( GPIOD,GPIO_Pin_12);

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

      EXTI_ClearITPendingBit(EXTI_Line0);

  }

}
4 REPLIES 4
Posted on March 18, 2013 at 21:07

Yeah, you'd want to make sure that the SysTick interrupt had a higher priority, and could preempt the EXTI one.

A smarter move would be to toggle the LED once in the EXTI interrupt, and have the SysTick one toggle it again 4000 interrupts later? Perhaps via a down counter.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on March 18, 2013 at 21:15

You don't want to dwell in an interrupt.

volatile int LEDToggle = 0;
void EXTI0_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line0) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line0); /* Clear the User Button EXTI line pending bit */
if (LEDToggle == 0)
{ 
GPIO_ToggleBits ( GPIOD,GPIO_Pin_12);
LEDToggle = 4000; //4 seconds
}
}
}
void SysTick_Handler(void)
{
if (LEDToggle != 0) // Countdown active?
{
LEDToggle--; // Decrement
if (LEDToggle == 0) // Expired
GPIO_ToggleBits( GPIOD,GPIO_Pin_12);
}
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
khouja_houssem
Associate II
Posted on March 18, 2013 at 21:24

Goode Idea but I still want to know if the systick interrupt can preempt the EXTI one.

My program stops without decrementing the counter of the delay, Does this mean that the systick interrupt cant preempt the EXTI one.
Posted on March 18, 2013 at 21:50

Indeed, an that's why you'd use a better solution not dependent on external behaviour, either by changing the paradigm as suggested or waiting against a free running hardware resource like DWT_CYCCNT.

3. You can change the SysTick IRQ priority by calling the NVIC_SetPriority(SysTick_IRQn,...) just after the SysTick_Config() function call. The NVIC_SetPriority() is defined inside the core_cm3.h file.  

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