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

📄 key.c

📁 车载 液晶显示器的主控程序(主要使用芯片为 MYSON MTV512 单片机、RealTek 2323 Scare 芯片、TVP5147(视频解码)。配Sharp 8寸液晶显示器 )。
💻 C
📖 第 1 页 / 共 2 页
字号:
//----------------------------------------------------------------------------------------------------
// ID Code      : Key.c No.0000
// Update Note  : 
//
//----------------------------------------------------------------------------------------------------

#define __KEY__

#include "..\Header\Include.h"


//--------------------------------------------------
// Description  : Key scan process
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void CKeyHandler(void)
{
	ucKeyMessage = _NONE_MESSAGE;

    	if(CKeyScanReady())
    	{
        	// Store previous key state
        	ucKeyStatePrev = ucKeyStateCurr;

        	// Get current key state
        	ucKeyStateCurr = CKeyScan();

        	// Power key process, return if power key is pressed
        	if(CKeyPowerKeyProc())
            	return;

			//x.hu, for source switch
			if (CKeySourceKeyProc())
				return;

        	// Convert key state to key message, store in (ucKeyNotify)
        	CKeyMessageProc();
    	}

	#if (_IRPOLLING)
		#if (_IR_SHOW_CODE == _ENABLE)
		CIrShowCode();
		#endif
	#endif
}

//--------------------------------------------------
// Description  : Check power key process
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void CKeyCheckPowerKey(void)
{
    	// Store previous key state
    	ucKeyStatePrev = ucKeyStateCurr;

    	// Get current key state
    	ucKeyStateCurr = CKeyScan();

    	// Power key process
    	CKeyPowerKeyProc();
}

//--------------------------------------------------
// Description  : Initial key status
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void CKeyInitial(void)
{
	CLR_KEYSCANREADY();
	CLR_KEYSCANSTART();

	CLR_KEYREPEATENABLE();	//Add by zhang_dc for key initial.
}

//--------------------------------------------------
// Description  : Key scan ready process. We wait 0.02 sec in order to keep the keypad debounce
// Input Value  : None
// Output Value : None
//--------------------------------------------------
bit CKeyScanReady(void)
{
    	if(GET_KEYSCANSTART() && GET_KEYSCANREADY())
    	{
        	CLR_KEYSCANSTART();
        	CLR_KEYSCANREADY();

        	return _TRUE;
    	}
    	else if(!GET_KEYSCANSTART())
    	{
        	// Wait 0.02 sec in order to keep the keypad debounce
        	SET_KEYSCANSTART();
        	CTimerReactiveTimerEvent(SEC(0.02), CKeyScanReadyTimerEvent);

        	return _FALSE;
    	}
    	else
    	{
        	return _FALSE;
    	}
}

//--------------------------------------------------
// Description  : Key scan ready timer event
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void CKeyScanReadyTimerEvent(void)
{
    	SET_KEYSCANREADY();
}

//--------------------------------------------------
// Description  : Key repeat enable timer event
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void CKeyRepeatEnableTimerEvent(void)
{
    	SET_KEYREPEATSTART();
}

//--------------------------------------------------
// Description  : Key message translation
// Input Value  : ucKeyMask     --> Key mask
//                ucKeyMsg      --> Key message
// Output Value : None
//--------------------------------------------------
void CKeyMessageConvert(BYTE ucKeyMask, BYTE ucKeyMsg)
{
    	if((ucKeyStatePrev ^ ucKeyStateCurr) & ucKeyMask)
    	{
        	ucKeyMessage = ucKeyMsg;
    	}
    	else
    	{
        	if(GET_KEYREPEATENABLE())
        	{
	            	if(GET_KEYREPEATSTART())
	            	{
	                	ucKeyMessage = ucKeyMsg;
						//x.hu
						//if menu key is able to repeat, you should decrease 
						// the repeating times in one second, so do this:
						#if 0//(_MENU_KEY_REPEAT_OPTION == _TRUE) 
							if (ucKeyMessage == _MENU_KEY_MESSAGE) {
								if (_DELAY_MENU_REPEAT) {
									CTimerDelayXms(_DELAY_MENU_REPEAT);
	                				//CTimerActiveTimerEvent(SEC(0.6),CKeyRepeatEnableTimerEvent);
								}
							}
						#endif
	            	}
	            	else
	            	{
	                	CTimerActiveTimerEvent(SEC(_KEY_REPEAT_START_TIME),CKeyRepeatEnableTimerEvent);
	            	}
        	}
    	}
}

