这是我的接收函数
感觉在这里出了问题
unsigned char ps2_rec( )//返回接收到的字节,接收错误则返回0
{
unsigned char i,STOPBIT;
unsigned char PARITY,temp ;
PS2_CLK = 1;
PS2_DAT = 1;
Delay100us();
PS2_DAT = 0;
PS2_CLK = 1;
if (PS2_DAT)//PS2_DAT应该为低 如果此时PS2_DAT为高,则退出并发送错误状态字0xfe
{
ps2_send(0xfe);
return 0;
}
else
{
Delay40us();
for(i=0;i<8;i++)
{
Delay20us();
PS2_CLK = 0;
Delay40us();
PS2_CLK = 1;
Delay20us();
temp = temp>>1;
if(PS2_DAT)
{
temp |=0x80;
}
}
// SendByte(0X30+temp);
// delay_ms(3000);
if(!PS2_CLK) //每读完一个位都检测时钟线是否被拉低
{
ps2_send(0xfe);
return 0;
}
//======================================================
Delay20us();
PS2_CLK = 0;
Delay40us();
PS2_CLK = 1;
Delay20us();
PARITY = PS2_DAT;
if(!PS2_CLK) //每读完一个位都检测时钟线是否被拉低
{
ps2_send(0xfe);
return 0;
}
// SendByte(0x30+PARITY);
// SendStr("4");
//======================================================
Delay20us();
PS2_CLK = 0;
Delay40us();
PS2_CLK = 1;
Delay20us();
STOPBIT = PS2_DAT;
if(!PS2_CLK)//接收停止位,此时PS2_DAT应该为高,否则发送0xfe报错
{
ps2_send(0xfe);
return 0;
}
//======================================================
Delay15us();
PS2_DAT = 0;
Delay5us();
PS2_CLK = 0;
Delay40us();
PS2_CLK = 1;
Delay5us();
PS2_DAT = 1;
ACC = temp;
SendByte(P);
if(P == PARITY) //进行奇校验
{
// ps2_send(0xfe);
return 0;
}
// SendStr("0x33");
Delay40us();
Delay5us();
}
// SendByte(0X30+temp);
return temp;
} |