stm32 为什么加了消抖后LED就没反应了,不加消抖的话直接翻转的话是可以的。

关注让 “紫雾凌寒” 来帮你回答,本回答结合 DeepSeek
如果还有问题,请评论或点击头像私信,如果有帮助到您, 请关注、采纳、点赞
#include "stm32f10x.h"
#define LED_GPIO_PORT GPIOC
#define LED_GPIO_PIN GPIO_Pin_13
void delay(uint32_t count) {
for(volatile uint32_t i = 0; i < count; i++);
}
void GPIO_Configuration() {
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = LED_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStructure);
}
int main(void) {
GPIO_Configuration();
while(1) {
// 假设按键连接到某个GPIO口,这里用按键消抖举例
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_X; // 假设按键连接到GPIO_Pin_X
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // 上拉输入
GPIO_Init(GPIO_PORT_X, &GPIO_InitStructure);
if(GPIO_ReadInputDataBit(GPIO_PORT_X, GPIO_Pin_X) == 0) {
delay(50000); // 消抖延时
if(GPIO_ReadInputDataBit(GPIO_PORT_X, GPIO_Pin_X) ==
0) {
GPIO_SetBits(LED_GPIO_PORT, LED_GPIO_PIN);
}
} else {
GPIO_ResetBits(LED_GPIO_PORT, LED_GPIO_PIN);
}
}
}
希望以上解答对您有所帮助。如果您有任何疑问,欢迎在评论区提出。