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

📄 mst705Ȧ

📁 MST705源代码
💻
字号:
#define _KEYPAD_C_

#include <math.h>
#include "types.h"
#include "board.h"
#include "ms_reg.h"
#include "global.h"
#include "keypaddef.h"
#include "DEBUG.h"
#include "misc.h"
#include "ms_rwreg.h"
#include "Reg52.h"
#include "ir.h"
#include "menu.h"
#include "keypad.h"

//===============================================================
#define MAX_KEYVALUE        0x3F
#define HIGHLEVEL           0x3A
#define KeyJitterCounter	15
#define KeyStableCounter	1
#define KEY_Idle            0xFF

/*
#if(MARIA9_VERSION==MARIA9_B)
WORD Key_ScanValue(BYTE KeyAdcPort)
{
    BYTE ucBank,ucSARhigh6Bit,ucSARlow4Bit;
    ucBank = msReadByte(BK0_00_REGBK);

    if(KeyAdcPort==ADCKEY1)
    {
        msWriteByte(BK0_00_REGBK, REG_BANK7_CHIPTOP);
        ucSARlow4Bit = msReadByte(BK7_9E_CHIPTOP);
        ucSARhigh6Bit = PKEYADC1&0x3F;
        g_ucCurrentKeyADC[KeyAdcPort]=((WORD)ucSARhigh6Bit<<4)|(ucSARlow4Bit&0x0F);
        //if(g_ucCurrentKeyADC[KeyAdcPort]<0x3e0)
            //printf("\r\nPKEYADC1=0x%x",g_ucCurrentKeyADC[KeyAdcPort]);
    }
    else
    {
        msWriteByte(BK0_00_REGBK, REG_BANK7_CHIPTOP);
        ucSARlow4Bit = msReadByte(BK7_9E_CHIPTOP);
        ucSARhigh6Bit = PKEYADC1&0x3F;
        g_ucCurrentKeyADC[KeyAdcPort]=((WORD)ucSARhigh6Bit<<4)|((ucSARlow4Bit>>4)&0x0F);
        //if(g_ucCurrentKeyADC[KeyAdcPort]<0x3e0)
            //printf("\r\nPKEYADC1=0x%x",g_ucCurrentKeyADC[KeyAdcPort]);
    }

    msWriteByte(BK0_00_REGBK, ucBank);

    return g_ucCurrentKeyADC[KeyAdcPort];
}
#endif
*/
#ifdef USE_ARRAY_KEY

enum
{
	PANEL_KEY_01,	
	PANEL_KEY_02,	
	PANEL_KEY_03,	
	PANEL_KEY_04,	
	PANEL_KEY_05,	
	PANEL_KEY_06,	
	PANEL_KEY_07,	
	PANEL_KEY_08,	
	PANEL_KEY_09,	
	PANEL_KEY_10,	
	PANEL_KEY_11,	
	PANEL_KEY_12,	
	PANEL_KEY_13,	
	PANEL_KEY_14,	
	PANEL_KEY_15,	
	PANEL_KEY_16,
	PANEL_KEY_17,
	PANEL_KEY_18,
	PANEL_KEY_MAX	
};

code BYTE g_ucPanelkeyMap[PANEL_KEY_MAX]={
/* K01 */  KEY_SOURCE  
/* K02 */ ,KEY_DOWN
/* K03 */ ,KEY_MENU 
/* K04 */ ,KEY_POWER 
/* K05 */ ,KEY_LEFT 
/* K06 */ ,KEY_RIGHT 
/* K07 */ ,KEY_SOURCE 
/* K08 */ ,KEY_IR_0
/* K09 */ ,KEY_IR_1 
/* K10 */ ,KEY_IR_2 
/* K11 */ ,KEY_IR_3 
/* K12 */ ,KEY_IR_4  
/* K13 */ ,KEY_IR_5  
/* K14 */ ,KEY_IR_6 
/* K15 */ ,KEY_IR_7  
/* K16 */ ,KEY_IR_8  
/* K17 */ ,KEY_IR_9  
/* K18 */ ,KEY_IR_100
};

#define CLKIN_PIN	0
#define VD0_PIN		1
#define VD1_PIN		2
#define VD2_PIN		3
#define VD3_PIN		4

