📄 2410keyboard.c
字号:
//2410keyboard.c
#include "Def.h"
#include "2410addr.h"
#include "2410lib.h"
#include "option.h"
#include "2410keyboard.h"
#define KEYA1 0x11
#define KEYA2 0x12
#define KEYA3 0x13
#define KEYA4 0x14
#define KEYB1 0x21
#define KEYB2 0x22
#define KEYB3 0x23
#define KEYB4 0x24
#define KEYC1 0x31
#define KEYC2 0x32
#define KEYC3 0x33
#define KEYC4 0x34
#define KEYD1 0x41
#define KEYD2 0x42
#define KEYD3 0x43
#define KEYD4 0x44
int Read_Row1(void);
int Read_Row2(void);
int Read_Row3(void);
int Read_Row4(void);
int Write_ColumnA(int val);
int Write_ColumnB(int val);
int Write_ColumnC(int val);
int Write_ColumnD(int val);
int Scan_Column(char column);
void Init_Keyboard(void);
/*initialize the key reg*/
void Init_Keyboard(void)
{
//GPG2 0x01, GPG3 0x01, GPG5 0x01,GPG6 0x01, GPG7 0x01
//rGPGCON=((rGPGCON & ~(0xf<<4)) & ~(0x3f<<10))| ((0x5<<4))|(0x15<<10));
rGPGCON=((rGPGCON & ~((0xf<<4) |(0x3f<<10))))| ((0x1<<4));
//GPE1 0x01,GPE12 0x01,GPE13 0x01
rGPECON=(rGPECON & ~(0x3f<<22)) | (0x15<<22);
}/*end of init*/
/*read the 1 row,nSS1*/
int Read_Row1(void)
{
U16 reg;
reg=rGPGDAT;
reg=reg | ~(0x1<<3);
if(reg==0xffff)
return 1;
else
return 0;
}/*end read*/
/*read the 2 row,SPICLK1*/
int Read_Row2(void)
{
U16 reg;
reg=rGPGDAT;
reg=reg | ~(0x1<<7);
if(reg==0xffff)
return 1;
else
return 0;
}/*end read*/
/*read the 3 row,SPIMOSI1*/
int Read_Row3(void)
{
U16 reg;
reg=rGPGDAT;
reg=reg | ~(0x1<<6);
if(reg==0xffff)
return 1;
else
return 0;
}/*end read*/
/*read the 4 row,SPIMOSO1*/
int Read_Row4(void)
{
U16 reg;
reg=rGPGDAT;
reg=reg | ~(0x1<<5);
if(reg==0xffff)
return 1;
else
return 0;
}/*end read*/
/*write value to colunm A, SPIMOSO0*/
int Write_ColumnA(int val)
{
if(val==0)
{
rGPEDAT=rGPEDAT & ~(0x1<<11);
return 1;
}
else if(val==1)
{
rGPEDAT=rGPEDAT | (0x1<<11);
}
else
return 0;
}/*end write*/
/*write value to colunm B, SPIMOSI0*/
int Write_ColumnB(int val)
{
if(val==0)
{
rGPEDAT=rGPEDAT & ~(0x1<<12);
return 1;
}
else if(val==1)
{
rGPEDAT=rGPEDAT | (0x1<<12);
}
else
return 0;
}/*end write*/
/*write value to colunm C, SPICLK0*/
int Write_ColumnC(int val)
{
if(val==0)
{
rGPEDAT=rGPEDAT & ~(0x1<<13);
return 1;
}
else if(val==1)
{
rGPEDAT=rGPEDAT | (0x1<<13);
}
else
return 0;
}/*end write*/
/*write value to colunm d, nSS0*/
int Write_ColumnD(int val)
{
if(val==0)
{
rGPGDAT=rGPGDAT & ~(0x1<<2);
return 1;
}
else if(val==1)
{
rGPGDAT=rGPGDAT | (0x1<<2);
}
else
return 0;
}/*end write*/
/*scan a column */
int Scan_Column(char column)
{
int row1,row2,row3,row4;
switch(column)
{
case 'A':
//scan columnA
Write_ColumnA(0);
Write_ColumnB(1);
Write_ColumnC(1);
Write_ColumnD(1);
row1=Read_Row1();
row2=Read_Row2();
row3=Read_Row3();
row4=Read_Row4();
if(!(row1 && row2 && row3 && row4))
{
if(row1==0)
return KEYA1;
else if(row2==0)
return KEYA2;
else if(row3==0)
return KEYA3;
else if(row4==0)
return KEYA4;
}
else
return 0;
case 'B':
//scan columnB
Write_ColumnA(1);
Write_ColumnB(0);
Write_ColumnC(1);
Write_ColumnD(1);
row1=Read_Row1();
row2=Read_Row2();
row3=Read_Row3();
row4=Read_Row4();
if(!(row1 && row2 && row3 && row4))
{
if(row1==0)
return KEYB1;
else if(row2==0)
return KEYB2;
else if(row3==0)
return KEYB3;
else if(row4==0)
return KEYB4;
}
else
return 0;
case 'C':
//scan columnC
Write_ColumnA(1);
Write_ColumnB(1);
Write_ColumnC(0);
Write_ColumnD(1);
row1=Read_Row1();
row2=Read_Row2();
row3=Read_Row3();
row4=Read_Row4();
if(!(row1 && row2 && row3 && row4))
{
if(row1==0)
return KEYC1;
else if(row2==0)
return KEYC2;
else if(row3==0)
return KEYC3;
else if(row4==0)
return KEYC4;
}
else
return 0;
case 'D':
//scan cloumnD
Write_ColumnA(1);
Write_ColumnB(1);
Write_ColumnC(1);
Write_ColumnD(0);
row1=Read_Row1();
row2=Read_Row2();
row3=Read_Row3();
row4=Read_Row4();
if(!(row1 && row2 && row3 && row4))
{
if(row1==0)
return KEYD1;
else if(row2==0)
return KEYD2;
else if(row3==0)
return KEYD3;
else if(row4==0)
return KEYD4;
}
else
return 0;
default:
return 0;
}
}/*end of column scaning*/
/*scan key board*/
int Scan_Keyboard(void)
{
int row1,row2,row3,row4;
int i,j;
char column;
int keyNum;
//write 0 to columnA,B,C,D
Write_ColumnA(0);
Write_ColumnB(0);
Write_ColumnC(0);
Write_ColumnD(0);
//read row
row1=Read_Row1();
row2=Read_Row2();
row3=Read_Row3();
row4=Read_Row4();
if(row1==1 && row2==1 && row3==1 && row4==1)
return 0;//no key
for(i=0;i<10000;i++);//delay some time
//scan column a
column='A';
keyNum=Scan_Column(column);
if(keyNum!=0)
{
for(j=0;j++;j<100)
{
for(i=0;i<10000;i++);
if((Scan_Column(column))!=keyNum)
return 0;
}
return keyNum;
}
//scan column b
column='B';
keyNum=Scan_Column(column);
if(keyNum!=0)
{
for(j=0;j++;j<100)
{
for(i=0;i<10000;i++);
if((Scan_Column(column))!=keyNum)
return 0;
}
return keyNum;
}
//scan column c
column='C';
keyNum=Scan_Column(column);
if(keyNum!=0)
{
for(j=0;j++;j<100)
{
for(i=0;i<10000;i++);
if((Scan_Column(column))!=keyNum)
return 0;
}
return keyNum;
}
//scan column a
column='D';
keyNum=Scan_Column(column);
if(keyNum!=0)
{
for(j=0;j++;j<100)
{
for(i=0;i<10000;i++);
if((Scan_Column(column))!=keyNum)
return 0;
}
return keyNum;
}
return 0;
}/*end of scan*/
void Test_Keyboard(void)
{
char c;
int ikey;
int i;
Uart_Printf("Test Keyboad!\n\n");
Uart_Printf("Type any key to quit!\n\n");
rINTMSK = BIT_ALLMSK;
Init_Keyboard();
i=0;
while(1)
{
c=0;
//for(i=0;i<100;i++)
Delay(200);
if(i>0x100)
{
i=0;
Uart_Printf("To quit,please type 'x'!\n");
}
c=Uart_GetKey();
i++;
if(c!=0)
break;
ikey=0;
ikey=Scan_Keyboard();
if(ikey!=0)
{
if(ikey==KEYA1)
Uart_Printf("key A1 is pressed!!\n");
if(ikey==KEYA2)
Uart_Printf("key A2 is pressed!!\n");
if(ikey==KEYA3)
Uart_Printf("key A3 is pressed!!\n");
if(ikey==KEYA4)
Uart_Printf("key A4 is pressed!!\n");
if(ikey==KEYB1)
Uart_Printf("key B1 is pressed!!\n");
if(ikey==KEYB2)
Uart_Printf("key B2 is pressed!!\n");
if(ikey==KEYB3)
Uart_Printf("key B3 is pressed!!\n");
if(ikey==KEYB4)
Uart_Printf("key B4 is pressed!!\n");
if(ikey==KEYC1)
Uart_Printf("key C1 is pressed!!\n");
if(ikey==KEYC2)
Uart_Printf("key C2 is pressed!!\n");
if(ikey==KEYC3)
Uart_Printf("key C3 is pressed!!\n");
if(ikey==KEYC4)
Uart_Printf("key C4 is pressed!!\n");
if(ikey==KEYD1)
Uart_Printf("key D1 is pressed!!\n");
if(ikey==KEYD2)
Uart_Printf("key D2 is pressed!!\n");
if(ikey==KEYD3)
Uart_Printf("key D3 is pressed!!\n");
if(ikey==KEYD4)
Uart_Printf("key D4 is pressed!!\n");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -