cancel
Showing results for 
Search instead for 
Did you mean: 

External Interrupt does not turn led on on HAL library

rowellsa
Associate

Hello, i am trying to run a simple code that toggles the built-in led when i press the built-in user button. I wanted to do this manually using HAL library functions, but for some reason i do not seem to get it right somewhere. The code generated using CubeMX software works, but when i try to code it on my own, i cannot turn the led on. This is the code, and it already is very similar to the code that mx generated. Is there a library or a function that i'm missing?

 

 

#include "stm32g4xx.h" #include "stm32g4xx_hal.h" int main(){ GPIO_InitTypeDef GPIO_InitStruct = {0}; HAL_Init(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; GPIO_InitStruct.Pull = GPIO_PULLDOWN; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_5; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); while(1){ void EXTI15_10_IRQHandler(void) { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_12); HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13); } void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if(GPIO_Pin == GPIO_PIN_13) HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, SET); else if(GPIO_Pin == GPIO_PIN_12) HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, RESET); } } }
View more

 

 

3 REPLIES 3
Karl Yamashita
Principal

Why do you have EXTI15_10_IRQHandler inside the while loop? 

while(1){ void EXTI15_10_IRQHandler(void) { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_12); HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13); }

That is already in stm32xxxx_it.c file.

 

Did you enable the NVIC for that GPIO?

KarlYamashita_0-1719605466499.png

 

 

Don't worry, I won't byte.
TimerCallback tutorial! | UART and DMA Idle tutorial!

If you find my solution useful, please click the Accept as Solution so others see the solution.
Karl Yamashita
Principal

In your HAL_GPIO_EXTI_Callback you have no debounce routine. See this YouTube video to learn how to debounce a button using interrupt. https://www.youtube.com/watch?v=o0qhmXR5LD0

Don't worry, I won't byte.
TimerCallback tutorial! | UART and DMA Idle tutorial!

If you find my solution useful, please click the Accept as Solution so others see the solution.
Pavel A.
Evangelist III

 a simple code that toggles the built-in led when i press the built-in user button.

Nothing is too simple for a beginner. So let's look at your code again.

* The code probably does not compile as is because there are nested functions? You cannot run the program unless it compiles without errors.

* Why two interrupts on pins 12 and 13? You wrote about one "built-in user button". Is it a Nucleo board? What is another interrupt for?