|
发表于 2013-7-17 21:51:08
|
显示全部楼层
发一下我的按键程序吧。
是用本坛的“三行按键”基础上改的,阿莫论坛,就是神!
互相学习,提高吧。
#include "../hfile/config.h"
uint16 GCountLongPressTime;
uint8 GTrigger=0;
uint8 GHold=0;
uint8 g_KeyValue=0x00;
uint8 g_LongPressKeyValue=0x00;
/**********************************************************************
//this routine maybe put in the mainlop.no need decrease counter.
//it take the loop as a delay!
**********************************************************************/
uint8 GetPinValue(void )
{
uint8 KeyPort=0xff;
uint8 Key0value=0x00;
uint8 Key1value=0x00;
uint8 Key2value=0x00;
uint8 Key3value=0x00;
//if(0==PIN(B,3)) #define PIN(m,n)
if(
0==(
!(uint8)(GET_BIT(DDRB).bit3=0)&&\
(GET_BIT(PORTB).bit3=1)&&\
GET_BIT(PINB).bit3
//(uint8)( GET_BIT(PORTB).bit3=1)&&\//
)
)
Key0value=0x01;
if(0==PIN(D,4))
Key1value=0x02;
if(0==PIN(D,3))
Key2value=0x04;
if(0==PIN(B,2))
Key3value=0x08;
KeyPort=KeyPort&(~Key0value)&(~Key1value)&(~Key2value)&(~Key3value);
return KeyPort;
}
void ThreeLineKeyRead(void)//读按键
{
uint8 virtualPinValue=GetPinValue();
uint8 ReadData=0;
ReadData=virtualPinValue^0xff;
GTrigger=ReadData&(ReadData^GHold);
GHold=ReadData;
}
void WhichKeyIsShortPress(void)//短按键
{
if(GTrigger&0x01)
{
g_KeyValue=KEYCH;//左数第1个按键
}
if(GTrigger&0x02)
{
g_KeyValue=KEYADD;//左数第2个按键
}
if(GTrigger&0x04)
{
g_KeyValue=KEYDEC;//左数第3个按键
}
if(GTrigger&0x08)
{
g_KeyValue=KEYSET;//左数第4个按键
}
if(g_KeyValue!=0)g_SystemTime60s=0;
}
void WhichKeyIsLongPress(void) //长按键
{
if(GHold){GCountLongPressTime++;}
else {
GCountLongPressTime=0;
g_LongPressKeyValue=0;
}
if(GCountLongPressTime>500)//65500
{
if(GHold&0x02)
{
g_LongPressKeyValue=longKEYADD;//左数第2个按键
}
if(GHold&0x04)
{
g_LongPressKeyValue=longKEYDEC;//左数第3个按键
}
if(GHold&0x08)
{
g_LongPressKeyValue=longKEYSET;//左数第4个按键
}
}
}
void KeyRead(void)
{
ThreeLineKeyRead();//读按键
WhichKeyIsShortPress();
WhichKeyIsLongPress();
}
|
|