cancel
Showing results for 
Search instead for 
Did you mean: 

I have a problem with EXTI. I have connected switch with EXTI pin. When the switch is pressed first time it light up the led. But when I pressed the same switch second time it does not light up the led.

cjaya.1
Associate II
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
       if(GPIO_Pin == GPIO_PIN_10)
          {
          Switch10 = 1;
          HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);
          }
       
      if(GPIO_PIN == GPIO_PIN_11)
       {
       Swtich10 = 0;
       HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET);
        }
}
 
int main (void)
{
      while(1)
          {
           if(Switch10)
              {
              Test();             
              }  
          }
 
}

5 REPLIES 5

The code as presented is incomplete, and unhelpful for diagnostics

You're turning it off with a different pin.

Show pin and exti initialization code.

Show IRQHandler code.

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

Yes, I forget to mention I have second switch to turn off the same LED.

void EXTI15_10_IRQHandler(void)
{
   HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_10);
   HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_11);
}
 
void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
{
   if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
     {
     __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
     HAL_GPIO_EXTI_Callback(GPIO_Pin); 
     }
}
 
static void MX_GPIO_Init(void)
{
  GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
 HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
 HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
 
}

Yes, I forget to mention I have second switch to turn off the same LED.

    void EXTI15_10_IRQHandler(void)
    {
       HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_10);
       HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_11);
    }
     
    void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
    {
       if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
         {
         __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
         HAL_GPIO_EXTI_Callback(GPIO_Pin); 
         }
    }
     
    static void MX_GPIO_Init(void)
    {
      GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
      GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
      GPIO_InitStruct.Pull = GPIO_PULLDOWN;
      HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
     
     HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
     HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
     
    }
 
 

I have initialized static int Switch10. And found out that in real time debugging the static int Switch10 increment as counter it does not stay as static.I think, thats why it does not alllow for second time press to turn on the LED. Any comment about this?. Thank you

Karl Yamashita
Lead II

There is probably something happening in the background that you're not showing us in code. We don't know what you're doing in Test function? You've have a spelling mistake in the the HAL_GPIO_EXTI_Callback so that means you're not copying/pasting the code as is.

If you find my answers useful, click the accept button so that way others can see the solution.
S.Ma
Principal

A push button has decounce to counteract by HW or SW.

Want sometime rugged? Activate the EXTI interrupt after clearing the PR bits from main loop (if bare metal obviously) and make the callback self interrupt disable it. Ping pong it with some delay to pass the debounce period. It has the other benefit not to put the operating application to a grinding halt with a DDOS style attack by injecting MHz square wave to the EXTI interrupt pin.

Example code here for STM32C0316 (IRQ_SelfSettle.c)