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

📄 key.c

📁 针对philips的51单片机LPC931的具体应用的例子。实现对uart, spi, i2c等硬件资源的操作。还有针对小内存单片机操作系统的实现。
💻 C
字号:
#include "REG931.H"
#include "memory.h"
#include "system.h"
#include "key.h"

//-------------------------------------------
//功能:延时1.5us
// average:1T=1.5us,when,the more number, the more accarate
// if delay 1us=5us, if 2us=6us, if 3us=7us;if 4us=8us
//-------------------------------------------
void	Delayus(unsigned char us) {
#if    0
abcd:
	while(SP>0xfd){
		us=us;
		goto abcd;
	}
#endif
	while(us--);
}

//-------------------------------------------
//功能:延时1ms
//-------------------------------------------
void	DelayMs(unsigned char ms) {
	unsigned char i;
	while(ms--){
		for(i=0;i<4;i++)
			Delayus(250);
	}
}

#if    0
//-------------------------------------------
//功能:键盘中断初始化程序
//-------------------------------------------
void	KeyISR_Init()		// use P2.0,P21,P26,P27
{
	KBMASK=0xC3;				//设置P20,P21,P26,P27为中断源
	KBCON=0x00;					//清除键盘中断标志
	P0=P0&0x0F;					//P07->P04作输出,先使其输出0000xxxxB;
//	Delayus();					//延时3us左右,使程序对P0端口的操作更稳定
	EKBI=1;
//	EA=1;
}
#endif