BYTE bReadKeyPort(BYTE ucVal)    //use bit?
{
    BYTE ucValue;
	
	if (ucVal == CLKIN_PIN)
	{
    	ucValue = Hw_ReadKeyPin(_BIT0) >> 0; //CLKIN_PIN	
	}
	else if (ucVal == VD0_PIN)
	{
    	ucValue = Hw_ReadKeyPin(_BIT1) >> 1; //VD0_PIN	
	}
	else if (ucVal == VD1_PIN)
	{
    	ucValue = Hw_ReadKeyPin(_BIT2) >> 2; //VD1_PIN	
	}
	else if (ucVal == VD2_PIN)
	{
    	ucValue = Hw_ReadKeyPin(_BIT3) >> 3; //VD2_PIN	
	}
	else if (ucVal == VD3_PIN)
	{
    	ucValue = Hw_ReadKeyPin(_BIT4) >> 4; //VD3_PIN	
	}
	else
	{
		ucValue = 1;
	}

	return (ucValue);
}
BYTE bReadSetKeyPort(BYTE ucVal)    //use bit?
{
    BYTE ucValue;
	
	if (ucVal == CLKIN_PIN)
	{
    	ucValue = Hw_ReadSetKeyPin(_BIT0) >> 0; //CLKIN_PIN	
	}
	else if (ucVal == VD0_PIN)
	{
    	ucValue = Hw_ReadSetKeyPin(_BIT1) >> 1; //VD0_PIN	
	}
	else if (ucVal == VD1_PIN)
	{
    	ucValue = Hw_ReadSetKeyPin(_BIT2) >> 2; //VD1_PIN	
	}
	else if (ucVal == VD2_PIN)
	{
    	ucValue = Hw_ReadSetKeyPin(_BIT3) >> 3; //VD2_PIN	
	}
	else if (ucVal == VD3_PIN)
	{
    	ucValue = Hw_ReadSetKeyPin(_BIT4) >> 4; //VD3_PIN	
	}
	else
	{
		ucValue = 1;
	}

	return (ucValue);
}
void vSetKeyPinInit(void)
{
	Hw_SetClkin(1);
	Hw_SetVd0(1);
	Hw_SetVd1(1);
	Hw_SetVd2(1);
	Hw_SetVd3(1);
}

