C语言按键问题,
voidGetKey(uint8*pKeyValue){staticuint8s_u8KeyState=KEY_STATE_INIT;staticuint8s_u8Key...
void GetKey(uint8 *pKeyValue)
{
static uint8 s_u8KeyState = KEY_STATE_INIT ;
static uint8 s_u8KeyTimeCount = 0 ;
static uint8 s_u8LastKey = KEY_NULL ; //
KeyTemp = KeyScan() ; //
switch(s_u8KeyState)
{
case KEY_STATE_INIT :
if(KEY_NULL != (KeyTemp))
s_u8KeyState = KEY_STATE_WOBBLE ;
break ;
case KEY_STATE_WOBBLE : //
{
s_u8KeyState = KEY_STATE_PRESS ;
break ;
case KEY_STATE_PRESS :
if(KEY_NULL != (KeyTemp))
{
s_u8LastKey = KeyTemp ; //
KeyTemp |= KEY_DOWN ; /
s_u8KeyState = KEY_STATE_LONG ;
}
else
s_u8KeyState = KEY_STATE_INIT ;
break ;
case KEY_STATE_LONG :
if(KEY_NULL != (KeyTemp))
{
if(++s_u8KeyTimeCount > KEY_LONG_PERIOD)
{
s_u8KeyTimeCount = 0 ;
KeyTemp |= KEY_LONG ; //
s_u8KeyState = KEY_STATE_CONTINUE ;
}
}
else
s_u8KeyState = KEY_STATE_RELEASE ;
break ;
case KEY_STATE_CONTINUE :
if(KEY_NULL != (KeyTemp))
{
if(++s_u8KeyTimeCount > KEY_CONTINUE_PERIOD)
{
s_u8KeyTimeCount = 0 ;
KeyTemp |= KEY_CONTINUE ;
}
}
else
s_u8KeyState = KEY_STATE_RELEASE ;
break ;
case KEY_STATE_RELEASE :
s_u8LastKey |= KEY_UP ;
KeyTemp = s_u8LastKey ;
s_u8KeyState = KEY_STATE_INIT ;
break ;
default :break ;
}
*pKeyValue = KeyTemp ;
}
void main(void)
{
uint8 KeyValue = KEY_NULL;
uint8 temp = 0 ;
KeyInit() ;
while(1)
{
GetKey(&KeyValue);
if(KeyValue == (KEY_VALUE_1 | KEY_DOWN)) LED =0XFE ;//第“1”步
if(KeyValue == (KEY_VALUE_1 | KEY_LONG)) LED =0XFD ;//第“2”步
if(KeyValue == (KEY_VALUE_1 | KEY_CONTINUE))LED ^=0XFE ;//第“3”步
if(KeyValue == (KEY_VALUE_1 | KEY_UP))LED = 0x55 ;//第“4”步
}
}
我这个程序为什么执行的时候 只要按键一按下就跑到第“3”步了, 前面两步不走的,为什么? 展开
{
static uint8 s_u8KeyState = KEY_STATE_INIT ;
static uint8 s_u8KeyTimeCount = 0 ;
static uint8 s_u8LastKey = KEY_NULL ; //
KeyTemp = KeyScan() ; //
switch(s_u8KeyState)
{
case KEY_STATE_INIT :
if(KEY_NULL != (KeyTemp))
s_u8KeyState = KEY_STATE_WOBBLE ;
break ;
case KEY_STATE_WOBBLE : //
{
s_u8KeyState = KEY_STATE_PRESS ;
break ;
case KEY_STATE_PRESS :
if(KEY_NULL != (KeyTemp))
{
s_u8LastKey = KeyTemp ; //
KeyTemp |= KEY_DOWN ; /
s_u8KeyState = KEY_STATE_LONG ;
}
else
s_u8KeyState = KEY_STATE_INIT ;
break ;
case KEY_STATE_LONG :
if(KEY_NULL != (KeyTemp))
{
if(++s_u8KeyTimeCount > KEY_LONG_PERIOD)
{
s_u8KeyTimeCount = 0 ;
KeyTemp |= KEY_LONG ; //
s_u8KeyState = KEY_STATE_CONTINUE ;
}
}
else
s_u8KeyState = KEY_STATE_RELEASE ;
break ;
case KEY_STATE_CONTINUE :
if(KEY_NULL != (KeyTemp))
{
if(++s_u8KeyTimeCount > KEY_CONTINUE_PERIOD)
{
s_u8KeyTimeCount = 0 ;
KeyTemp |= KEY_CONTINUE ;
}
}
else
s_u8KeyState = KEY_STATE_RELEASE ;
break ;
case KEY_STATE_RELEASE :
s_u8LastKey |= KEY_UP ;
KeyTemp = s_u8LastKey ;
s_u8KeyState = KEY_STATE_INIT ;
break ;
default :break ;
}
*pKeyValue = KeyTemp ;
}
void main(void)
{
uint8 KeyValue = KEY_NULL;
uint8 temp = 0 ;
KeyInit() ;
while(1)
{
GetKey(&KeyValue);
if(KeyValue == (KEY_VALUE_1 | KEY_DOWN)) LED =0XFE ;//第“1”步
if(KeyValue == (KEY_VALUE_1 | KEY_LONG)) LED =0XFD ;//第“2”步
if(KeyValue == (KEY_VALUE_1 | KEY_CONTINUE))LED ^=0XFE ;//第“3”步
if(KeyValue == (KEY_VALUE_1 | KEY_UP))LED = 0x55 ;//第“4”步
}
}
我这个程序为什么执行的时候 只要按键一按下就跑到第“3”步了, 前面两步不走的,为什么? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询