STM32 单片机:按键控制 8 种 LED 点亮模式的 C 语言实现
以下是一个使用 C 语言实现上述功能的示例代码,假设使用的是单片机开发环境(比如 STM32),并且 LED 连接在特定的 GPIO 端口上: ```c #include "stm32f10x.h" // 请根据实际使用的单片机型号修改头文件 // 定义 LED 端口 #define LED_PORT GPIOA // 定义按键端口 #define KEY1_PORT GPIOA #define KEY2_PORT GPIOA #define KEY3_PORT GPIOA #define KEY4_PORT GPIOA // 定义 LED 引脚 #define LED1_PIN GPIO_Pin_0 #define LED2_PIN GPIO_Pin_1 #define LED3_PIN GPIO_Pin_2 #define LED4_PIN GPIO_Pin_3 #define LED5_PIN GPIO_Pin_4 #define LED6_PIN GPIO_Pin_5 #define LED7_PIN GPIO_Pin_6 #define LED8_PIN GPIO_Pin_7 // 定义按键引脚 #define KEY1_PIN GPIO_Pin_8 #define KEY2_PIN GPIO_Pin_9 #define KEY3_PIN GPIO_Pin_10 #define KEY4_PIN GPIO_Pin_11 // 模式变量 int mode = 0; void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; // 使能 GPIOA 时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 配置 LED 为推挽输出 GPIO_InitStructure.GPIO_Pin = LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN | LED5_PIN | LED6_PIN | LED7_PIN | LED8_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(LED_PORT, &GPIO_InitStructure); // 配置按键为上拉输入 GPIO_InitStructure.GPIO_Pin = KEY1_PIN | KEY2_PIN | KEY3_PIN | KEY4_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(KEY1_PORT, &GPIO_InitStructure); } void LED_All_On(void) { GPIO_SetBits(LED_PORT, LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN | LED5_PIN | LED6_PIN | LED7_PIN | LED8_PIN); } void LED_All_Off(void) { GPIO_ResetBits(LED_PORT, LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN | LED5_PIN | LED6_PIN | LED7_PIN | LED8_PIN); } void Mode_Change(int key) { if (key == 2 && mode < 7) // 模式加一 { mode++; } else if (key == 3 && mode > 0) // 模式减一 { mode--; } } void LED_Mode_Control(void) { switch (mode) { case 0: LED_All_Off(); break; case 1: GPIO_SetBits(LED_PORT, LED1_PIN | LED3_PIN | LED5_PIN | LED7_PIN); GPIO_ResetBits(LED_PORT, LED2_PIN | LED4_PIN | LED6_PIN | LED8_PIN); break; case 2: GPIO_SetBits(LED_PORT, LED2_PIN | LED4_PIN | LED6_PIN | LED8_PIN); GPIO_ResetBits(LED_PORT, LED1_PIN | LED3_PIN | LED5_PIN | LED7_PIN); break; case 3: GPIO_SetBits(LED_PORT, LED1_PIN | LED2_PIN); GPIO_ResetBits(LED_PORT, LED3_PIN | LED4_PIN | LED5_PIN | LED6_PIN | LED7_PIN | LED8_PIN); break; case 4: GPIO_SetBits(LED_PORT, LED3_PIN | LED4_PIN); GPIO_ResetBits(LED_PORT, LED1_PIN | LED2_PIN | LED5_PIN | LED6_PIN | LED7_PIN | LED8_PIN); break; case 5: GPIO_SetBits(LED_PORT, LED5_PIN | LED6_PIN); GPIO_ResetBits(LED_PORT, LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN | LED7_PIN | LED8_PIN); break; case 6: GPIO_SetBits(LED_PORT, LED7_PIN | LED8_PIN); GPIO_ResetBits(LED_PORT, LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN | LED5_PIN | LED6_PIN); break; case 7: GPIO_SetBits(LED_PORT, LED1_PIN | LED4_PIN | LED5_PIN | LED8_PIN); GPIO_ResetBits(LED_PORT, LED2_PIN | LED3_PIN | LED6_PIN | LED7_PIN); break; default: break; } } int main(void) { GPIO_Configuration(); while (1) { if (GPIO_ReadInputDataBit(KEY1_PORT, KEY1_PIN) == 0) { LED_All_On(); } if (GPIO_ReadInputDataBit(KEY2_PORT, KEY2_PIN) == 0) { Mode_Change(2); } if (GPIO_ReadInputDataBit(KEY3_PORT, KEY3_PIN) == 0) { Mode_Change(3); } if (GPIO_ReadInputDataBit(KEY4_PORT, KEY4_PIN) == 0) { LED_All_Off(); } LED_Mode_Control(); } } ``` 上述代码只是一个示例,您需要根据实际的硬件连接和开发环境进行相应的修改和调整。 ######[AI写代码神器 | 1350点数解答 | 2024-11-06 22:02:31]
- C51 程序:利用外部中断 0 边沿触发实现 P2 口 8 个 LED 依次点亮及代码分析(DeepSeek | 228点数解答 | 2025-06-22 23:18:42)159
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)313
- 8051单片机代码详解:按钮控制LED闪烁及消抖原理(讯飞星火 | 697点数解答 | 2024-10-29 14:54:31)276
- 51单片机外部中断0实现LED流水灯:代码详解与要点提醒(DeepSeek | 214点数解答 | 2025-06-22 23:19:09)185
- 51 单片机按键控 LED:代码详解、优化与注意事项揭秘(DeepSeek | 326点数解答 | 2025-06-22 23:19:40)204
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)345
- 8051单片机代码:按钮控制LED状态切换代码详细注释解析(讯飞星火 | 830点数解答 | 2024-10-29 15:29:41)296
- 8051单片机代码:带详细注释的按键控制LED程序解析(讯飞星火 | 693点数解答 | 2024-10-29 15:31:00)418
- 8051 单片机:修正代码实现按钮控制 LED 及去抖逻辑(讯飞星火 | 570点数解答 | 2024-10-29 15:32:29)261
- 数字时钟模块代码:深入剖析输入输出与内部信号(字节豆包 | 36点数解答 | 2024-11-15 10:12:43)257
- Verilog代码:数字时钟模块的详细设计与实现(GPT | 3130点数解答 | 2024-11-15 10:14:41)248
- 仅36元!低成本人体感应自动门控制系统,硬件选型、代码全揭秘 (DeepSeek | 586点数解答 | 2025-04-08 16:54:34)187