我刚开始学习如何使用 Nucleo 板进行编程,并决定做一个简单的任务。当我按下我的用户按钮时,我希望我的 LED 打开(开始时关闭)。我有以下问题,我不太确定答案是什么。我想知道它是特定于STM32还是我不明白的微妙之处。
我正在运行以下代码。我的疑问是,目前我的 LED 总是打开的,只有当我按下用户按钮时它才会熄灭。当我按下按钮时,我的 PinState = 0 的值对我来说没有意义。我认为它应该是 1。
先感谢您
#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);
int PinState;
while (1)
{
PinState = HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13);
if(PinState == 1){
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5, GPIO_PIN_SET);
}
else {
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5, GPIO_PIN_RESET);
}
}
}