#include "KeyBoard.h"
#include "AS1.h"
//unsigned int msCount;
#define KeyBuffLen 8
struct Struct_KeyBoardBuff
{
uchar buff[KeyBuffLen];
uchar in;
uchar out;
}Key;
enum KeyFSM_Enum
{
_Key_Idle=0,
_Key1_down,
_Key1_press,
_Key1_up,
_Key2_down,
_Key2_press,
_Key2_up,
_Key_confirm,
};
void KeyBoardInit(void)
{
Key.in=0;
Key.out=0;
}
uchar ReadKey(void)
{
uchar Value;
if (Key.out!=Key.in)
{
Value=Key.buff[Key.out++];
if (Key.out>=KeyBuffLen)
{
Key.out=0;
}
}
else
{
Value=KeyNull;
}
return(Value);
}
uchar JudgeKey(uchar key)
{
uchar i,count=0;
for (i=0;i<6 ;i++ )
{
if ((key&0x01)==0)count++;
key=key>>1;
}
return(count);
}
void KeyIn(void)
{
static uchar Key_Input=0;
static uchar volatile step=0;
//Put the code get the Key_Value
uchar NewKey=KEY_PORT&0x3f;
//---------------------------
uchar num=JudgeKey(NewKey);
switch(step)
{
case _Key_Idle:
{
switch (num)
{
case 1:
{
Key_Input=NewKey;
step=_Key1_down;
break;
}
case 2:
{
Key_Input=NewKey;
step=_Key2_down;
break;
}
default:
{
break;
}
}
break;
}
case _Key1_down:
{
switch (num)
{
case 1:
{
if(NewKey==Key_Input)
step=_Key1_press;
else
step=_Key_Idle;
break;
}
case 2:
{
Key_Input=NewKey;
step=_Key2_down;
break;
}
default:
{
step=_Key_Idle;
break;
}
}
break;
}
case _Key1_press:
{
switch (num)
{
case 1:
{
if(NewKey!=Key_Input)
step=_Key1_up;
break;
}
case 2:
{
Key_Input=NewKey;
step=_Key2_down;
break;
}
default:
{
step=_Key1_up;
break;
}
}
break;
}
case _Key1_up:
{
switch (num)
{
case 1:
{
if(NewKey==Key_Input)
step=_Key1_press;
else
step=_Key_confirm;
break;
}
case 2:
{
Key_Input=NewKey;
step=_Key2_down;
break;
}
default:
{
step=_Key_confirm;
break;
}
}
break;
}
case _Key2_down:
{
if(NewKey==Key_Input)
step=_Key2_press;
else
step=_Key_Idle;
break;
}
case _Key2_press:
{
if (num==0)
step=_Key2_up;
break;
}
case _Key2_up:
{
if (num==0)
step=_Key_confirm;
break;
}
default:
{
step=_Key_Idle;
break;
}
}
if (step==_Key_confirm)
{
step=_Key_Idle;
if (Key_Input!=0)
{
Key.buff[Key.in++]=Key_Input;
if (Key.in>=KeyBuffLen)
{
Key.in=0;
}
}
}
}
void Key_Board_Test(void)
{
uchar key;
key=ReadKey();
switch (key)
{
case KeyUp:
{
AS1_SendString("Up.");
break;
}
case KeyDown:
{
AS1_SendString("Down.");
break;
}
case KeyAdd:
{
AS1_SendString("Add.");
break;
}
case KeySub:
{
AS1_SendString("Sub.");
break;
}
case KeyEnter:
{
AS1_SendString("Enter.");
break;
}
case KeyReturn:
{
AS1_SendString("Return.");
break;
}
case KeyLr:
{
AS1_SendString("LR.");
break;
}
default:
{
//AS1_SendString("None.");
break;
}
}
}
文件Keyboard.h
#ifndef _Key_Board_h_
#define _Key_Board_h_
#define KEY_PORT PORTA
#define KeyUp 0x3e
#define KeyDown 0x3d
#define KeyAdd 0x3b
#define KeySub 0x37
#define KeyEnter 0x2f
#define KeyReturn 0x1f
#define KeyLr 0x33
#define KeyNull 0xff
typedef unsigned char uchar;
extern unsigned int msCount;
extern void KeyBoardInit(void);
extern unsigned char ReadKey(void);
extern void KeyIn(void);
extern void Key_Board_Test(void);
#endif |