BYTE bGetKeyPadId(void)
{
    //BYTE ucTempId,ucValue;

	vSetKeyPinInit();
// 	delay10us(1);

////hight priority keypad,directly connect to GND
	if (!bReadKeyPort(CLKIN_PIN)) // ClkIn & Gnd
	{
		return (PANEL_KEY_01);
	}
	else if (!bReadKeyPort(VD0_PIN)) // Vd0 & Gnd
	{
		return (PANEL_KEY_02);
	}
	else if (!bReadKeyPort(VD1_PIN)) // Vd1 & Gnd
	{
		return (PANEL_KEY_03);
	}
	else if (!bReadKeyPort(VD2_PIN)) // Vd2 & Gnd
	{
		return (PANEL_KEY_04);
	}
	else if (!bReadKeyPort(VD3_PIN)) // Vd3 & Gnd
	{
		return (PANEL_KEY_05);
	}
	else
	{
		Hw_SetClkin(0);
// 		delay10us(1);
		
		if (!bReadKeyPort(VD0_PIN))
		{		
		       if (!bReadKeyPort(VD1_PIN)) // ClkIn & Vd0 & Vd1
			{
				return (PANEL_KEY_MAX);
			}
			else if (!bReadKeyPort(VD2_PIN)) // ClkIn & Vd0 & Vd2 
			{
				return (PANEL_KEY_MAX);
			}
			else if (!bReadKeyPort(VD3_PIN)) // ClkIn & Vd0 & Vd3  
			{
				return (PANEL_KEY_MAX);
			}
			else  // ClkIn & Vd0
			{	
				if((bReadSetKeyPort(CLKIN_PIN)==0)&(bReadKeyPort(VD0_PIN)))
				{
					return (PANEL_KEY_06);//
				}
				else
					return (PANEL_KEY_02);	
			}
		}
		else if (!bReadKeyPort(VD1_PIN))
		{
			if (!bReadKeyPort(VD2_PIN)) // ClkIn & Vd1 & Vd2
			{
				return (PANEL_KEY_MAX);
			}
			else if (!bReadKeyPort(VD3_PIN)) // ClkIn & Vd1 & Vd3 
			{
				return (PANEL_KEY_MAX);
			}
			else // ClkIn & Vd1
			{
				if((0==bReadSetKeyPort(CLKIN_PIN))&(bReadKeyPort(VD1_PIN)))
				{
					return (PANEL_KEY_07);//
				}
				else
					return (PANEL_KEY_03);
			}
		}
		else if (!bReadKeyPort(VD2_PIN))
		{
			if (!bReadKeyPort(VD3_PIN)) // ClkIn & Vd2 & Vd3
			{
				return (PANEL_KEY_MAX);
			}
			else // ClkIn & Vd2
			{
				if((0==bReadSetKeyPort(CLKIN_PIN))&(bReadKeyPort(VD2_PIN)))
				{
					return (PANEL_KEY_08);//
				}
				else
					return (PANEL_KEY_04);
			}
		}
		else if (!bReadKeyPort(VD3_PIN))// ClkIn & Vd3 
		{
				if((0==bReadSetKeyPort(CLKIN_PIN))&(bReadKeyPort(VD3_PIN)))
				{
					return (PANEL_KEY_09);//
				}
				else
					return (PANEL_KEY_05);
		}
		else
		{
			vSetKeyPinInit();
 //			delay10us(1); 
			Hw_SetVd0(0);
 //			delay10us(1);

			if (!bReadKeyPort(VD1_PIN))
			{
				if (!bReadKeyPort(VD2_PIN)) // Vd0 & Vd1 & Vd2 
				{
					return (PANEL_KEY_16);
				}
				else if (!bReadKeyPort(VD3_PIN)) // Vd0 & Vd1 & Vd3 
				{
					return (PANEL_KEY_MAX);
				}
				else // Vd0 & Vd1
				{
					if((0==bReadSetKeyPort(VD0_PIN))&(bReadKeyPort(VD1_PIN)))
					{
						return (PANEL_KEY_10);//
					}
					else
						return (PANEL_KEY_03);
					
				}
			}
			else if (!bReadKeyPort(VD2_PIN))
			{
				if (!bReadKeyPort(VD3_PIN)) // Vd0 & Vd2 & Vd3 
				{
					return (PANEL_KEY_17);//20110607未分析
				}
				else // Vd0 & Vd2 
				{
					if((0==bReadSetKeyPort(VD0_PIN))&(bReadKeyPort(VD2_PIN)))
					{
						return (PANEL_KEY_11);//
					}
					else
						return (PANEL_KEY_04);
				}
			}
			else if (!bReadKeyPort(VD3_PIN)) // Vd0 & Vd3
			{
					if((0==bReadSetKeyPort(VD0_PIN))&(bReadKeyPort(VD3_PIN)))
					{
						return (PANEL_KEY_12);//
					}
					else
						return (PANEL_KEY_05);				
			}
			else
			{
				vSetKeyPinInit();
//	 			delay10us(1);
				Hw_SetVd1(0);
//	 			delay10us(1);
				
				if (!bReadKeyPort(VD2_PIN))
				{
					if (!bReadKeyPort(VD3_PIN)) // Vd1 & Vd2 & Vd3 
					{
						return (PANEL_KEY_18);//20110607未分析
					}
					else // Vd1 & Vd2
					{
						if((0==bReadSetKeyPort(VD1_PIN))&(bReadKeyPort(VD2_PIN)))
						{
							return (PANEL_KEY_13);//
						}
						else
							return (PANEL_KEY_04);					
					}
				}
				else if (!bReadKeyPort(VD3_PIN)) // Vd1 & Vd3 
				{
					if((0==bReadSetKeyPort(VD1_PIN))&(bReadKeyPort(VD3_PIN)))
					{
						return (PANEL_KEY_14);//
					}
					else
						return (PANEL_KEY_05);					
				}
				else
				{
					vSetKeyPinInit();
//		 			delay10us(1);
					Hw_SetVd2(0);
//		 			delay10us(1);

					if (!bReadKeyPort(VD3_PIN)) // Vd2 & Vd3 
					{
						if((0==bReadSetKeyPort(VD2_PIN))&(bReadKeyPort(VD3_PIN)))
						{
							return (PANEL_KEY_15);//
						}
						else
							return (PANEL_KEY_05);					
					}
				}
			}
		}
	}

	return (PANEL_KEY_MAX);
}

XDATA BYTE g_ucLastKeyIndex;
BYTE bGetKeyPadStatus(void)
{
    BYTE ucValue;
    ucValue = bGetKeyPadId();
	if (ucValue != PANEL_KEY_MAX)
	{
	    printf("\r\nGetKeyId:<%x>", ucValue);
		if(g_ucLastKeyIndex != ucValue)
		{
		    g_ucKeyCounter = KeyJitterCounter;
			g_ucLastKeyIndex = ucValue;
		    return (KEY_Idle);
		}

		g_ucKeyCounter--;
		if (!g_ucKeyCounter)
		{
			g_ucLastKeyIndex = PANEL_KEY_MAX;
    		printf("\r\nGetKey:<%x>", g_ucPanelkeyMap[ucValue]);
		    return (g_ucPanelkeyMap[ucValue]);			
		}
	}

    return (KEY_Idle);
}

