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

📄 key.c

📁 键盘扫描
💻 C
字号:
#include <string.h>
#include "2410addr.h"
#include "2410lib.h"
#include "key.h"

void __irq KeyPressed(void);
void Int_init(void);
unsigned char set_start;
void keytest(void);
unsigned int value;
volatile unsigned int keyend;

void keytest(void)
{
	keyend=0;
	//Int_init();
	rGPGCON=rGPFCON & 0xbffff;   //
	rGPGCON|=(2<<18);			//GPG9:INT17
	rGPGUP|=(0x01<<9);          //GPG9 pull-up DISABLE
	
	pISR_EINT8_23 = (unsigned)KeyPressed;
	
	rEXTINT2 &= ~(0x1<<4);
	rEXTINT2 &= ~(0x1<<5);
	rEXTINT2 &= ~(0x1<<6);
	
	rEINTMASK &= ~(0x1<<17);
	rSRCPND &= ~(BIT_EINT8_23);
	rINTMOD &= ~(BIT_EINT8_23);
	rINTPND &= ~(BIT_EINT8_23);
	rINTMSK &= ~(BIT_EINT8_23);
	//rEINTMASK =~(BIT_EINT4_7) ;
	
	//Uart_Printf("a key is pressed\r\n");
	while(!keyend);
	Uart_Printf("\nEnd keyboard test!!! \n");	
}

char KeyValueGet(void)
{	
	//int KPPress;
	unsigned char key,temp,temp_key;
	// Character from Keypad
	//unsigned int KPData0;     // Register data
	//unsigned int KPData1;     // Register data
	//unsigned int KPPress;     // Keypad press count
	
	//rGPEDAT &= (0x0D<<3);
	//rGPEUP = 0x0;
	
	temp_key = ((rGPEDAT>>3)&0xff);
	
	temp = (temp_key & 0xff);
		Delay(500);
		if (temp == 0xff)
		return 0;
		else
		key =temp;
		return key;
	/*
	switch(temp_key)
	{
	case (temp_key == 0x0E):
		{
		temp = (temp_key & 0xff);
		Delay(1000);
		if (temp == 0xff)
		return 0;
		else
		key =temp;
		return key;
		break;
		}
	case (temp_key == 0x0D):
		{
		temp = (temp_key & 0xff);
		Delay(1000);
		if (temp == 0xff)
		return 0;
		else
		key =temp;
		return key;
		break;
		}
	case (temp_key == 0x0B):
		{
		temp = (temp_key & 0xff);
		Delay(1000);
		if (temp == 0xff)
		return 0;
		else
		key =temp;
		return key;
		break;
		}
	case (temp_key == 0x07):
		{
		temp = (temp_key & 0xff);
		Delay(1000);
		if (temp == 0xff)
		return 0;
		else
		key =temp;
		return key;
		break;
		}
		
	default:
		break;
	}
	*/
	
	
}

