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

📄 keypadtest.c

📁 pxa270矩阵键盘的驱动代码
💻 C
📖 第 1 页 / 共 2 页
字号:
    if(kpc & KEYP_KPC_DI)
    {
    	UINT32 val = KPDK_REG;
    	
    	if(val & (1UL<<31))
    	{
    		if(val & (1<<0))
    			return 'E';
    		if(val & (1<<1))
    			return 'F';
    		if(val & (1<<2))
    			return '0';
    	}
    }

    KPData0 = KPAS_REG;
    KPData1 = (KPData0 & 0xF0) >> 4;           // Extract Row data and right justify
	KPPress = (KPData0 & 0x7C000000) >> 26;    // Extract key press count and right justify
	//printf("hello");
	KPData0 = KPData0 & 0xF;                   // Isolate Column Data
//	printf("KPData0 = %08x\r\n", KPData0);
//	printf("Key Count = %08x\r\n", KPPress);
    KeyData = 0x0A;
      if (KPPress == 1)                         // Decode only if 1 keypress has been detected
	{
    	switch (KPData0)
		{
	    case 0:
			if(KPData1 == 0) KeyData = 'A';
			if(KPData1 == 1) KeyData = '3';
			if(KPData1 == 2) KeyData = '2';
			if(KPData1 == 3) KeyData = '1';
			break;

	    case 1:
			if(KPData1 == 0) KeyData = 'B';
			if(KPData1 == 1) KeyData = '6';
			if(KPData1 == 2) KeyData = '5';
			if(KPData1 == 3) KeyData = '4';
			break;

		case 2:
			if(KPData1 == 0) KeyData = 'C';
			if(KPData1 == 1) KeyData = '9';
			if(KPData1 == 2) KeyData = '8';
			if(KPData1 == 3) KeyData = '7';
			break;
		}
	}
	return (KeyData);
}
/*
*******************************************************************************
*
* FUNCTION:
*    FastTapGetData
*
* DESCRIPTION:
*    translate a keypress into a key
*
* INPUT PARAMETERS:
*    None.
*
* RETURNS:
*    None.
*
* GLOBAL EFFECTS:
*    None.
*
* ASSUMPTIONS:
*    None.
*
* CALLS:
*    None.
*
* CALLED BY:
*    Keypad test main function.
*
* PROTOTYPE:
*    char FastTapGetData(void);
*
*******************************************************************************
*/
/*

		C0		C1		C2		C3		C4		C5		C6
R0		A		B		C		D		E		F		SEND
			1		!		2		?		3		[]
R1		G		H		I		J		K		L		END
			4		$		5		&		6		[]
R2		M		N		O		P		Q		R		SOFT1
			7		:		8		;		9		[]
R3		S		T		U		V		W		X		SOFT2
			*		'		0		,		#		[]
R4		.		@		Y		Z		/		\		[]
			TAB		(		DEL		)		ENTER	[]
R5		HOME	SHIFT	SPACE	SPACE	CTRL	BACK	[]
			[]		[]		[]		[]		[]		[]	
R6		UP		DOWN	LEFT	RIGHT	ACTION	[]		[]

*/

