⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 key.c

📁 利用A/D口的按键处理程序
💻 C
字号:

#include "define.h"
#include "key.h"

//--------------------------------------
const UINT8 code arLKeyTime[]=
{
	_TC_1000ms,
	_TC_2000ms,
	_TC_1000ms,
	_TC_1000ms,
	_TC_1000ms,
	_TC_1000ms,
	_TC_1000ms,
	_TC_1000ms,
	_TC_1000ms,
	_TC_1000ms,
	_TC_1000ms,
	_TC_1000ms,
	_TC_1000ms,
	_TC_1000ms,
	_TC_1000ms,
	_TC_1000ms
};

//--------------------------------------

UINT8 ADCvt(UINT8 chan)
{
	AMX0SL = (AMX0SL & 0xf8) | chan;		//set adc pin
	ADC0CN = 0xd1;							//start adc, data left adjustify
	while (ADC0CN & 0x8);					//wait adc complete
	return ADC0H;
}

UINT8 GetADCKey(UINT8 chan)
{
	static UINT8 keyAnti, keyADC;
	UINT8 tmp;
	tmp = (ADCvt(chan)+8)>>4;
	if (tmp == keyADC) 
		{
		if (++keyAnti >= 3)
			{
			keyAnti = 0;
			keyADC = tmp;
			return tmp;
			}
		else
			return 0xff;
		}
	else
		{
		keyAnti = 0;
		keyADC = tmp;
		return 0xff;		//have no new key
		}
}

/***********************************************/
KEYOUT GetKey()
{
	UINT8 keyTmp;
	static UINT8 keyLast;
	KEYOUT keyOut = {_Inv_Key, _KeyS_Null};
	keyTmp = GetADCKey(_Key_AdChan);
	if (keyTmp == 0xff)		// have no stable key
		return keyOut;
	if (keyStatus < 0x10)	//all release status
		{
		if (keyTmp == _Null_Key)	//no key press
			{
			if (keyTimer == 0)
				{
				keyStatus = _KeyS_Null;	// --> free
				keyLast = _Null_Key;
				}
			//else /*keep keyStatus, keyLast*/
			return keyOut;
			}
		else if (keyTmp != keyLast)	//new key press
			{
			keyStatus = _KeyS_Press;
			keyTimer = arLKeyTime[keyTmp];
			keyLast = keyTmp;
			keyOut.value = keyTmp;
			keyOut.attr = keyStatus;
			return keyOut;
			}
		}
	else if(keyTmp != keyLast)			//all key press status
		{
		keyStatus -= 0x10;		//to release
		keyTimer = _TC_CKey_Wait;
		keyOut.value = keyLast;
		keyOut.attr = keyStatus;
		return keyOut;
		}

/* only deal with (key holding) and (the same key pressed in release status)*/
	switch (keyStatus)
		{
		/* key holding handle */
		case _KeyS_Press:
		case _KeyS_Long:
		case _KeyS_Hold:

		case _KeyS_SComb:
		case _KeyS_SL:
		case _KeyS_SH:

		case _KeyS_LComb:
		case _KeyS_LL:
		case _KeyS_LH:
			if (keyTimer == 0)
				{
				keyTimer = _TC_HKey;
				if ((keyStatus & 0x3) != 3)
					keyStatus += 1;
				}
			else
				return keyOut;
			break;

		/* 	the same key pressed in release status */
		case _KeyS_SRls:
			keyStatus = _KeyS_SComb;
			keyTimer = arLKeyTime[keyTmp];
			break;

		case _KeyS_LRls:
		case _KeyS_HRls:
			keyStatus = _KeyS_LComb;
			keyTimer = arLKeyTime[keyTmp];
			break;

		case _KeyS_SCRls:
		case _KeyS_SLRls:
		case _KeyS_SHRls:
		case _KeyS_LCRls:
		case _KeyS_LLRls:
		case _KeyS_LHRls:
			keyStatus = _KeyS_Press;
			keyTimer = arLKeyTime[keyTmp];
			break;
			
		default:
			return keyOut;
		}
		keyOut.value = keyTmp;
		keyOut.attr = keyStatus;
		return keyOut;
	
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -