矩阵键盘扫描程序_心灵港湾_新浪博客

金波开发板矩阵键盘的扫描程序

 

#include<reg52.h>
#include<intrins.h>
#include<absacc.h>
//键盘扫描程序及其他控制的程序
//请将开发板的j7、j8、j10的跳线短接

unsigned char bdata data_574;
sbit keyout0 = data_574^0;
sbit keyout1 = data_574^1;
sbit keyout2 = data_574^2;
sbit keyout3 = data_574^3;
sbit keyout4 = data_574^4;
sbit keyout5 = data_574^5;
sbit buzzer_ = data_574^6;
sbit relay_  = data_574^7;

#define out_port XBYTE[0x8400]      //574锁存器输出地址
#define in_port XBYTE[0x8300]       //245输入地址
unsigned char key;

void delay();
unsigned char get_key();

void main()
{
    unsigned char temp;

    data_574 = 0xff;
    out_port = data_574;

    while(1)
   {
      delay();
      temp = get_key();

      if((temp != 0) && (temp != key))
                                      //当值不为零,且与上一键值不相等,说明有新键值产生
          out_port = data_574;

          switch(temp)
          {
             case 1:
                relay_ = 0;     //继电器吸合
                break;
             case 2:
                relay_ = 1;      //继电器关闭
                break;
             case 3:
                P1 = 0x00;       //led开
                break;
             case 4:
                P1 = 0xff;       //led关
                break;
             case 5:
                buzzer_ = 0;     //蜂鸣器响
                break;
             case 6:
                buzzer_ = 1;     //蜂鸣器停止
                break;
             default:
                break;
          }
          out_port = data_574;
      }

      key = temp;
   }
}

void delay()
{
   unsigned int i;
   for(i = 3000; i != 0; i--);
}


unsigned char get_key()
{
   unsigned char i,j,k,n;

   data_574 |= 0x0f;         //ff
   k = 0xfe;                 //1111_1110

   for(i = 0; i < 4; i++)
   {
      data_574 &= k;                  //ff & fe = fe  1111_1110
      out_port = data_574;
      _nop_();
      _nop_();
      _nop_();

      j = in_port;
      j |= 0xf0;                      //j | 1111_0000   保留低4位

      if(j != 0xff)      //有键按下,计算键值
                                 
          for(n = 0; n < 4; n++)
          {
             if((j | 0xfe) == 0xfe)        //1111_1110    首先判断{zd1}位
             {
                  j = i*4 + n +1;
                  data_574 |= 0x0f;
                  out_port = data_574;
                  return j;
             }
             j >>= 1;
          }
      }
      k = _crol_(k,1);//循环移1位
   }

   data_574 |= 0x0f;
   out_port = data_574;
   return 0;
}

已投稿到:
郑重声明:资讯 【矩阵键盘扫描程序_心灵港湾_新浪博客】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——