📄 ex14.c
字号:
/****************************************************************************/
//
//
// Created by Starlight Embeded Studio
// www.cedn.cn
// If there are any concerns on the code
// go to www.cedn.cn for discussion
//
/****************************************************************************/
#include <stdio.h>
#include "44B.H"
//GPG5
#define SET_PS_CLK_OUT (rPCONG |= 0x0400)
#define SET_PS_CLK(x) ((x)?(rPDATG|=0x0020):(rPDATG&=0xFFDF))
#define SET_PS_CLK_IN (rPCONG &= 0xF3FF)
#define GET_PS_CLK (rPDATG & 0x0020 ? 1:0)
//GPE7
#define SET_PS_DAT_OUT (rPCONE |= 0x4000)
#define SET_PS_DAT(x) ((x)?(rPDATE|=0x0080):(rPDATE&=0xFF7F))
#define SET_PS_DAT_IN (rPCONE &= 0xF3FFF)
#define GET_PS_DAT (rPDATE & 0x0080 ? 1:0)
static unsigned char abc[46]={
0x45,0x16,0x1E,0x26,0x25,0x2E,0x36,0x3D,0x3E,0x46,
0x70,0x69,0x72,0x7A,0x6B,0x73,0x74,0x6C,0x75,0x7D,
0x1C,0x32,0x21,0x23,0x24,0x2B,0x34,0x33,0x43,0x3B,0x42,0x4B,0x3A,
0x31,0x44,0x4D,0x15,0x2D,0x1B,0x2C,0x3C,0x2A,0x1D,0x22,0x35,0x1A
};
void Delay(int time)
{
static int DelayLoopCount = 500;
int i,j=0;
for(j=0;j<time;j++)
for(i=0;i<DelayLoopCount;i++);
}
void PSIoInit()
{
rPUPE |= 0x0080;
rPDATE &= 0x17F;
rPCONE &= 0xF3FFF;
rPUPG |= 0x0010;
rPDATG |= 0x0010;
rPCONG &= 0xF3FF;
}
int PS2GetBit()
{
unsigned int timeout;
timeout =0;
while (GET_PS_CLK) {
timeout++;
if (timeout>0x1000) return -1;
}
while (!GET_PS_CLK);
return GET_PS_DAT;
}
unsigned char GetKBData()
{
unsigned char data;
int i;
SET_PS_DAT_IN;
SET_PS_CLK_IN;
data = 0;
if (PS2GetBit()<0) return 0xFF; //Start bit
for (i=0;i<8;i++)
{
data>>=1;
if (PS2GetBit()) {
data |= 0x80;
}
}
PS2GetBit(); //Partify
PS2GetBit(); //Stop
return data;
}
int SendKBCmd(unsigned char cmd)
{
int t,i;
unsigned int timeout;
t=0;
SET_PS_CLK_OUT;
SET_PS_CLK(0);
Delay(1000);
SET_PS_DAT_OUT;
SET_PS_DAT(0);
SET_PS_CLK_IN;
while (!GET_PS_CLK);
for (i=0;i<9;i++)
{
if (i==8) SET_PS_DAT(t&1);
else {
SET_PS_DAT(cmd&1);
if (cmd&1) t++;
}
timeout = 0;
while (GET_PS_CLK) {
if (timeout++>0x1000) return -1;
}
while (!GET_PS_CLK);
cmd>>=1;
}
SET_PS_DAT_IN;
while (!GET_PS_DAT);
while (!GET_PS_CLK);
while (GET_PS_CLK);
while (GET_PS_DAT);
return 0;
}
int GetKBKey(unsigned char *buf)
{
int i,c;
c = 0;
for (i=0;i<5;i++) {
buf[i] = GetKBData();
if (buf[i]==0xFF) break;
c++;
}
return c;
}
char ConvertScan(unsigned char *buf)
{
int i;
if (buf[0]==0xE0 || buf[0]==0xF0) return 0x80;
if (buf[0]==0x5A) return 0x0D;
if (buf[0]==0x29) return ' ';
if (buf[0]==0x76) return 0xFF;
for (i=0;i<sizeof(abc);i++) {
if (abc[i]==buf[0]) {
if (i<10) return '0'+i;
else if (i<20) return '0'+i-10;
else return 'A'+i-20;
}
}
return 0x80;
}
void Test14()
{
int c;
char ch;
unsigned char buf[5];
printf("\nStart PS2 Test!\n");
printf("\nOnly (0-9) ('A'-'Z') are available now.\n");
PSIoInit();
if (SendKBCmd(0xaa)<0) {
printf("\nNo PS2 KeyBoard is detected!\n");
printf("\nFinish PS2 Test!\n");
return;
}
Delay(1000);
while(1) {
c=GetKBKey(buf);
if (c>0) {
ch = ConvertScan(buf);
if (ch==0xFF) break;
else if (ch!=0x80) {
if (ch==0x0D) {
printf("\n");
} else
printf("%c",ch);
}
}
}
printf("\nFinish PS2 Test!\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -