写一段STM32F103CBT6的长按3S开机,短按循环切换红黄绿灯,双击循环切换蓝白灯,长按2S关机的程序
时间: 2024-10-25 08:09:55 浏览: 36
这是一个基于STM32F103微控制器的基本控制程序,使用GPIO模块来操作LED灯,并利用中断机制处理按键的长按和短按事件。以下是简化的伪代码示例:
```c
#include "stm32f10x.h"
#define LED_R GPIO_Pin_2 // 红色LED
#define LED_Y GPIO_Pin_3 // 黄色LED
#define LED_G GPIO_Pin_4 // 绿色LED
#define LED_B GPIO_Pin_5 // 蓝色LED
#define LED_W GPIO_Pin_6 // 白色LED
#define KEY_PIN GPIO_Pin_7 // 按键输入
#define KEY_DOWN_LEVEL 0 // 下拉状态
// 定义延时常数
#define LONG_PRESS_DELAY 3000 // 长按3秒
#define SHORT_PRESS_DELAY 500 // 短按500毫秒
#define DOUBLE_CLICK_DELAY 100 // 双击间隔时间
volatile uint32_t long_press_count = 0;
volatile bool short_press_flag = false;
void led_state(uint8_t led) {
GPIO_SetBits(LED_PORT, led); // 开启指定颜色LED
HAL_Delay(SHORT_PRESS_DELAY);
GPIO_ResetBits(LED_PORT, led); // 关闭LED
}
void EXTI_IRQHandler() {
if (EXTI_GetITStatus(KEY_PIN_EXTI_LINE) != RESET) { // 判断是否有按键按下
EXTI_ClearITPendingBit(KEY_PIN_EXTI_LINE);
if (GPIO_ReadPin(KEY_PIN) == KEY_DOWN_LEVEL) { // 检测按键状态
if (long_press_count > 0) {
// 长按处理:关闭所有灯并等待释放
long_press_count--;
if (long_press_count == 0) {
GPIO_ResetBits(GPIOA, LED_ALL); // 关闭所有LED
// 清零计数并退出长按模式
long_press_count = 0;
}
} else if (!short_press_flag) {
// 短按处理:切换LED颜色
short_press_flag = true;
led_state((led_state == LED_R) ? LED_Y : LED_R);
} else {
// 双击处理:切换蓝白灯
led_state(short_press_flag ? LED_B : LED_W);
short_press_flag = !short_press_flag;
}
}
}
}
int main(void) {
// 初始化GPIO和EXTI
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
// ... (其他GPIO配置)
EXTI_InitStructure.EXTI_Line = KEY_PIN_EXTI_LINE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode Falling;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
while (1) {
HAL_GPIO_TogglePin(LED_PORT, LED_R | LED_Y | LED_G | LED_B | LED_W); // 循环切换所有灯
HAL_Delay(LONG_PRESS_DELAY * 2); // 每隔一定时间检查长按
}
}
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)