51/AVR单片机技术驿站!  <在线翻译>     便利工具    特色网址   无弹窗、无插件的绿色站点...  英才招聘   学历查询  喜欢>>收藏我站 

当前位置:首页 > 单片机源码 > 详细内容
20键PS2小键盘中断服务程序(C)
发布时间:2009/6/4  阅读次数:1111  字体大小: 【】 【】【

来源:网络收集     作者:未知

/*------------------------------------
          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键盘出错
}

  

我要评论
  • 匿名发表
  • [添加到收藏夹]
  • 发表评论:(匿名发表无需登录,已登录用户可直接发表。) 登录状态:未登录
最新评论
所有评论[0]
    暂无已审核评论!

网站导航 管理登陆 ┊ 免责声明 问题反馈  友链说明
本站部分内容来自网络共享资源,如有冒犯您的权利请来信告之删除或纠正!
不得对本站进行复制、盗链或镜像,转载内容须获得同意或授权;欢迎友情链接、站务合作!

    我要报警 Alexa
 mcusy_cn#126.com (请把#改成@) 交流:522422171
本站学习交流群:138..158(高级群1-)、77930286(高级群2)、61804809(群3)
Copyright© MCUSY All Rights Reserved
本站网警备案号: WZ36040002485
  ICP备案证书号:粤ICP备09034963号