void Get_Key(void)
{
	unsigned char i=0,keyvalue,save_E,save_UE;
	//KeyPressed();
	save_E = rGPECON;
	save_UE = rGPEUP;
	rGPECON |= (0x55<<6);//gpa3~6设置为output
	rGPECON &= (0x55<<6);
	rGPECON &= ~(0xff<<14);//gpe7~10设置为input 电阻上拉 没有键按下时为高
	rGPEUP=rGPEUP | 0x7f8;
	//rGPEDAT = (0x07<<3);
	//rEINTMASK = (unsigned)KeyPressed;
	
	
	
	rGPEDAT = (0x07<<3);
	i=KeyValueGet();
	switch(i)
	{
		case 0xE7: Uart_Printf("4\r\n");keyvalue=0x66;SMG_test(keyvalue);break;
		case 0xD7: Uart_Printf("8\r\n");keyvalue=0x7f;SMG_test(keyvalue);break;
		case 0xB7: Uart_Printf("C\r\n");keyvalue=0x39;SMG_test(keyvalue);break;
		case 0x77: Uart_Printf("0\r\n");keyvalue=0x3f;SMG_test(keyvalue);break;
		//case 0x00: Uart_Printf("0\r\n");keyvalue=0x3f;SMG_test(keyvalue);break;
		default : break;
	}
	
	
	rGPEDAT = (0x0b<<3);
	i=KeyValueGet();
	switch(i)
	{
		case 0xEB: Uart_Printf("3\r\n");keyvalue=0x4f;SMG_test(keyvalue);break;
		case 0xDB: Uart_Printf("7\r\n");keyvalue=0x07;SMG_test(keyvalue);break;
		case 0xBB: Uart_Printf("B\r\n");keyvalue=0x7c;SMG_test(keyvalue);break;
		case 0x7B: Uart_Printf("F\r\n");keyvalue=0x71;SMG_test(keyvalue);break;
		//case 0x00: Uart_Printf("0\r\n");keyvalue=0x3f;SMG_test(keyvalue);break;
		default : break;
	}
	
	
	rGPEDAT = (0x0d<<3);
	i=KeyValueGet();
	switch(i)
	{
		case 0xED: Uart_Printf("2\r\n");keyvalue=0x5b;SMG_test(keyvalue);break;
		case 0xDD: Uart_Printf("6\r\n");keyvalue=0x7d;SMG_test(keyvalue);break;
		case 0xBD: Uart_Printf("A\r\n");keyvalue=0x77;SMG_test(keyvalue);break;
		case 0x7D: Uart_Printf("E\r\n");keyvalue=0x79;SMG_test(keyvalue);break;
		//case 0x00: Uart_Printf("0\r\n");keyvalue=0x3f;SMG_test(keyvalue);break;
		default : break;
	}
	
	
	rGPEDAT = (0x0e<<3);
	i=KeyValueGet();
	switch(i)
	{
		//case 0x00: Uart_Printf("0\r\n");keyvalue=0x3f;SMG_test(keyvalue);break;
		case 0xEE: Uart_Printf("1\r\n");keyvalue=0x06;SMG_test(keyvalue);break;
		case 0xDE: Uart_Printf("5\r\n");keyvalue=0x6d;SMG_test(keyvalue);break;
		case 0xBE: Uart_Printf("9\r\n");keyvalue=0x6f;SMG_test(keyvalue);break;
		case 0x7E: Uart_Printf("D\r\n");keyvalue=0x5e;SMG_test(keyvalue);break;
		default : break;
	}
	
	rGPECON = save_E ;
	rGPEUP = save_UE;
}





void __irq KeyPressed(void)
{
	
	
	//rEINTMASK |= (1<<7);
	do{
		Get_Key();
	  }while(value!='0');
	  
	value='?';
	
	rEINTMASK |= (0x1<<17);
	//ClearPending(BIT_EINT4_7);
	rSRCPND |= (BIT_EINT8_23);
	//rINTPND |= (BIT_EINT8_23);
	rINTMOD |= (BIT_EINT8_23);
	//rINTMSK |= (BIT_EINT8_23);
	rINTPND |= (BIT_EINT8_23);
	rINTMSK |= (BIT_EINT8_23);
	keyend=1;
	//Uart_Printf("a key is pressed\r\n");
}

void Int_init(void)
{
	rGPFCON &= ~(0x1) | (10<<14);
	
	//pISR_EINT4_7 = (U32)Eint4_7Int;
	rEINTMASK &=~(0x1<<7);
	rSRCPND = BIT_EINT4_7;
	rINTPND = BIT_EINT4_7;
	rINTMSK &= ~(BIT_EINT4_7);
	
	rEINTPEND = (1<<7);
	rEINTMASK &= ~(1<<7);
}

void SMG_test(unsigned char value)
{
	//unsigned int i;
	//int j;
	#define  SMG    (*(volatile unsigned char *)0x20000000)
	//long data_temp,i,j,data[4];
	//int data_addr;
	//*0x20000000 = data_temp;
	rGPACON &=~(0xf<<5);//gpa5~gpa8
	rGPACON &=~(0x1<<15);
	//rGPACON &=~(0xff);
	
	//rGPADAT &=~(0xf<<5);
	rGPADAT |=(0x1<<15);//gpa15
	//j=rGPACON;
	//Delay(200);
	SMG=value;
		
	rGPADAT &=~(0x1<<15);
	//long data_addr[4] ={0x20000000,0x20000004,0x20000008,0x2000000a};
	//data_temp = 1234;
	//data_addr = 0x20000000;
	//*((unsigned int*)data_addr)=data_temp;
	//data[0] = data_temp % 10;
	//data[1] = data_temp % 100 / 10;
	//data[2] = data_temp % 1000 / 100;
	//data[3] = data_temp / 1000;
	
	
	//for(i=0;i<=0x3088;i++)
	//{
		//data_temp[j] = data[i];
		
		//((volatile*)(data_addr[j])) = (data[i]);
		
		
		
		//Delay(20000);
		
		//rGPADAT |= (0x1<<15);
		
		//SMG=0x3f;
		/*
		rGPADAT &=~(0x1<<15);
		Delay(10000);
		rGPADAT |= (0x1<<15);
		for(i=0;i<=0xff;i++)
		{
			rGPADAT |= (0x1<<15);//打开锁存芯

⌨️ 快捷键说明

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