[DemoCode下载] 一种非传统的矩阵键盘扫描函数

[复制链接]
779|1
 楼主| ofsummer 发表于 2016-3-27 20:55 | 显示全部楼层 |阅读模式
//子函数delay_1ms(1)大家自己写吧
  1. #define scan_key_port P3

  2. uchar key_scan()
  3. {
  4.     uchar sccode,recode;
  5.     uchar key_value;
  6.     scan_key_port=0xf0; /* 发全 "0" 的行扫描码,列线输入 */
  7.     if((scan_key_port&0xf0)!=0xf0) /* 若有键按下 */
  8.     {
  9.         delay_1ms(1); /*延时去抖动*/
  10.         if((scan_key_port&0xf0)!=0xf0) /* 若有键按下 */
  11.         {
  12.             sccode=0xfe; /*逐行扫描初值*/
  13.             while((sccode&0x10)!=0)
  14.             {
  15.                 scan_key_port=sccode; /* 输出行扫描码 */
  16.                 if((scan_key_port&0xf0)!=0xf0) /*  本行有键按下 */
  17.                 {
  18.                     recode=(scan_key_port&0xf0)|0x0f;
  19.                     key_value=(~sccode)+(~recode);/* 返回特征码 */
  20.                  }
  21.                 else sccode=(sccode<<1)|0x01; /* 行扫描左移一位 */
  22.             }
  23.         }
  24.      }
  25.     else {         //若无键按下
  26.      scan_key_port = 0xff;
  27.   return (0);
  28. }
  29.     scan_key_port = 0xff; /*释放按键扫描端口*/
  30.     switch(key_value){ /*显示按键对应的丝印*/
  31.     case 0x11:
  32.      return('0');
  33.    
  34.     case 0x12:
  35.      return('1');
  36.    
  37.     case 0x14:
  38.      return('2');
  39.    
  40.     case 0x21:
  41.      return('3');
  42.    
  43.     case 0x22:
  44.      return('4');
  45.    
  46.     case 0x24:
  47.      return('5');
  48.    
  49.     case 0x41:
  50.      return('6');
  51.    
  52.     case 0x42:
  53.      return('7');
  54.    
  55.     case 0x44:
  56.      return('8');
  57.    
  58.     case 0x81:
  59.      return('9');
  60.    
  61.     case 0x82:
  62.      return('A');
  63.    
  64.     case 0x84:
  65.      return('B');
  66.    
  67.     case 0x18:
  68.      return('C');
  69.    
  70.     case 0x28:
  71.      return('D');
  72.    
  73.     case 0x48:
  74.      return('E');
  75.    
  76.     case 0x88:
  77.      return('F');
  78.     }
  79. }


orangebanana 发表于 2016-3-28 20:58 | 显示全部楼层
还是用的延时函数消抖,这样太浪费CPU的时间了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

10

主题

71

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部