简体   繁体   English

STM32L0 GPIO中断不起作用

[英]STM32L0 GPIO Interrupt does not work

I am trying to blink the led on GPIO PA5 port, every time when PC13 Button is clicked. 每次单击PC13按钮时,我都试图使GPIO PA5端口上的led闪烁。 However, it does not work. 但是,它不起作用。 Could you please advice, how can i solve the problem? 您能否请教,我该如何解决这个问题?

main.c - main program main.c-主程序

#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. GPIO端口配置部分。 PA5 and PC13 ports are configured. PA5和PC13端口已配置。 Interrupt on EXTI13 enabled. 启用EXTI13中断。

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. stm32l0xx_it.c-中断文件。 IRQ handler and Callback function defined. IRQ处理程序和回调函数已定义。

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 . 如果要在每个按钮事件中更改LED的状态,则无需在HAL_GPIO_EXTI_Callback中设置延迟。 It's not a good practice in firmware development. 在固件开发中,这不是一个好习惯。 IRQs are supposed to manage events quickly. IRQ应该可以快速管理事件。 Their processes have a higher priority than the program execution (here, your main). 他们的进程比程序执行(这里是您的主程序)具有更高的优先级。

    \nvoid HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) 无效HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)\n{ {\n    if(GPIO_Pin == GPIO_PIN_13) if(GPIO_Pin == GPIO_PIN_13)\n    { {\n        HAL_GPIO_TogglePin (GPIOA, GPIO_PIN_5); 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). 如果要根据按钮事件开始/停止LED闪烁,则需要使用一个计时器(例如,按钮启动一个计时器,每个计时器经过 irq会切换PA5)。

In MX_GPIO_Init function, you have to call HAL_GPIO_WritePin after the PA5 initialization ( HAL_GPIO_Init ). MX_GPIO_Init函数中,必须在PA5初始化( HAL_GPIO_Init )之后调用HAL_GPIO_WritePin

Take a look to your hardware before setting a pullup on PA13. 在PA13上设置上拉电阻之前,请先查看一下您的硬件。

I advise you to download STM32Cube . 我建议您下载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 ). 示例显示了如何配置外部中断线以在按钮事件发生时使LED闪烁(存储库路径: ... \\ 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. 除非将exti irq的优先级更改为高于systick irq的优先级,否则HAL_Delay()将不起作用。 In your implementation, I assume, default priorities are 0 for both and HAL_Delay() hangs because you use it in same isr priority. 在您的实现中,我假设默认优先级都为0,并且HAL_Delay()挂起,因为您以相同的isr优先级使用它。 Try to change exti irq priority to 1. 尝试将外部优先级更改为1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM