微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 51单片机模拟PS2协议制作5X5矩阵工业键盘

51单片机模拟PS2协议制作5X5矩阵工业键盘

时间:11-23 来源:互联网 点击:

CT_KB=OFF;
EX0=0;

H_DATA=0;Delay10us();//start bit
H_CLK=1;
Delay10us();
H_CLK=0;
Delay30us();

for(i=0;i<8;i++)
{
if(dat&0x01==0x01){H_DATA=1;Delay10us();}
else {H_DATA=0;Delay10us();}
dat>>=1;
H_CLK=1;
Delay10us();
H_CLK=0;
Delay30us();
}

H_DATA=ParityBit;Delay10us();//parity bit
H_CLK=1;
Delay10us();
H_CLK=0;
Delay30us();

H_DATA=1;Delay10us();//stop bit
H_CLK=1;
Delay10us();
H_CLK=0;
Delay30us();

IE0=0;
EX0=1;
H_CLK=1;
H_DATA=1;
CT_KB=ON;
Delay30us();
}
//---------------------------------------------------------------------------
void ExternInterrupt0(void) interrupt 0
{
BOOL ParityBit=0,CLKFlag=1;
BYTE i=0,j=8,dat=0x00;

EX0=0;

while(CLKFlag)
{
i++;
if(H_CLK==1)CLKFlag=0;
if(i>0xEE)
{
IE0=0;
EX0=1;
return ;
}
}
while(j--)//延时等待大键盘的动作
{
for(i=0;i<0x88;i++)//检查是否有数据处理0x88
{
if(H_CLK==0)//有动作则是大键盘存在
{
OnKeyBoardOnline(TRUE);//大键盘存在,开机由大键盘应答
IE0=0;
EX0=1;
return ;//存在的话置标志位,并返回
}
}
}
OnKeyBoardOnline(FALSE);//大键盘不存在,由小键盘应答

//转到接收
for(i=0;i<8;i++)//read 8bit
{
Delay30us();
H_CLK=0;
Delay30us();
H_CLK=1;
dat=dat>>1;
if(H_DATA)dat|=0x80;

if(H_CLK==0){return;}//如果时钟被拉低,则有错误发生
}

Delay30us();
H_CLK=0;
Delay30us();
H_CLK=1;
if(H_DATA)ParityBit=1;
else ParityBit=0;

Delay30us();
H_CLK=0;
Delay30us();
H_CLK=1;//STOP BIT H_DATA 0 ERR

Delay10us();
H_DATA=0;//ACK bit
Delay10us();
H_CLK=0;
Delay30us();Delay30us();
H_CLK=1;
Delay30us();
H_DATA=1;

OnPS2ReceiveChar(dat,ParityBit);

IE0=0;
EX0=1;
return ;
}
//---------------------------------------------------------------------------
void ProcessPS2(void)
{
if(KeyBoardFlag==FALSE)//大键盘不存在
{
if(PS2RecChar==0xF3)//1
{
OnPS2SendChar(0xFA);
PS2RecChar=0xCC;
}
else if(PS2RecChar==0x00)//11
{
OnPS2SendChar(0xFA);
PS2RecChar=0xCC;
}
else if(PS2RecChar==0x02)//111
{
OnPS2SendChar(0xFA);
PS2RecChar=0xCC;
}
else if(PS2RecChar==0x20)//1111
{
OnPS2SendChar(0xFA);
PS2RecChar=0xCC;
}
else if(PS2RecChar==0xED)//2
{
OnPS2SendChar(0xFA);
PS2RecChar=0xCC;
}
else if(PS2RecChar==0xF0)//3
{
OnPS2SendChar(0xFA);
PS2RecChar=0xCC;
}
else if(PS2RecChar==0xF2)//4
{
OnPS2SendChar(0xFA);
Delay30us();
OnPS2SendChar(0xAB);
Delay30us();
OnPS2SendChar(0x83);
PS2RecChar=0xCC;
}
else if(PS2RecChar==0xEF)//5
{
OnPS2SendChar(0xFA);
Delay30us();
OnPS2SendChar(0xBF);
Delay30us();
OnPS2SendChar(0xB0);
PS2RecChar=0xCC;
}
else if(PS2RecChar==0xF3)//6
{
OnPS2SendChar(0xFA);
PS2RecChar=0xCC;
}
else if(PS2RecChar==0xFE)//7 //resend
{
PS2RecChar=0xCC;
}
else if(PS2RecChar==0xEE)//8
{
OnPS2SendChar(0xEE);
PS2RecChar=0xCC;
}
else if(PS2RecChar==0xEE)//9
{
OnPS2SendChar(0xEE);
PS2RecChar=0xCC;
}
else if(PS2RecChar==0xF1)//10
{
OnPS2SendChar(0xFA);
PS2RecChar=0xCC;
}
else if(PS2RecChar==0xF4)//11
{
OnPS2SendChar(0xFA);
PS2RecChar=0xCC;
}
else if(PS2RecChar==0xAA)//12
{
OnPS2SendChar(0xAA);
PS2RecChar=0xCC;
}
else if(PS2RecChar==0xFF)//13
{
OnPS2SendChar(0xFA);
Delay30us();
OnPS2SendChar(0xAA);
Delay30us();
PS2RecChar=0xCC;
}
else ;
}
}
//---------------------------------------------------------------------------
void PS2Init(void)
{
IT0=0;//低电平触发中断
PX0=1;
EX0=1;
}
//---------------------------------------------------------------------------

extern void ProcessPS2(void);
extern void PS2Init(void);
void main(void)
{
unsigned char tem;
PS2Init();
ProcessPS2();
while(1)
{
tem=Key_Scan();
switch(tem&0xff)
{
case 23 :
break;
default:
{
OnPS2SendChar(PS2Value[tem]);
}
break;
}
//开机应答,使电脑能识别到键盘
//other code
}
}
仿真矩阵键盘电路图,该电路未连接PS2,但可通过LED灯观察每个按键按下之后的键值返回知否和第二套键盘码对应一致


第二套键值码对应表:


Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top