//--------------------------------------------------
// Description  : Power key process
// Input Value  : None
// Output Value : Return _TRUE if power key is pressed
//--------------------------------------------------
bit CKeyPowerKeyProc(void)
{
    	if(ucKeyStateCurr & _POWER_KEY_MASK)						//if current key is power key
    	{
        	if((ucKeyStatePrev ^ ucKeyStateCurr) & _POWER_KEY_MASK)	//if previous key is power key too
        	{
            		//CTimerDelayXms(25);	//x.hu
            		CTimerDelayXms(10);
            		ucKeyStateCurr = CKeyScan();		//get key again

            		if((ucKeyStatePrev ^ ucKeyStateCurr) & _POWER_KEY_MASK) //if still power key again
            		{
                		CKeyPowerKeyMix();

                		SET_POWERSWITCH();		//set power process flag

                		return _TRUE;				//confirm power process
            		}
        	}
    	}

    	return _FALSE;		//if only current key is power key, disable power process flag
}


//x.hu, for source switch
BYTE CSourceGetNext(void)
{
	if (stSystemData.InputSource>=(_INPUT_PORT_MAX-1))
		return _INPUT_PORT_0;
	
	return (stSystemData.InputSource+1);
}
//--------------------------------------------------
// Description  : Source key process
// Input Value  : None
// Output Value : Return _TRUE if source key is pressed
//--------------------------------------------------
bit CKeySourceKeyProc(void)
{
	#if (_SOURCE_KEY_TYPE == _SOURCE_KEY_MULTIKEY || _SOURCE_KEY_TYPE == _SOURCE_KEY_MIXKEY)
   	if((ucKeyStateCurr & _VIDEO1_KEY_MASK) || (ucKeyStateCurr & _VIDEO2_KEY_MASK))
   	{
       	if((ucKeyStatePrev ^ ucKeyStateCurr) & _VIDEO1_KEY_MASK)
       	{
       		CTimerDelayXms(10);
       		ucKeyStateCurr = CKeyScan();
      		if((ucKeyStatePrev ^ ucKeyStateCurr) & _VIDEO1_KEY_MASK)
       		{
      			SET_SOURCESTATUS(_INPUT_PORT_0+1);	// set 1 as request source for _INPUT_PORT_0
          		return _TRUE;
       		}
      	}
		else if((ucKeyStatePrev ^ ucKeyStateCurr) & _VIDEO2_KEY_MASK){
       		CTimerDelayXms(10);
       		ucKeyStateCurr = CKeyScan();
      		if((ucKeyStatePrev ^ ucKeyStateCurr) & _VIDEO2_KEY_MASK)
      		{
       			SET_SOURCESTATUS(_INPUT_PORT_1+1);	// set 2 as request source for _INPUT_PORT_1
            	return _TRUE;
            }
		}
    }
	#endif	//end of #if (_SOURCE_KEY_TYPE == _SOURCE_KEY_MULTIKEY || _SOURCE_KEY_TYPE == _SOURCE_KEY_MIXKEY)
	
	#if  (_SOURCE_KEY_TYPE == _SOURCE_KEY_ONEKEY || _SOURCE_KEY_TYPE == _SOURCE_KEY_MIXKEY)
   	if(ucKeyStateCurr & _SOURCE_KEY_MASK)
   	{
       	if((ucKeyStatePrev ^ ucKeyStateCurr) & _SOURCE_KEY_MASK)
       	{
       		CTimerDelayXms(10);
       		ucKeyStateCurr = CKeyScan();
      		if((ucKeyStatePrev ^ ucKeyStateCurr) & _SOURCE_KEY_MASK)
       		{

			
      			SET_SOURCESTATUS(CSourceGetNext()+1);	// set 1 as request source for _INPUT_PORT_0
          		return _TRUE;
       		}
      	}
    }
	#endif	//end of #if (_SOURCE_KEY_TYPE == _SOURCE_KEY_ONEKEY || _SOURCE_KEY_TYPE == _SOURCE_KEY_MIXKEY)
	
    return _FALSE;	// if no any source switch key is found, then return _FALSE

}


