小编Sam*_*bal的帖子

STM32F401RE-Nucleo LED 和 BUTTON 的示例

我正在尝试在我的新 Nucleo 板上运行一个简单的程序,

我编写这个程序是为了在按下用户按钮(蓝色按钮)时打开和关闭绿色 LED,

#include "stm32f4xx.h"
#include "stm32f4xx_nucleo.h"
#include "system_stm32f4xx.h"
#include "stm32f4xx_hal_gpio.h"
#include "stm32f4xx_hal_rcc.h"

GPIO_InitTypeDef GPIO_InitStructure; 

int main(void) {

    HAL_Init(); 

    __GPIOA_CLK_ENABLE();
    GPIO_InitStructure.Pin   = GPIO_PIN_5;
    GPIO_InitStructure.Mode  = GPIO_MODE_OUTPUT_PP;    
    GPIO_InitStructure.Pull  = GPIO_PULLUP;
    GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;  
    HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);    

    __GPIOC_CLK_ENABLE();
    GPIO_InitStructure.Pin   = GPIO_PIN_13;
    GPIO_InitStructure.Mode  = GPIO_MODE_INPUT;
    GPIO_InitStructure.Pull  = GPIO_PULLDOWN;
    GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);

    while (1) {
        if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13)) {         
            HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); 
        } else {
            HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但我得到错误的结果:

  • 当我按下用户的按钮时,什么也没有发生,
  • 当我一直按下用户的按钮并重新启动(通过按下黑色按钮)时,我得到了很好的结果,

好像程序只在第一次运行时检查蓝色按钮的状态,我不知道为什么,

如果您对问题的根源有任何想法,或者您有一个正在运行的程序可以切换 LED 按钮按下,请帮助我。

stm32

1
推荐指数
1
解决办法
4万
查看次数

标签 统计

stm32 ×1