|
//如果用是按键建议10MS执行一次,如果用来计数,可调快扫描周期。
//#define C_ENCODE_A BIT4
//#define C_ENCODE_B BIT5
//#define PIN_ENCODE_KEY (C_ENCODE_A | C_ENCODE_B)
//=======================================================================================
KEY_CODE_Type ScanEncode(void)
{
KEY_CODE_Type KeyCode = C_NO_KEY_CODE;
u8 KeyState,KeyTemp,KeyNew;
static s16 iEncode = 0; //conter of encoder
static u8 KeyOld = PIN_ENCODE_KEY;
static u8 f_debounce,Debounce;
//---------------------------------------------------------------------------------------
SET_ENCODE_PIN(); //P0->PIN |= PIN_ENCODE_KEY; //BIT6 P06
KeyNew = GET_ENCODE_PIN(); //KeyNew = P0->PIN & PIN_ENCODE_KEY;
KeyState = KeyOld ^ KeyNew;
if (KeyState) // Press
{
if (!f_debounce)
{
f_debounce = 1;
Debounce = C_DEBOUCE_TICK;
}
}
else //check long key press
{
//-------------------------------------------------------------------------------
}
//------------------------------------------------------------------------------------
if (f_debounce)
{
if (Debounce)
{
Debounce--;
}
else
{
f_debounce = 0;
//----------------------------------------------------------------------------
KeyTemp = KeyState & KeyOld; //KEY DOWN
if (KeyTemp)
{
if (KeyTemp & C_ENCODE_A)
{
KeyOld &= (~C_ENCODE_A);
if (KeyNew & C_ENCODE_B)
{
iEncode++;
KeyCode = C_CW_KEY_CODE;
}
else
{
iEncode--;
KeyCode = C_CCW_KEY_CODE;
}
}
else if (KeyTemp & C_ENCODE_B)
{
//f_DownKeyPress = 1;
KeyOld &= (~C_ENCODE_B);
}
}
//----------------------------------------------------------------------------
KeyTemp = KeyState & KeyNew; //KEY UP
if (KeyTemp)
{
if (KeyTemp & C_ENCODE_A)
{
KeyOld |= C_ENCODE_A;
if (KeyNew & C_ENCODE_B)
{
iEncode--;
KeyCode = C_CCW_KEY_CODE;
}
else
{
iEncode++;
KeyCode = C_CW_KEY_CODE;
}
}
else if (KeyTemp & C_ENCODE_B)
{
//f_DownKeyPress = 0;
KeyOld |= C_ENCODE_B;
}
}
}
}
return KeyCode;
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
|