STM32-11-一键控制4个灯

lemon Lv5

1.代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "stm32f10x.h"
#include "sys.h"
#define KEY PCin(0)

#define LED1 PBout(8)//0不亮,1亮
#define LED2 PBout(9)
#define LED3 PBout(10)
#define LED4 PBout(11)
u8 id=0;
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11);//0
}

void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}

void Delay(unsigned int count)
{
unsigned int i;
for (; count != 0; count--)
{
i = 5000;
while (i--);
}
}

u8 KEY_Scan(void)
{
if(KEY==0)
{
Delay(20);
if(KEY==0)
{
id=id%4;
if(id==0)
{
LED1=1;
LED2=0;
LED3=0;
LED4=0;
while(KEY==0);
}
else if(id==1)
{
LED1=0;
LED2=1;
LED3=0;
LED4=0;
while(KEY==0);
}
else if(id==2)
{
LED1=0;
LED2=0;
LED3=3;
LED4=0;
while(KEY==0);
}
else if(id==3)
{
LED1=0;
LED2=0;
LED3=0;
LED4=1;
while(KEY==0);
}
id=id+1;
}
}
}

int main(void)
{
LED_Init();
KEY_Init();
while (1)
{
KEY_Scan();
}
}

2.模拟图

a


代码下载链接(123云盘):https://www.123684.com/s/wFuijv-i8Zgh

  • 标题: STM32-11-一键控制4个灯
  • 作者: lemon
  • 创建于 : 2025-07-22 23:00:31
  • 更新于 : 2025-07-23 00:13:49
  • 链接: https://lemon2003.github.io/post/20250722230031.html
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
目录
STM32-11-一键控制4个灯