关于ps2键盘驱动的问题
现在我有一个键盘连接在ps2 的接口上,我知道键盘传输数据的 ps2协议,这个ps2接口中没有控制器,只能通过软件模拟来接收字符,但是无法屏蔽键盘中断,也就是说,我只能用查询方式来取得键盘输入的数据。下面是我得代码,这段代码是用来从键盘接收一个字符扫描码的,但是我把他放在一个死循环里面就不能保证每一个接收字符都是正确的,因为有可能有偏移导致读入的数据不正确。如果有中断控制,我可以在读入数据的时候关掉中断,保证接收的数据是从正确的起始位开始读取的,但是在没有中断的情况下,我该怎么做才能保证正确性呢?
byte i, bit, p = 0, c = 0;
if( 1 != read_CLOCK()) {
printf("clock has been mask!!\n");
return 0xff; /* End of start bit clock. */
}
wait_CLOCK(0); /* Wait for start bit clock. */
if (read_DATA() != 0) {/* Check for valid stop bit. */
printf("start bit error!\n");
}
wait_CLOCK(1); /* End of start bit clock. */
for (i = 0; i < 8; i++) {
wait_CLOCK(0); /* Wait for clock pulse. */
bit = read_DATA(); /* Read data bit from pin. */
c = c + (bit << i);
p = p + bit; /* Accumulate data bit into parity. */
wait_CLOCK(1); /* Wait for end of clock pulse. */
}
wait_CLOCK(0); /* Parity bit clock. */
p = p + read_DATA(); /* Accumulate parity bit into parity. */
if ((p & 0x01) == 0) {/* Check for odd parity. */
printf("parity Error!\n");
PS2_error(); /* Parity error! */
}
wait_CLOCK(1); /* End of parity bit clock. */
wait_CLOCK(0); /* Stop bit clock. */
if (read_DATA() == 0) {/* Check for valid stop bit. */
printf("Check for stop bit Error!\n");
}
set_CLOCK(1); /* Release Clock. */
wait_us(50); /* Wait out the final clock pulse. */
value = c;
printf("%x!\n", c);
return value;