char FastTapGetData(void)
{
char KeyData;             // Character from Keypad
unsigned int KPData0;     // Register data
unsigned int KPData1;     // Register data
unsigned int KPPress;     // Keypad press count
int timer;                // Loop coutner/timer (for test timeout)

    KeyData = 0x30;                           // Inital setting = ASCII ?
    timer = 200;                              // This will provide about a 10 second timeout

    while (((KPC_REG & KEYP_KPC_MI) != KEYP_KPC_MI) & (timer != 0))
    {
    	DM_WaitMs(50);            // Dealy 50 mS as part of timeout loop
		timer = timer -1;         // Decrement timer
    }

	if (timer == 0)
	{
		KeyData = 0x7E;     // ASCII "~" for Time out
		return (KeyData);
	}

    // Code picks up here if a key is pressed

    DM_WaitMs(50);   // Dealy 50 mS for data to settle

    KPData0 = KPAS_REG;
    KPData1 = (KPData0 & 0xF0) >> 4;           // Extract Row data and right justify
	KPPress = (KPData0 & 0x7C000000) >> 26;    // Extract key press count and right justify
	KPData0 = KPData0 & 0xF;                   // Isolate Column Data
    
   // if (POSTRunState() & VS_DEBUG_ON)
	{
	    printf("KPData0 = %08x\r\n", KPData0);
		printf("Key Count = %08x\r\n", KPPress);
    }
    
    KeyData = 0xA;
    if (KPPress == 1)     // Decode only if 1  keypress has been detected
	{
    	switch (KPData0) // now KPData0 is the column data
		{
	    case 0:   // If key a,b,c,d,e,f, SEND
			if(KPData1 == 0) KeyData = KEY_A;
			if(KPData1 == 1) KeyData = KEY_B;
			if(KPData1 == 2) KeyData = KEY_C;
			if(KPData1 == 3) KeyData = KEY_D;
			if(KPData1 == 4) KeyData = KEY_E;
			if(KPData1 == 5) KeyData = KEY_F;
			if(KPData1 == 6) KeyData = KEY_SND;
			break;

	    case 1:   // If key g,h,i,j,k,l, END
			if(KPData1 == 0) KeyData = KEY_G;
			if(KPData1 == 1) KeyData = KEY_H;
			if(KPData1 == 2) KeyData = KEY_I;
			if(KPData1 == 3) KeyData = KEY_J;
			if(KPData1 == 4) KeyData = KEY_K;
			if(KPData1 == 5) KeyData = KEY_L;
			if(KPData1 == 6) KeyData = KEY_END;
			break;

		case 2:   // If key M,N,O,P,Q,R, SOFT 1
			if(KPData1 == 0) KeyData = KEY_M;
			if(KPData1 == 1) KeyData = KEY_N;
			if(KPData1 == 2) KeyData = KEY_O;
			if(KPData1 == 3) KeyData = KEY_P;
			if(KPData1 == 4) KeyData = KEY_Q;
			if(KPData1 == 5) KeyData = KEY_R;
			if(KPData1 == 6) KeyData = KEY_SF1;
			break;

	    case 3:   // If key S,T,U,V,W,X, SOFT2
			if(KPData1 == 0) KeyData = KEY_S;
			if(KPData1 == 1) KeyData = KEY_T;
			if(KPData1 == 2) KeyData = KEY_U;
			if(KPData1 == 3) KeyData = KEY_V;
			if(KPData1 == 4) KeyData = KEY_W;
			if(KPData1 == 5) KeyData = KEY_X;
			if(KPData1 == 6) KeyData = KEY_SF2;
			break;

	    case 4:   // If key . @ Y Z and slashes
			if(KPData1 == 0) KeyData = KEY_PER;
			if(KPData1 == 1) KeyData = KEY_AT;
			if(KPData1 == 2) KeyData = KEY_Y;
			if(KPData1 == 3) KeyData = KEY_Z;
			if(KPData1 == 4) KeyData = KEY_FS;
			if(KPData1 == 5) KeyData = KEY_BS;
			break;

	    case 5:   //HOME thru BACK
			if(KPData1 == 0) KeyData = KEY_HOME;
			if(KPData1 == 1) KeyData = KEY_SHIFT;
			if(KPData1 == 2) KeyData = KEY_SPACE;
			if(KPData1 == 3) KeyData = KEY_SPACE;
			if(KPData1 == 4) KeyData = KEY_CTRL;
			if(KPData1 == 5) KeyData = KEY_BACK;
			break;

	    case 6:   //UP thru ACTION
			if(KPData1 == 0) KeyData = KEY_UP;
			if(KPData1 == 1) KeyData = KEY_DN;
			if(KPData1 == 2) KeyData = KEY_LFT;
			if(KPData1 == 3) KeyData = KEY_RGT;
			if(KPData1 == 4) KeyData = KEY_ACT;
			break;

		}
	}

    else if (KPPress == 4)     // Decode only if 4  keypresses have been detected
	{
    	switch (KPData0) // now the column data
		{

	    default:   // If key *,0, or # pressed
			KeyData = KEY_9;   // hacked for now. need to develop multiple key press later

		}
	}

	return (KeyData);

}// End of KeypadGetPress()


