原创 20键PS2小键盘中断服务程序

2006-12-30 01:52 4402 7 7 分类: MCU/ 嵌入式


20键PS2小键盘中断服务程序
mood25.gif HotPower 发表于 2004-8-22 19:45 侃单片机 ←返回版面 按此察看该网友的资料 按此把文章加入收藏夹 按此编辑本帖举报该贴


/*------------------------------------
     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)){//搜索
              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键盘出错
}

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
7
关闭 站长推荐上一条 /3 下一条