2025-08-02 8:42 AM
I try to create code, which flashes LED2 ( few flashes , not one toggle ) in response of one press of Blue button.
What could be wrong in my code ?
Selected two pins PA5 ( input for LED2 ) and PC13 (EXTI for Blue button with External interrupt Mode with Rising edge trigger detection + no pull up no pull down ) NVIC EXTI line checkbox - marked.
And Cube IDE generated code with EXTI callback function etc.
In my main.c file, I added this code only:
/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
/* Prevent unused argument(s) compilation warning */
UNUSED(GPIO_Pin);
if (GPIO_Pin == GPIO_PIN_13) // If The INT Source Is EXTI Line9 (A9 Pin)
{
HAL_NVIC_DisableIRQ(EXTI15_10_IRQn); // Disable EXTI interrupt
Flash_LED2();
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); // Re-enable EXTI interrupt
}
}
void Flash_LED2(void) {
//HAL_NVIC_DisableIRQ(EXTI15_10_IRQn); // Disable EXTI interrupt
for (int i = 0; i < 7; i++) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
HAL_Delay(100);
}
HAL_Delay(1500);
//HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); // Re-enable EXTI interrupt
}
/* USER CODE END 4 */
What could be wrong as on pressing Blue button, LED2 is not flashing few times ?
2025-08-02 8:56 AM
If you use HAL_Delay within an interrupt, you need to ensure systick has a higher (numerically lower) priority than the interrupt it's in.
It's generally better not to have blocking code within an interrupt. Instead, set a flag and handle it in the main loop.