I am trying to blink the led on GPIO PA5 port, every time when PC13 Button is clicked. However, it does not work. Could you please advice, how can i solve the problem?
main.c - main program
#include "main.h"
#include "stm32l0xx_hal.h"
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
while (1)
{
}
}
GPIO port conficuration section. PA5 and PC13 ports are configured. Interrupt on EXTI13 enabled.
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
/*Configure GPIO pin : PC13 */
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pin : PA5 */
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);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
}
stm32l0xx_it.c - interrupt file. IRQ handler and Callback function defined.
void EXTI4_15_IRQHandler(void)
{
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13);
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
HAL_Delay(500);
}
Best regards,
I don't understand what you want to do.
If you want to change the state of your LED on each push button event, you don't need to put a delay in the HAL_GPIO_EXTI_Callback . It's not a good practice in firmware development. IRQs are supposed to manage events quickly. Their processes have a higher priority than the program execution (here, your main).
\nvoid HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) \n{ \n if(GPIO_Pin == GPIO_PIN_13) \n { \n HAL_GPIO_TogglePin (GPIOA, GPIO_PIN_5); \n } \n} \n
If you want to start/stop blinking your LED based on push button events, you need to use a timer (eg the button start a timer, each timer elapsed irq toggles PA5).
In MX_GPIO_Init function, you have to call HAL_GPIO_WritePin after the PA5 initialization ( HAL_GPIO_Init ).
Take a look to your hardware before setting a pullup on PA13.
I advise you to download STM32Cube . They are lot of code samples. An example shows how to configure external interrupt lines to blink a LED on button events (repository path: ...\\STM32Cube\\Repository\\STM32Cube_FW_L0_V1.8.0\\Projects\\STM32L073RZ-Nucleo\\Examples\\GPIO\\GPIO_EXTI ).
HAL_Delay() will not work until you change priority of exti irq to be higher then priority of systick irq. In your implementation, I assume, default priorities are 0 for both and HAL_Delay()
hangs because you use it in same isr priority. Try to change exti irq priority to 1.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.