/*
*******************************************************************************
*
* FUNCTION:
*    KeyapdHWSetup
*
* DESCRIPTION:
*    Initialize the Keyapd Hardware Controller
*
* INPUT PARAMETERS:
*    None.
*
* RETURNS:
*    None.
*
* GLOBAL EFFECTS:
*    None.
*
* ASSUMPTIONS:
*    None.
*
* CALLS:
*    None.
*
* CALLED BY:
*    Keypad test main function.
*
* PROTOTYPE:
*    void KeyapdHWSetup(void);
*
*******************************************************************************
*/
void KeypadHWSetup(void)	//modified by hzh
{
//    DM_CwDbgPrintf(DM_CW_XS_PCMCIA_0, "KeypadHWSetup:");

	// Configure the GPIO Pins

	GPDR2_REG &= ~BIT29;	// Set GP 93 as Input (Keypad Direct Input)
	GAFR2_U_REG	|= BIT26;	// Alternate Function 1
	GAFR2_U_REG &= ~BIT27;  // Clear this bit

	GPDR2_REG &= ~BIT30;	// Set GP 94 as Input (Keypad Direct Input)
	GAFR2_U_REG	|= BIT28;	// Alternate Function 1
	GAFR2_U_REG &= ~BIT29;  // Clear this bit

	GPDR2_REG &= ~BIT31;	// Set GP 95 as Input (Keypad Direct Input)
	GAFR2_U_REG	|= BIT30;	// Alternate Function 1
	GAFR2_U_REG &= ~BIT31;  // Clear this bit

	GPDR3_REG &= ~BIT1; 	// Set GP 97 as Input (Matrix Keypad Input)
	GAFR3_L_REG	|= BIT2;	// Alternate Function 1
	GAFR3_L_REG |= BIT3;   // Clear this bit

	GPDR3_REG &= ~BIT4; 	// Set GP 100 as Input (Matrix Keypad Input)
	GAFR3_L_REG	|= BIT8;	// Alternate Function 1
	GAFR3_L_REG &= ~BIT9;   // Clear this bit

	GPDR3_REG &= ~BIT5; 	// Set GP 101 as Input (Matrix Keypad Input)
	GAFR3_L_REG	|= BIT10;	// Alternate Function 1
	GAFR3_L_REG &= ~BIT11;   // Clear this bit

	GPDR3_REG &= ~BIT6; 	// Set GP 102 as Input (Matrix Keypad Input)
	GAFR3_L_REG	|= BIT12;	// Alternate Function 1
	GAFR3_L_REG &= ~BIT13;  // Clear this bit

	GPDR3_REG |= BIT7;		// Set GP 103 as Output (Matrix Keypad Output)
	GAFR3_L_REG	&= ~BIT14;	// Clear this bit
	GAFR3_L_REG |= BIT15;   // Alternate Function 2

	GPDR3_REG |= BIT8;		// Set GP 104 as Output (Matrix Keypad Output)
	GAFR3_L_REG	&= ~BIT16;	// Clear this bit
	GAFR3_L_REG |= BIT17;   // Alternate Function 2

	GPDR3_REG |= BIT9;		// Set GP 105 as Output (Matrix Keypad Output)
	GAFR3_L_REG	&= ~BIT18;	// Clear this bit
	GAFR3_L_REG |= BIT19;   // Alternate Function 2

   // Insure Keypad Clock is enabled
    xsCMEnableClock(CK_KEYPAD);


	// Set Up the Keypad Interface Control Register
	KPC_REG = (KEYP_KPC_ASACT | KEYP_KPC_MKRN | KEYP_KPC_MKCN |
	           KEYP_KPC_ME | KEYP_KPC_DKIN | KEYP_KPC_DE | 
	           KEYP_KPC_MS0 | KEYP_KPC_MS1 | KEYP_KPC_MS2);

    KPKDI_REG = 10 | (10<<8);	// Set Debounce time low because the rotary encoder also used this value!
	KPREC_REG = 0x7F; // Set scroll wheel value to a mid-point value
	           
    DM_WaitMs(10);   // Dealy 10 mS for Controller and GPIOs to set up
	{
		UINT32 val;
		val = KPC_REG;
		val = KPDK_REG;
		val = KPMK_REG;
	}

}// End of KeypadHWSetup()

