流水灯按键控制正反stm32
时间: 2024-01-04 09:04:02 浏览: 95
以下是使用STM32按键控制流水灯正反变换的示例代码:
```c
#include "stm32f10x.h"
#define LED_GPIO_PORT GPIOA
#define LED_GPIO_PIN GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3
#define BUTTON_GPIO_PORT GPIOC
#define BUTTON_GPIO_PIN GPIO_Pin_13
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 配置LED引脚为推挽输出
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);
// 配置按键引脚为上拉输入
GPIO_InitStructure.GPIO_Pin = BUTTON_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(BUTTON_GPIO_PORT, &GPIO_InitStructure);}
int main(void)
{
GPIO_Configuration();
while (1)
{
// 检测按键是否按下
if (GPIO_ReadInputDataBit(BUTTON_GPIO_PORT, BUTTON_GPIO_PIN) == 0)
{
// 正向流水灯
GPIO_SetBits(LED_GPIO_PORT, LED_GPIO_PIN);
delay(2000); // 延时2秒
// 反向流水灯
GPIO_ResetBits(LED_GPIO_PORT, LED_GPIO_PIN);
delay(2000); // 延时2秒
// 全亮
GPIO_SetBits(LED_GPIO_PORT, LED_GPIO_PIN);
delay(2000); // 延时2秒
// 循环
GPIO_ResetBits(LED_GPIO_PORT, LED_GPIO_PIN);
}
}
}
```
请注意,上述代码仅为示例,具体的引脚配置和延时函数需要根据实际情况进行修改。
阅读全文
相关推荐














