/**************************
文件所用資源
1.外部中斷0
2.端口:P3.3、P3.4
**************************/
sbit PS2_DAT=P3^7;//數(shù)據(jù)
uchar code unshifted[][2]=//shift鍵沒按下譯碼表
{
0x0e,'`',
0x15,'q',
0x16,'1',
0x1a,'z',
0x1b,'s',
0x1c,'a',
0x1d,'w',
0x1e,'2',
0x21,'c',
0x22,'x',
0x23,'d',
0x24,'e',
0x25,'4',
0x26,'3',
0x29,' ',
0x2a,'v',
0x2b,'f',
0x2c,'t',
0x2d,'r',
0x2e,'5',
0x31,'n',
0x32,'b',
0x33,'h',
0x34,'g',
0x35,'y',
0x36,'6',
0x39,',',
0x3a,'m',
0x3b,'j',
0x3c,'u',
0x3d,'7',
0x3e,'8',
0x41,',',
0x42,'k',
0x43,'i',
0x44,'o',
0x45,'0',
0x46,'9',
0x49,'.',
0x4a,'/',
0x4b,'l',
0x4c,';',
0x4d,'p',
0x4e,'-',
0x52,''',
0x54,'[',
0x55,'=',
0x5b,']',
0x5d,'\',
0x61,'<',
0x69,'1',
0x6b,'4',
0x6c,'7',
0x70,'0',
0x71,'.',
0x72,'2',
0x73,'5',
0x74,'6',
0x75,'8',
0x79,'+',
0x7a,'3',
0x7b,'-',
0x7c,'*',
0x7d,'9',
0,0
};
uchar code shifted[][2]= //shift鍵按下譯碼表
{
0x0e,'~',
0x15,'Q',
0x16,'!',
0x1a,'Z',
0x1b,'S',
0x1c,'A',
0x1d,'W',
0x1e,'@',
0x21,'C',
0x22,'X',
0x23,'D',
0x24,'E',
0x25,'$',
0x26,'#',
0x29,' ',
0x2a,'V',
0x2b,'F',
0x2c,'T',
0x2d,'R',
0x2e,'%',
0x31,'N',
0x32,'B',
0x33,'H',
0x34,'G',
0x35,'Y',
0x36,'^',
0x39,'L',
0x3a,'M',
0x3b,'J',
0x3c,'U',
0x3d,'&',
0x3e,'*',
0x41,'<',
0x42,'K',
0x43,'I',
0x44,'O',
0x45,')',
0x46,'(',
0x49,'>',
0x4a,'?',
0x4b,'L',
0x4c,':',
0x4d,'P',
0x4e,'_',
0x52,'"',
0x54,'{',
0x55,'+',
0x5b,'}',
0x5d,'|',
0x61,'>',
0x69,'1',
0x6b,'4',
0x6c,'7',
0x70,'0',
0x71,'.',
0x72,'2',
0x73,'5',
0x74,'6',
0x75,'8',
0x79,'+',
0x7a,'3',
0x7b,'-',
0x7c,'*',
0x7d,'9',
0,0
};
void Decode(uchar scancode);//聲明函數(shù)原型
uchar bitcount,bytecount,byte2;//edge為0下降沿中斷,為1上升沿中斷
uchar ascii2[35],ascii[99]; //bitcount為位計數(shù)值
//ascii為翻譯后的ASCII碼,初值為空格
//**************************************************
//外部中斷服務(wù)子函數(shù)
//***************************************************
void StopInt() interrupt 0
{
static uchar udata; // 聲明局部靜態(tài)變量來保存掃描碼
if(bitcount < 11 && bitcount > 2) //3到10位是數(shù)據(jù),起始位,校驗位和停止位忽略
{
udata = (udata >> 1); //右移保存數(shù)據(jù)
if(PS2_DAT)
{
udata|=0x80; //存儲一個'1'
}
}
if(--bitcount==0) //如果11位全部接收完畢
{
bitcount = 11; //重新設(shè)為11位數(shù)據(jù)
ascii[bytecount]=udata;
if(bytecount<6)bytecount++;
}
}
/*******************************************
函數(shù)名稱: Decode
功 能:
參 數(shù): scancode--需要翻譯的掃描碼
返回值 : 無
/********************************************/
void Decode(uchar scancode)
{
static uchar up=0,shift=0;//up為通、斷碼標(biāo)志,shift為shift鍵按下標(biāo)志
uchar i;
if (!up) //已接收的11位數(shù)據(jù)是通碼(up為0)
{
switch (scancode)//開始翻譯掃描碼
{
case 0xF0: //鍵盤釋放標(biāo)志(隨后的一個字節(jié)是斷碼)
up=1; //設(shè)置up為斷碼標(biāo)志
break;
case 0x12: //左shift鍵按下
shift=1; //設(shè)置shift為按下標(biāo)志
break;
case 0x59: //右shift鍵按下
shift=1; //設(shè)置shift為按下標(biāo)志
break;
default:
if(!shift) //如果shift鍵沒有按下
{ //查找unshifted表,表中左列是掃描碼,右列是對應(yīng)的ASCII碼
for(i=0;unshifted[i][0]!=scancode&&unshifted[i][0];i++);
if(unshifted[i][0]==scancode)
{
ascii2[3]=unshifted[i][1];
}
}
else //如果shift鍵按下
{ //查找shifted表
for(i=0;shifted[i][0]!=scancode&&shifted[i][0];i++);
if(shifted[i][0]==scancode)
{
ascii2[3]=shifted[i][1];
}
}
break;
}
}
else //已接收的11位數(shù)據(jù)是斷碼(up為1)
{
up = 0; //將斷碼標(biāo)志復(fù)位
switch (scancode)//檢測shift鍵釋放
{
case 0x12 : //左shift鍵
shift = 0;
break;
case 0x59 ://右shift鍵
shift = 0;
break;
default:
break;
}
}
}