"); //-->
#include "delay.h"
#include "sys.h"
#include "Key.h"
#include <stdlib.h>
#define KeyPin PAin(5)
typedef enum
{
KeyDown,
KeyUp,
KeyShort,
KeyLong
}KEYSTATE;
KEYSTATE KeyState;
void KeyPinInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能PB端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;//KeyPin检测
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
static u16 KeyDownTimes,KeyDoubleTimes;
void PollingKey(void)
{
static u8 KeyStep;
switch (KeyStep)
{
case 0:
{
if (!KeyPin)//有键按下
{
//printf("\r\n按下");
KeyState = KeyDown;
if (KeyDoubleTimes > 0)
{
KeyStep = 4;//双击
}
else
{
KeyStep = 2;//不是双击
}
}
else if(KeyDoubleTimes > 0)
{
KeyDoubleTimes--;
if ((KeyDoubleTimes == 0)&&(KeyState == KeyShort))
{
printf("\r\n短按处理");
KeyState = KeyUp;
}
}
}
break;
case 1://按下
{
if (!KeyPin)
{
KeyDownTimes++;
if (KeyDownTimes < 2800)
{
KeyState = KeyShort;
}
else if(KeyState != KeyLong)
{
KeyState = KeyLong;
printf("\r\n长按处理");
}
else if (KeyDownTimes >= 2800)//防止加满溢出
{
KeyDownTimes = 2800;
}
}
else
{
KeyStep = 2;
}
}
break;
case 2://
{
if (KeyPin)//弹起
{
KeyStep = 3;
}
else//按下
{
KeyStep = 1;
}
}
break;
case 3://弹起
{
if (KeyPin)//弹起
{
if (KeyDownTimes < 2800)
{
KeyDoubleTimes = 500;
}
KeyDownTimes = 0;
KeyStep = 0;
}
else
{
KeyStep = 2;
}
}
break;
case 4:
{
if (KeyPin)//如果弹起的话就执行
{
KeyState = KeyUp;
printf("\r\n双击处理");
KeyStep = 0;
}
}
break;
default:
KeyStep = 0;
break;
}
delay_us(100);
}
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。