/*
*******************************************************************************
*
* FUNCTION:
*    FastTapHWSetup
*
* DESCRIPTION:
*    Initialize the Keyapd Hardware Controller
*
* INPUT PARAMETERS:
*    None.
*
* RETURNS:
*    None.
*
* GLOBAL EFFECTS:
*    None.
*
* ASSUMPTIONS:
*    None.
*
* CALLS:
*    None.
*
* CALLED BY:
*    Keypad test main function.
*
* PROTOTYPE:
*    void KeyapdHWSetup(void);
*
*******************************************************************************
*/
void FastTapHWSetup(void)
{
//    DM_CwDbgPrintf(DM_CW_XS_PCMCIA_0, "KeypadHWSetup:");

	// Configure the GPIO Pins
/*
; GPIO Setup is as follows:
;
;	PIN		PIN NAME	DIRECTION	ALT FUNC	SET LEVEL
;	===   ===========   =========   ========    =========
;    93   KP_DKIN[0]       IN          1           n/a
;    94   KP_DKIN[1]       IN          1           n/a
;    95   KP_MKIN[6]       IN          3		   n/a
;    96   KP_MKOUT[6]      OUT         3           LOW
;    97   KP_MKIN[3]       IN          3           n/a
;    98   KP_MKIN[4]       IN          3           n/a
;    99   KP_MKIN[5]       IN          3           n/a							   
;    100  KP_MKIN[0]       IN          1		   n/a
;    101  KP_MKIN[1]       IN          1		   n/a
;    102  KP_MKIN[2]       IN          1		   n/a
;    103  KP_MKOUT[0]      OUT         2		   LOW
;    104  KP_MKOUT[1]      OUT         2		   LOW
;    105  KP_MKOUT[2]      OUT         2		   LOW
;    106  KP_MKOUT[3]      OUT         2		   LOW
;    107  KP_MKOUT[4]      OUT         2		   LOW
;    108  KP_MKOUT[5]      OUT         2		   LOW
*/

	GPDR2_REG &= ~BIT29;	// Set GP 93 as Input (Rotary Encoder Input)
	GAFR2_U_REG	|= BIT26;	// Alternate Function 1
	GAFR2_U_REG &= ~BIT27;  // Clear this bit

	GPDR2_REG &= ~BIT30;	// Set GP 94 as Input (Rotary Encoder Input)
	GAFR2_U_REG	|= BIT28;	// Alternate Function 1
	GAFR2_U_REG &= ~BIT29;  // Clear this bit

	GPDR2_REG &= ~BIT31;	// Set GP 95 as Input (Rotary Encoder Push Button)
	GAFR2_U_REG	|= BIT30;	// Alternate Function 3
	GAFR2_U_REG |= BIT31;   // Set this bit

	GPDR3_REG |= BIT0;		// Set GP 96 as Output 
	GAFR3_L_REG	|= BIT0;	// Set this bit
	GAFR3_L_REG |= BIT1;    // Alternate Function 3

	GPDR3_REG &= ~BIT1;		// Set GP 97 as Input 
	GAFR3_L_REG	|= BIT2;	// Alternate Function 3
	GAFR3_L_REG |= BIT3;    // Set this bit

	GPDR3_REG &= ~BIT2; 	// Set GP 98 as Input (Keypad Direct Input)
	GAFR3_L_REG	|= BIT4;	// Alternate Function 3
	GAFR3_L_REG |= BIT5;    // Set this bit

	GPDR3_REG &= ~BIT3; 	// Set GP 99 as Input (Keypad Direct Input)
	GAFR3_L_REG	|= BIT6;	// Alternate Function 3
	GAFR3_L_REG |= BIT7;    // Clear this bit

	GPDR3_REG &= ~BIT4; 	// Set GP 100 as Input (Matrix Keypad Input)
	GAFR3_L_REG	|= BIT8;	// Alternate Function 1
	GAFR3_L_REG &= ~BIT9;   // Clear this bit

	GPDR3_REG &= ~BIT5; 	// Set GP 101 as Input (Matrix Keypad Input)
	GAFR3_L_REG	|= BIT10;	// Alternate Function 1
	GAFR3_L_REG &= ~BIT11;   // Clear this bit

	GPDR3_REG &= ~BIT6; 	// Set GP 102 as Input (Matrix Keypad Input)
	GAFR3_L_REG	|= BIT12;	// Alternate Function 1
	GAFR3_L_REG &= ~BIT13;  // Clear this bit

	GPDR3_REG |= BIT7;		// Set GP 103 as Output (Matrix Keypad Output)
	GAFR3_L_REG	&= ~BIT14;	// Clear this bit
	GAFR3_L_REG |= BIT15;   // Alternate Function 2

	GPDR3_REG |= BIT8;		// Set GP 104 as Output (Matrix Keypad Output)
	GAFR3_L_REG	&= ~BIT16;	// Clear this bit
	GAFR3_L_REG |= BIT17;   // Alternate Function 2

	GPDR3_REG |= BIT9;		// Set GP 105 as Output (Matrix Keypad Output)
	GAFR3_L_REG	&= ~BIT18;	// Clear this bit
	GAFR3_L_REG |= BIT19;   // Alternate Function 2

	GPDR3_REG |= BIT10;		// Set GP 106 as Output (Matrix Keypad Output)
	GAFR3_L_REG	&= ~BIT20;	// Clear this bit
	GAFR3_L_REG |= BIT21;   // Alternate Function 2

	GPDR3_REG |= BIT11;		// Set GP 107 as Output (Matrix Keypad Output)
	GAFR3_L_REG	&= ~BIT22;	// Clear this bit
	GAFR3_L_REG |= BIT23;   // Alternate Function 2

	GPDR3_REG |= BIT12;		// Set GP 108 as Output (Matrix Keypad Output)
	GAFR3_L_REG	&= ~BIT24;	// Clear this bit
	GAFR3_L_REG |= BIT25;   // Alternate Function 2
	
   // Insure Keypad Clock is enabled
    xsCMEnableClock(CK_KEYPAD);


	// Set Up the Keypad Interface Control Register
	KPC_REG = (KEYP_KPC_ASACT | KEYP_KPC_MKRN | KEYP_KPC_MKCN |	KEYP_KPC_MIE |
	           KEYP_KPC_ME | KEYP_KPC_DKIN | KEYP_KPC_DEE0 | KEYP_KPC_DE | 
	           KEYP_KPC_MS0 | KEYP_KPC_MS1 | KEYP_KPC_MS2 | KEYP_KPC_MS3 |
	           KEYP_KPC_MS4 | KEYP_KPC_MS5 | KEYP_KPC_MS6 | KEYP_KPC_MS7);

    KPKDI_REG = 1;    // Set Debounce time low because the rotary encoder also used this value!
	KPREC_REG = 0x7F; // Set scroll wheel value to a mid-point value
	           
    DM_WaitMs(10);   // Dealy 10 mS for Controller and GPIOs to set up


}// End of FastTapHWSetup()


void KPostKeypadTest(PVOID arg, PCHAR param)
{
    PostKeypadTest();
}

void KPostFastTapTest(PVOID arg, PCHAR param)
{
  //  PostFastTapTest();
}

⌨️ 快捷键说明

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