//--------------------------------------------------
// Description  : Get key status
// Input Value  : None
// Output Value : Return Key status
//--------------------------------------------------
#if(_PCB_TYPE == _PCB_PTX23A21)
BYTE ReadADPort(BYTE ADPort)		//for fixing ADC value error. Add by zhang_dc
{
	register BYTE key_value[2];
	register BYTE i;

	do
	{
		for(i=0; i<2; i++)
		{
			MCU_ADC_F10 = ADPort;
			CTimerDelayXms(2);
			key_value[i] = MCU_ADC_F10 & 0x3f;
		}
	}
	while(key_value[0] != key_value[1]);

	return key_value[0];
}
#endif

BYTE CKeyScan(void)
{
	BYTE ucKeyState = 0;
// evan 050511
#if(_PCB_TYPE == _PCB_PTX23A21)
    BYTE ucKeyValue = 0;

	ucKeyState = 0;
	ucKeyValue = ReadADPort(ADCHANNEL1);
	
	if(ucKeyValue<10)
		ucKeyState    = ucKeyState | _POWER_KEY_MASK;
	else if(ucKeyValue>14 && ucKeyValue<35)
		ucKeyState    = ucKeyState | _MENU_KEY_MASK;
	else if(ucKeyValue>35 && ucKeyValue<50)
		ucKeyState    = ucKeyState | _EXIT_KEY_MASK; 

	if(ucKeyState)
		return ucKeyState;
	
	ucKeyValue = ReadADPort(ADCHANNEL2);
	
	if(ucKeyValue<10)
		ucKeyState    = ucKeyState | _RIGHT_KEY_MASK;
	else if(ucKeyValue>14 && ucKeyValue<35)
		ucKeyState    = ucKeyState | _LEFT_KEY_MASK;
	
	//return ucKeyState;	//x.hu, for IR
	if (ucKeyState)
		return ucKeyState;
	#if (_IRPOLLING)
	ucKeyState = CIrKeyScan();
	if (ucKeyState)
		return ucKeyState;

	return 0;
	#endif


#elif ( _PCB_TYPE == _PCB_PV013BA1 || \
		_PCB_TYPE == _PCB_RIGHTECH_A )

    if(!bPOWER_KEY)
       	ucKeyState    	= ucKeyState | _POWER_KEY_MASK;
    /*else*/ if(!bMENU_KEY)
       	ucKeyState    	= ucKeyState | _MENU_KEY_MASK;
    /*else*/ if(!bLEFT_KEY)
       	ucKeyState    	= ucKeyState | _LEFT_KEY_MASK;
   	/*else*/ if(!bRIGHT_KEY) 
       	ucKeyState    	= ucKeyState | _RIGHT_KEY_MASK;
	#if (_SOURCE_KEY_TYPE == _SOURCE_KEY_ONEKEY)	// source switch one key from keypad
	/*else*/ if (!bSource)
		ucKeyState		= ucKeyState | _SOURCE_KEY_MASK;
	#endif
	#if (_SOURCE_KEY_TYPE == _SOURCE_KEY_MULTIKEY || _SOURCE_KEY_TYPE == _SOURCE_KEY_MIXKEY)
    /*else*/ if(!bVIDEO1)								// source switch multi-key from keypad, but one key from IR
       	ucKeyState    	= ucKeyState | _VIDEO1_KEY_MASK;
    /*else*/ if(!bVIDEO2) 
       	ucKeyState    	= ucKeyState | _VIDEO2_KEY_MASK; 
	#endif
	if (ucKeyState)
	{

⌨️ 快捷键说明

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