ifndef _SKEY_H_
#define _SKEY_H_
#define uchar unsigned char
#define uint unsigned int
#define SKEY_FR RA0
#define SKEY_AC RA1
#define SKEY_RDEF RA3
#define BT_FR 0X0F //REC按鍵值
#define BT_AC 0XF0 //AC按鍵值
#define BT_RDEF 0XFF //RDEF按鍵值
#define UNKEY 0X00 //無按鍵
//返回的按鍵值
#define SUM 16 //檢測按鍵的總次數(shù)。每次為1ms
#define PRE 0XAA //按鍵按下
#define CURRENT 0X0F //釋放按鍵
uchar read_skey(void); //返回的當(dāng)前按鍵值
#endif
#i nclude
#i nclude"skey.h"
static uchar read_pre(uchar SkeyBit,uchar * SkeyCount);//讀釋放狀態(tài)
static uchar read_current(uchar SkeyBit,uchar *SkeyCount);//讀按下狀態(tài)
uchar SkeyFrState=UNKEY,SkeyAcState=UNKEY,SkeyRdefState=UNKEY;
uchar FrCount,AcCount,RdefCount;
uchar FrPreCount,AcPreCount,RdefPreCount;
static void io_init()
{
ANS0 = 0; //設(shè)置AN0為數(shù)字端口
ANS1 = 0; //設(shè)置AN1為數(shù)字端口
TRISA0 = 1;
TRISA1 = 1;
TRISA3 = 1; //設(shè)置三個(gè)按鍵為輸入
}
static uchar scan_pre(uchar SkeyBit) //掃描按下的鍵值
{
uchar skey_v = UNKEY;
switch(SkeyBit)
{
case BT_FR:
if(!SKEY_FR)
{
skey_v=BT_FR;
}
break;
case BT_AC:
if(!SKEY_AC)
{
skey_v=BT_AC;
}
break;
case BT_RDEF:
if(!SKEY_RDEF)
{
skey_v=BT_RDEF;
}
break;
default:break;
}
return skey_v; //按鍵為低電平有效
}
static uchar scan_current(uchar SkeyBit) //掃描釋放的鍵值
{
uchar skey_v = UNKEY;
switch(SkeyBit)
{
case BT_FR:
if(SKEY_FR)
skey_v=BT_FR;
break;
case BT_AC:
if(SKEY_AC)
skey_v=BT_AC;
break;
case BT_RDEF:
if(SKEY_RDEF)
skey_v=BT_RDEF;
break;
default:break;
}
return skey_v; //釋放按鍵為高電平有效
}
static uchar read_pre(uchar SkeyBit,uchar *SkeyCount)//參數(shù)為:需要采集的開關(guān)位
{
uchar SkeyTemp,PreState=UNKEY;//,SkeyCount;
if(*SkeyCount)
{
SkeyTemp = scan_pre(SkeyBit);
if(SkeyTemp == SkeyBit)
{
*SkeyCount += 1;
}
else
{
*SkeyCount = 0;
PreState = UNKEY;
}
}
else
{
SkeyTemp = scan_pre(SkeyBit);//采集第一個(gè)低電平
if(SkeyTemp == SkeyBit)
{
*SkeyCount = 1;
}
}
if(*SkeyCount == SUM) //完成了采集次數(shù)
{
PreState = PRE;
SkeyCount = 0;
}
return PreState;
}
static uchar read_current(uchar SkeyBit,uchar *SkeyCount)//參數(shù):需要采集的開關(guān)位
{
uchar SkeyTemp,CurrentState=PRE;//,SkeyCount;
if(*SkeyCount)
{
SkeyTemp = scan_current(SkeyBit);
if(SkeyTemp == SkeyBit)
{
*SkeyCount += 1;
}
else
{
CurrentState = PRE;
*SkeyCount = 0;
}
}
else
{ //采集第一個(gè)高電平
SkeyTemp = scan_current(SkeyBit);
if(SkeyTemp == SkeyBit)
{
*SkeyCount = 1;
}
}
if(*SkeyCount == SUM) //完成了采集次數(shù)
{
CurrentState = CURRENT;
*SkeyCount = 0;
}
return CurrentState;
}
/*******************************************
函數(shù)名:uchar read_skey()
功能:檢測并返回當(dāng)前按鍵值
參數(shù):無
返回值:當(dāng)前按鍵值
編寫者:
備注:read_skey()必須每5ms-30ms內(nèi),
運(yùn)行一次才能正確的采集按鍵,并返回按鍵值
修改目的:實(shí)現(xiàn)3個(gè)按鍵不會相互干擾
修改日期:
*******************************************/
uchar read_skey()
{
uchar SkeyValue = UNKEY;
io_init();
if(SkeyFrState == PRE) //狀態(tài)為按下
{
SkeyFrState = read_current(BT_FR,&FrCount);
}
else if(SkeyFrState == UNKEY) //狀態(tài)為無按鍵
{
SkeyFrState = read_pre(BT_FR,&FrPreCount);
}
if(SkeyFrState == CURRENT) //狀態(tài)為釋放
{
SkeyValue = BT_FR;
SkeyFrState = UNKEY;
return SkeyValue;
}
if(SkeyAcState == PRE) //狀態(tài)為按下
{
SkeyAcState = read_current(BT_AC,&AcCount);
}
else if(SkeyAcState == UNKEY) //狀態(tài)為無按鍵
{
SkeyAcState = read_pre(BT_AC,&AcPreCount);
}
if(SkeyAcState == CURRENT) //狀態(tài)為釋放
{
SkeyValue = BT_AC;
SkeyAcState = UNKEY;
return SkeyValue;
}
if(SkeyRdefState == PRE) //狀態(tài)為按下
{
SkeyRdefState = read_current(BT_RDEF,&RdefCount);
}
else if(SkeyRdefState == UNKEY) //UNKEY狀態(tài)為無按鍵
{
SkeyRdefState = read_pre(BT_RDEF,&RdefPreCount);
}
if(SkeyRdefState == CURRENT) //狀態(tài)為釋放
{
SkeyValue = BT_RDEF;
SkeyRdefState = UNKEY;
return SkeyValue;
}
return SkeyValue;
}