stm32f401key1按键控制流水灯,key2控制呼吸灯,key3控制跑马灯
时间: 2023-09-19 11:13:26 浏览: 181
首先,你需要连接按键和 LED 灯到 STM32F401RE 开发板上。
然后,在编写代码之前,你需要配置 GPIO 引脚作为输入(按键)或输出(LED 灯)。你可以使用 HAL 库来配置 GPIO 引脚。
接下来,你需要编写代码来检测按键状态,并根据按键状态来控制 LED 灯的状态。
以下是一个实现流水灯、呼吸灯和跑马灯的示例代码:
```c
#include "stm32f4xx.h"
#include "stm32f4xx_hal.h"
#define LED1_Pin GPIO_PIN_5
#define LED1_GPIO_Port GPIOA
#define LED2_Pin GPIO_PIN_6
#define LED2_GPIO_Port GPIOA
#define LED3_Pin GPIO_PIN_7
#define LED3_GPIO_Port GPIOA
#define KEY1_Pin GPIO_PIN_0
#define KEY1_GPIO_Port GPIOA
#define KEY2_Pin GPIO_PIN_1
#define KEY2_GPIO_Port GPIOA
#define KEY3_Pin GPIO_PIN_2
#define KEY3_GPIO_Port GPIOA
#define DELAY_MS 10
void delay_ms(uint32_t ms)
{
HAL_Delay(ms);
}
void led_on(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
HAL_GPIO_WritePin(GPIOx, GPIO_Pin, GPIO_PIN_SET);
}
void led_off(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
HAL_GPIO_WritePin(GPIOx, GPIO_Pin, GPIO_PIN_RESET);
}
void led_toggle(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
HAL_GPIO_TogglePin(GPIOx, GPIO_Pin);
}
int main(void)
{
HAL_Init();
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Pin = LED1_Pin | LED2_Pin | LED3_Pin;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Pin = KEY1_Pin | KEY2_Pin | KEY3_Pin;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
uint32_t counter = 0;
uint32_t delay_counter = 0;
uint32_t brightness_counter = 0;
uint32_t brightness = 0;
uint32_t direction = 1;
uint32_t last_key1_state = 1;
uint32_t last_key2_state = 1;
uint32_t last_key3_state = 1;
while (1)
{
// 处理按键
uint32_t key1_state = HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin);
uint32_t key2_state = HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin);
uint32_t key3_state = HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin);
if (key1_state == 0 && last_key1_state == 1)
{
// 按下 KEY1,启动流水灯
counter = 0;
led_off(LED1_GPIO_Port, LED1_Pin);
led_on(LED2_GPIO_Port, LED2_Pin);
led_off(LED3_GPIO_Port, LED3_Pin);
}
else if (key2_state == 0 && last_key2_state == 1)
{
// 按下 KEY2,启动呼吸灯
counter = 0;
brightness_counter = 0;
direction = 1;
brightness = 0;
led_on(LED1_GPIO_Port, LED1_Pin);
led_off(LED2_GPIO_Port, LED2_Pin);
led_off(LED3_GPIO_Port, LED3_Pin);
}
else if (key3_state == 0 && last_key3_state == 1)
{
// 按下 KEY3,启动跑马灯
counter = 0;
led_on(LED1_GPIO_Port, LED1_Pin);
led_off(LED2_GPIO_Port, LED2_Pin);
led_on(LED3_GPIO_Port, LED3_Pin);
}
last_key1_state = key1_state;
last_key2_state = key2_state;
last_key3_state = key3_state;
// 处理 LED 灯
if (counter % 50 == 0)
{
if (HAL_GPIO_ReadPin(LED1_GPIO_Port, LED1_Pin) == GPIO_PIN_SET)
{
led_toggle(LED1_GPIO_Port, LED1_Pin);
led_toggle(LED2_GPIO_Port, LED2_Pin);
led_toggle(LED3_GPIO_Port, LED3_Pin);
}
}
if (counter % 10 == 0 && HAL_GPIO_ReadPin(LED1_GPIO_Port, LED1_Pin) == GPIO_PIN_SET)
{
// 呼吸灯
if (direction == 1)
{
brightness += 5;
if (brightness >= 100)
{
direction = 0;
}
}
else
{
brightness -= 5;
if (brightness <= 0)
{
direction = 1;
}
}
if (brightness_counter < brightness)
{
led_on(LED1_GPIO_Port, LED1_Pin);
}
else
{
led_off(LED1_GPIO_Port, LED1_Pin);
}
brightness_counter++;
if (brightness_counter == 100)
{
brightness_counter = 0;
}
}
if (counter % 50 == 0 && HAL_GPIO_ReadPin(LED1_GPIO_Port, LED1_Pin) == GPIO_PIN_SET)
{
// 跑马灯
led_toggle(LED1_GPIO_Port, LED1_Pin);
led_toggle(LED3_GPIO_Port, LED3_Pin);
}
counter++;
delay_ms(DELAY_MS);
}
}
```
这个例子中,我们使用了 3 个 LED 灯和 3 个按键。当按下不同的按键时,会启动不同的灯效。流水灯会让三个灯在不同的时间点亮起来;呼吸灯会让 LED1 呈现呼吸灯效果;跑马灯会让 LED1 和 LED3 交替闪烁。
这个例子并不是最优的实现,你可以根据自己的需求进行改进。
阅读全文