#if 0
void vGpioTest(void)
{	
	BYTE ucTempValue;
	
	ucTempValue |= Hw_ReadKeyPin(_BIT0); //CLKIN_PIN	
	ucTempValue |= Hw_ReadKeyPin(_BIT1); //VD0_PIN	
	ucTempValue |= Hw_ReadKeyPin(_BIT2); //VD1_PIN	
	ucTempValue |= Hw_ReadKeyPin(_BIT3); //VD2_PIN	
	ucTempValue |= Hw_ReadKeyPin(_BIT4); //VD3_PIN	

 	delay10us(100);
	printf("\r\nBK7_20_CHIPTOP:<%x>", ucTempValue);
}
#endif

void Key_ScanKeypad(void)
{
    BYTE keypadStatus;

    keypadStatus = bGetKeyPadStatus();//Key_GetKeypadStatus();
	//vGpioTest();
	keypadStatus = KEY_Idle;
    if (keypadStatus!=KEY_Idle)
    {
        if(keypadStatus!=KEY_NOTHING)
        {
            printf("\r\nKEY<%x>", keypadStatus);

            if (keypadStatus == g_ucLastKeypad)
            {
                //printMsg("repeat key pad");
                EventRepeatProcess(keypadStatus, 2);
            }
            else
            {
                //printMsg("Single key pad");
                EventProcess(keypadStatus);
            }
        }
        g_ucLastKeypad = keypadStatus;
        Key_KeyInit();
    }
}

#else

BYTE Key_ScanValue(BYTE KeyAdcPort)
{
	if(KeyAdcPort==ADCKEY1)
		g_ucCurrentKeyADC[KeyAdcPort]=PKEYADC1;
	else
		g_ucCurrentKeyADC[KeyAdcPort]=PKEYADC2;

	return g_ucCurrentKeyADC[KeyAdcPort];
}