// key pin is active low level
void KeyScan(void){	// use P20,P21,P26,P27
	uchar key;
#if  PLCC_PACKAGE_IO
	P2 |= 0xC3;	// 11xx  xx11 B
	key=P2&0xc3;
	if(key==0xc3)		// no key pressed
		return;
#elif   TSSOP_PACKAGE_IO
	P0|= 0x0F;    // xxxx 1111 B
	key=P0 & 0x0F;
	if(key==0x0F)		// no key pressed
		return;
#elif   TSSOP_PACKAGE_IO_SECOND
	P0|= 0x4e;    // x1xx 111x B
	key=P0 & 0x4e;
	if(key==0x4e)		// no key pressed
		return;
#endif
	switch(key){
		case K1_PIN:		KeyData=K_1;	break;
		case K2_PIN:		KeyData=K_2;	break;
		case K3_PIN:		KeyData=K_3;	break;
		case K4_PIN:		KeyData=K_4;	break;
		case K12_PIN:	KeyData=K_12;	break;
		case K13_PIN:	KeyData=K_13;	break;
		case K14_PIN:	KeyData=K_14;	break;
		case K23_PIN:	KeyData=K_23;	break;
		case K24_PIN:	KeyData=K_24;	break;
		case K34_PIN:	KeyData=K_34;	break;
		default:		KeyData=K_InValid;  break;
	}
	KeyData |=S_PANEL; 
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *	*
 * Name:		CalKeyEvent()												* 
 * Input:		KEYDATA											*
 * Output:	KeyEvent = PRESS_DOWN,PRESS_HOLD,PRESS_UP OR lastevent		*
 * Used:		Static PrvKData,KeyTimer									*
 * Function:	Key higher process for Remote,PanelKey,Encoder.To calculate *
 *				out KeyEvent,only return once if event not be responsed		*
 *				After event responsed,LastEvent should be changed to IDLE	*
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  */
#if     1              // process double spanel key press
void CalKeyEvent(void){	  // modify by ldy 2004-12-16
	uchar KeyStatus,PrvStatus;
	static uchar data PrvKData,KeyTimer,SaveKey;
	static bit bExit;
	KeyStatus = KeyData& 0xc0;
	PrvStatus=PrvKData & 0xc0;	
	switch(KeyStatus){
	case S_IDLE:
		if(PrvStatus==S_IDLE)
			break;
//KUP:   
		PrvKData=0;
		if(KeyEvent == PRESS_HOLD)
			KeyEvent=PRESS_UP_LONG;
		else if(!bExit && KeyTimer >= PRESS_DELAY_TIME)
			KeyEvent=PRESS_UP_SHORT;
		KeyTimer =0;			//Reset timer
		KeyData=SaveKey;
		bExit=0;
		break;
	case S_PANEL:
	/*	FPanelKey=1;*/
		if(PrvStatus == S_PANEL){
			if((KeyData &0x3f) || (PrvKData & 0x3f)){    // single & double key pressed
				if(KeyData == PrvKData){		// cur key == old key
					KeyData &= 0x3f;	//KeyStatus=IDLE
					SaveKey= KeyData;
					if(KeyTimer == KEYHOLDTIME+PRESS_DELAY_TIME){			
						KeyEvent=PRESS_HOLD;
						break;
					}
					else
						KeyTimer++;	
					if(KeyTimer == PRESS_DELAY_TIME)
						KeyEvent = PRESS_DOWN;
				}
				else{	 // key pressed, but cur key != old key,
					if(KeyTimer>=PRESS_DELAY_TIME){  // is press up
						if(KeyEvent == PRESS_HOLD)
							KeyEvent=PRESS_UP_LONG;
						else 
							KeyEvent=PRESS_UP_SHORT;
						KeyTimer=PRESS_DELAY_TIME-1;
						KeyData=SaveKey;
						bExit=1;
						PrvKData &=0xc0;
					}
					else{ 
						if((KeyData&0x3f)>=K_12 && (!bExit)/*(KeyTimer!=PRESS_DELAY_TIME-1)*/)	// first double key
							PrvKData=KeyData;
						KeyData &=0x3f;
					}
				}
			}
			else{
				KeyData = 0;
			}
		}
		else{	  // first press key(s)
			PrvKData=KeyData;
	             KeyTimer=0;
			KeyData &= 0x3f;		//set KeySatus = IDLE
//			FManual_Key=1;
		}
		break;
	case S_REMOTE:
		break;
	}
}

#else
void CalKeyEvent(void){	
	uchar KeyStatus,PrvStatus;
	static uchar data PrvKData,KeyTimer;
	KeyStatus = KeyData& 0xc0;
	PrvStatus=PrvKData & 0xc0;	
	switch(KeyStatus){
	case S_IDLE:
		if(PrvStatus==S_IDLE)
			break;
//KUP:   
		PrvKData=0;
		if(KeyEvent == PRESS_HOLD)
			KeyEvent=PRESS_UP_LONG;
		else
			KeyEvent=PRESS_UP_SHORT;
		KeyTimer =0;			//Reset timer
		break;
	case S_PANEL:
	/*	FPanelKey=1;*/
		if(PrvStatus == S_PANEL){
			if(KeyData==PrvKData){
				KeyData &= 0x3f;		//KeyStatus=IDLE
//KHD:   
				if(KeyTimer == KEYHOLDTIME){			
					KeyEvent=PRESS_HOLD;
					break;		//return once								
				}
				else
					KeyTimer++;				
			}
			else
				KeyData=PrvKData=0;		//key error
		}
		else{
			PrvKData=KeyData;
//	case S_ENCODER:
			KeyData &= 0x3f;			//set KeySatus = IDLE
//KPD:
	            KeyTimer=0;
			KeyEvent=PRESS_DOWN;
//			FManual_Key=1;
		}
		break;
#if     0
	case S_REMOTE:
		RemoteTimer=4;//40ms*4=80ms
	       if(REMVALID){
            if(KeyTimer>2){
                FChanged=1;
                SaveData=KeyData;
                KeyData=PrvKData;
                goto KUP;
            }
KEY_CONTINUE:
            if(FChanged)
               KeyData=SaveData;
            PrvKData=KeyData;
            REMVALID=0;
			goto KPD;
        }
        if(FChanged)
            goto KEY_CONTINUE;
		if(RPTVALID||FREM_Key)		//32*8=256ms
			goto KHD;
		KeyData=KeyData& 0x3f;			//set KeySatus = IDLE
		goto KUP;
#endif
	}
}

#endif

⌨️ 快捷键说明

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