来源:网络收集 作者:未知
/*------------------------------------
PS2键盘外部INT1中断服务程序
原创: HotPower@126.com
------------------------------------*/
void int1proc() interrupt IE1_VECTOR using 1
{
unsigned char i, key;
code unsigned char PS2TAB[] = {//20键PS2小键盘键码表
0x70,//0
0x69,//1
0x72,//2
0x7a,//3
0x6b,//4
0x73,//5
0x74,//6
0x6c,//7
0x75,//8
0x7d,//9
0x05,//F1
0x06,//F2
0x04,//F3
0x0c,//F4
0x03,//F5
0x0b,//F6
0x5a,//Enter
0x76,//Esc
0x66,//Bksp
0x71//KP.
};
if (PS2Buffers.PS2KeyCount == 0){//PS2起始位测试
if (!PS2CLOCK && !PS2DATA){//低电平是起始位
if (PS2Buffers.PS2KeyExtFlage != 0xf0){
PS2Buffers.PS2KeyTemp = 0;
PS2Buffers.PS2KeyExtFlage = 0;
PS2Buffers.PS2KeyPopError = 0;
}
PS2Buffers.PS2KeyCount ++;//脉冲计数
}
else PS2Buffers.PS2KeyPopError = 0xeb;//置键起始位错误号0xeb
}
else if (PS2Buffers.PS2KeyCount < 9){//PS2数据位
key = PS2Buffers.PS2KeyTemp;//取键盘缓冲区移位数据
key >>= 1;
if (PS2DATA) key |= 0x80;
PS2Buffers.PS2KeyTemp = key;
PS2Buffers.PS2KeyCount++;//脉冲计数
}
else if (PS2Buffers.PS2KeyCount == 9){//PS2数据奇校验
ACC = PS2Buffers.PS2KeyTemp;//取键盘缓冲区移位数据(C51取偶校验位)
if (P != PS2DATA) PS2Buffers.PS2KeyCount ++;//脉冲计数
else{
PS2Buffers.PS2KeyPopError = 0xec;////置键奇校验位错误号0xec
PS2Buffers.PS2KeyCount = 0;//脉冲计数归零
}
}
else if (PS2Buffers.PS2KeyCount == 10){//停止位
key = PS2Buffers.PS2KeyTemp;//取键盘缓冲区移位数据
PS2Buffers.PS2KeyCount = 0;//脉冲计数归零
if (PS2DATA){//高电平是停止位
if (key == 0xe0){//本次是扩展键
PS2Buffers.PS2KeyExtFlage = 0xe0;//置扩展键标志(小键盘只有回车键)
}
else if (key == 0xf0){//本次是键断码,键释放
PS2Buffers.PS2KeyExtFlage = 0xf0;//置键释放标志
}
else{//本次必为键值
if ((key == 0xaa) || (key == 0xfa)) PS2Buffers.PS2KeyPushCount = 0;//长压键计数器清零
else{
PS2CLOCK = 0;//阻止PS2键盘立即回送数据
PS2Buffers.PS2KeyPopError = 0xaa;//出错码
for (i = 0; i < 20; i++){
if (key == (PS2TAB[i])){//搜索
key = i + 1;
if (PS2Buffers.PS2KeyExtFlage == 0xf0){
key |= 0x80;//键释放
PS2Buffers.PS2KeyPushCount = 0;//长压键计数器清零
PS2Buffers.PS2KeyMessage = key;//存入当前键值并执行命令
}
else{
if (key != PS2Buffers.PS2KeyVal){//换了一个键
PS2Buffers.PS2KeyPushCount = 0;//长压键计数器清零
PS2Buffers.PS2KeyMessage = key;//存入当前键值并执行命令
}
else{//未换键
PS2Buffers.PS2KeyPushCount ++;//长压键计数器计数
if (PS2Buffers.PS2KeyPushCount > DEFPS2PUSHCOUNT){//长压时间到
PS2Buffers.PS2KeyMessage = key | 0x40;//存入当前键值并执行命令
PS2Buffers.PS2KeyPushCount = 0;//长压键计数器清零
}
}
}
PS2Buffers.PS2KeyVal = key;//存入当前键值值1~20或0x80+(1~20)
PS2Buffers.PS2KeyTemp = 0;//键码移位记录器
PS2Buffers.PS2KeyExtFlage = 0;//扩展键标志
PS2Buffers.PS2KeyPopError = 0;//键释放标志或出错码
break;
}
}
if (PS2Buffers.PS2KeyPopError) PS2Buffers.PS2KeyPushCount = 0;//长压键计数器清零
PS2CLOCK = 1;//释放PS2时钟总线
}
}
}
else PS2Buffers.PS2KeyPopError = 0xed;//置停止位错误号0xed
}
else PS2Buffers.PS2KeyCount = 0;//PS2键盘出错
}