Skip to content

Commit c4fb3d5

Browse files
Create main
1 parent e41fb27 commit c4fb3d5

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

main

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#include "stm32f10x.h"
2+
#include "key.h"
3+
#include "sys.h"
4+
5+
//////////////////////////////////////////////////////////////////////////////////
6+
//本程序只供学习使用,未经作者许可,不得用于其它任何用途
7+
//ALIENTEK精英STM32开发板
8+
//按键驱动代码
9+
//正点原子@ALIENTEK
10+
//技术论坛:www.openedv.com
11+
//修改日期:2012/9/3
12+
//版本:V1.0
13+
//版权所有,盗版必究。
14+
//Copyright(C) 广州市星翼电子科技有限公司 2009-2019
15+
//All rights reserved
16+
//////////////////////////////////////////////////////////////////////////////////
17+
18+
u8 isKey1 = 0;
19+
u8 isKey2 = 0;
20+
u8 isKey3 = 0;
21+
u8 isKey4 = 0;
22+
u8 isKey5 = 0;
23+
u8 isKey6 = 0;
24+
u8 isKey7 = 0;
25+
u8 isKey8 = 0;
26+
27+
//
28+
/*
29+
******按键初始化函数*****************************************
30+
* 功能: 按键初始化函数
31+
* 入口参数: 无
32+
* 返回参数: 无
33+
* 说明: KEY1-PB14 KEY2-PB12 KEY3-PB15 KEY4-PB13
34+
************************************************************
35+
*/
36+
void KEY_Init(void) //IO初始化
37+
{
38+
GPIO_InitTypeDef GPIO_InitStructure;
39+
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能PORTA时钟
40+
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;//KEY0,KEY1
41+
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
42+
GPIO_Init(GPIOB, &GPIO_InitStructure);//
43+
}
44+
45+
46+
void Key1Press(void)
47+
{
48+
if(isKey1 == 0)
49+
isKey1=1;
50+
}
51+
52+
void Key2Press(void)
53+
{
54+
if(isKey2 == 0)
55+
isKey2=1;
56+
57+
}
58+
59+
void Key3Press(void)
60+
{
61+
if(isKey3 == 0)
62+
isKey3=1;
63+
64+
}
65+
66+
void Key4Press(void)
67+
{
68+
if(isKey4 == 0)
69+
isKey4=1;
70+
71+
}
72+
73+
//void Key5Press(void)
74+
//{
75+
// if(isKey5 == 0)
76+
// isKey5=1;
77+
//}
78+
79+
//void Key6Press(void)
80+
//{
81+
// if(isKey6 == 0)
82+
// isKey6=1;
83+
84+
//}
85+
86+
//void Key7Press(void)
87+
//{
88+
// if(isKey7 == 0)
89+
// isKey7=1;
90+
91+
//}
92+
93+
//void Key8Press(void)
94+
//{
95+
// if(isKey8 == 0)
96+
// isKey8=1;
97+
98+
//}
99+
100+
/*
101+
******按键扫描函数*****************************************
102+
* 功能: 按键扫描函数
103+
* 入口参数: 无
104+
* 返回参数: 无
105+
* 说明: KEY1-PB14 KEY2-PB12 KEY3-PB15 KEY4-PB13
106+
************************************************************
107+
*/
108+
void KeyScan(void)
109+
{
110+
static int keyCount = 0;
111+
static int keyState = 0;
112+
if (KEY1 == 0 && keyState == 0) //按键按下
113+
{
114+
keyCount++;
115+
if (keyCount > 1 && KEY1 == 0 && keyState == 0) //加两次类似延迟10ms,不好解释
116+
{
117+
/*ToDo:按键按下执行的操作*/
118+
Key1Press();
119+
keyState = 1;
120+
}
121+
}
122+
else if (KEY2 == 0 && keyState == 0)
123+
{
124+
keyCount++;
125+
if (keyCount > 1 && KEY2 == 0 && keyState == 0)
126+
{
127+
/*ToDo:按键按下执行的操作*/
128+
Key2Press();
129+
keyState = 1;
130+
}
131+
}
132+
else if (KEY3 == 0 && keyState == 0)
133+
{
134+
keyCount++;
135+
if (keyCount > 1 && KEY3 == 0 && keyState == 0)
136+
{
137+
/*ToDo:按键按下执行的操作*/
138+
Key3Press();
139+
keyState = 1;
140+
}
141+
}
142+
else if (KEY4 == 0 && keyState == 0)
143+
{
144+
keyCount++;
145+
if (keyCount > 1 && KEY4 == 0 && keyState == 0)
146+
{
147+
/*ToDo:按键按下执行的操作*/
148+
Key4Press();
149+
keyState = 1;
150+
}
151+
}
152+
153+
154+
else if (KEY1 == 1 && KEY2 == 1 && KEY3 == 1 && KEY4 == 1 &&keyState == 1) //当所有按键都处于抬起状态,状态刷新 && KEY5 == 1 && KEY6 == 1 && KEY7 == 1 && KEY8 == 1&&keyState ==
155+
{
156+
keyCount = 0;
157+
keyState = 0;
158+
}
159+
}

0 commit comments

Comments
 (0)