BYTE Key_GetKeypadStatus(void)
{
    //BYTE KeyData = KEY_NOTHING;
//#if(MARIA9_VERSION==MARIA9_B)
//    WORD ADCValue;
//#else
    BYTE ADCValue;
//#endif

#if(KEY_TYPE==Single_ADCKey)
//------------------------------------------------
		ADCValue=Key_ScanValue(ADCKEY1);

		if (ADCValue < (KEY_AD_L0 + KEY_AD_DELTA))
		    {
		    if(g_ucKeyStableCountBuff[KEY_POWER]++>KeyStableCounter)
		        {
        		    return KEY_POWER;
		        }
		    }
    	else if (ADCValue < (KEY_AD_L1+KEY_AD_DELTA)&&ADCValue > (KEY_AD_L1 - KEY_AD_DELTA))
    	    {
		    if(g_ucKeyStableCountBuff[KEY_SOURCE]++>KeyStableCounter)
		        {
        		    return KEY_SOURCE;
		        }
    	    }
    	else if (ADCValue < (KEY_AD_L2+KEY_AD_DELTA)&&ADCValue > (KEY_AD_L2 - KEY_AD_DELTA))
    	    {
		    if(g_ucKeyStableCountBuff[KEY_MENU]++>KeyStableCounter)
		        {
        		    return KEY_MENU;
		        }
    	    }
    	else if (ADCValue < (KEY_AD_L3+KEY_AD_DELTA)&&ADCValue > (KEY_AD_L3 - KEY_AD_DELTA))
    	    {
		    if(g_ucKeyStableCountBuff[KEY_RIGHT]++>KeyStableCounter)
		        {
        		    return KEY_RIGHT;
		        }
    	    }
    	else if (ADCValue < (KEY_AD_L4+KEY_AD_DELTA)&&ADCValue > (KEY_AD_L4 - KEY_AD_DELTA))
    	    {
		    if(g_ucKeyStableCountBuff[KEY_LEFT]++>KeyStableCounter)
		        {
        		    return KEY_LEFT;
		        }
    	    }
    	else if (ADCValue < (KEY_AD_L5+KEY_AD_DELTA)&&ADCValue > (KEY_AD_L5 - KEY_AD_DELTA))
    	    {
		    if(g_ucKeyStableCountBuff[KEY_UP]++>KeyStableCounter)
		        {
        		    return KEY_UP;
		        }
    	    }
    	else if (ADCValue < (KEY_AD_L6+KEY_AD_DELTA)&&ADCValue > (KEY_AD_L6 - KEY_AD_DELTA))
    	    {
		    if(g_ucKeyStableCountBuff[KEY_DOWN]++>KeyStableCounter)
		        {
        		    return KEY_DOWN;
		        }
    	    }

#elif(KEY_TYPE==Dual_ADCKey)
//------------------------------------------------
		ADCValue=Key_ScanValue(ADCKEY1);
		if (ADCValue < (KEY_AD_L0 + KEY_AD_DELTA))
		    {
		    if(g_ucKeyStableCountBuff[KEY_RIGHT]++>KeyStableCounter)
		        {
        		    return KEY_RIGHT;
		        }
		    }
    	else if (ADCValue < (KEY_AD_L1+KEY_AD_DELTA)&&ADCValue > (KEY_AD_L1 - KEY_AD_DELTA))
    	    {
		    if(g_ucKeyStableCountBuff[KEY_LEFT]++>KeyStableCounter)
		        {
        		    return KEY_LEFT;
		        }
    	    }
    	else if (ADCValue < (KEY_AD_L2+KEY_AD_DELTA)&&ADCValue > (KEY_AD_L2 - KEY_AD_DELTA))
    	    {
		    if(g_ucKeyStableCountBuff[KEY_UP]++>KeyStableCounter)
		        {
        		    return KEY_UP;
		        }
    	    }
    	else if (ADCValue < (KEY_AD_L3+KEY_AD_DELTA)&&ADCValue > (KEY_AD_L3 - KEY_AD_DELTA))
    	    {
		    if(g_ucKeyStableCountBuff[KEY_DOWN]++>KeyStableCounter)
		        {
        		    return KEY_DOWN;
		        }
    	    }

		ADCValue=Key_ScanValue(ADCKEY2);
		if (ADCValue < (KEY_AD_L0 + KEY_AD_DELTA)&&ADCValue > (KEY_AD_L0 - KEY_AD_DELTA))
		    {
		    if(g_ucKeyStableCountBuff[KEY_POWER]++>KeyStableCounter)
		        {
        		    return KEY_POWER;
		        }
		    }
    	else if (ADCValue < (KEY_AD_L1+KEY_AD_DELTA)&&ADCValue > (KEY_AD_L1 - KEY_AD_DELTA))
    	    {
		    if(g_ucKeyStableCountBuff[KEY_MENU]++>KeyStableCounter)
		        {
        		    return KEY_MENU;
		        }
    	    }
    	else if (ADCValue < (KEY_AD_L2+KEY_AD_DELTA)&&ADCValue > (KEY_AD_L2 - KEY_AD_DELTA))
    	    { //printf("\r\nADCValue===<%x>", ADCValue);
		    if(g_ucKeyStableCountBuff[KEY_SOURCE]++>KeyStableCounter)
		        {
        		    return KEY_SOURCE;
		        }
    	    }
#endif

		if(!g_ucKeyCounter)
            return KEY_NOTHING;

        g_ucKeyCounter--;

        return KEY_Idle;
}

void Key_ScanKeypad(void)
{
    BYTE keypadStatus;

    keypadStatus=KEY_Idle;//Key_GetKeypadStatus();

    if (keypadStatus!=KEY_Idle)
    {
        if(keypadStatus!=KEY_NOTHING)
        {
            printf("\r\nKEY<%x>", keypadStatus);

            if (keypadStatus == g_ucLastKeypad)
            {
                //printMsg("repeat key pad");
                EventRepeatProcess(keypadStatus, 2);
            }
            else
            {
                //printMsg("Single key pad");
                EventProcess(keypadStatus);
            }
        }
        g_ucLastKeypad = keypadStatus;
        Key_KeyInit();
    }
}
#endif

void Key_KeyInit(void)
{
    g_ucKeyCounter=KeyJitterCounter;

    g_ucKeyStableCountBuff[0]=0;
    g_ucKeyStableCountBuff[1]=0;
    g_ucKeyStableCountBuff[2]=0;
    g_ucKeyStableCountBuff[3]=0;
    g_ucKeyStableCountBuff[4]=0;
    g_ucKeyStableCountBuff[5]=0;
    g_ucKeyStableCountBuff[6]=0;
    g_ucKeyStableCountBuff[7]=0;
}

void CheckFactoryKeyStatus(void)
{
	BYTE keypadStatus, i;

#ifndef USE_ARRAY_KEY
	for(i=0;i<10;i++)
	{
  		keypadStatus=Key_GetKeypadStatus();
	}
#endif

  	Clr_FactoryModeFlag();

  	if (keypadStatus==KEY_MENU)
    	{
    		Set_FactoryModeFlag();
      		Set_PowerOnFlag();
    	}
}

⌨️ 快捷键说明

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