zoukankan      html  css  js  c++  java
  • 矩阵按键单个触发的压缩代码

    #include "REG52.H"
    #define const_voice_short 40
    #define const_key_time 20
    void initial_myself();
    void initial_peripheral();
    void delay_long(unsigned int uiDelayLong);
    void T0_time();
    void key_service();
    void key_scan();
    sbit key_sr1=P0^1;
    sbit key_sr2=P0^2;
    sbit key_sr3=P0^3;
    sbit key_dr1=P0^4;
    sbit key_dr2=P0^5;
    sbit key_dr3=P0^6;
    sbit key_dr4=P0^7;
    sbit beep_dr=P1^5;
    unsigned char ucKeyStep=1;
    unsigned char ucKeySec=0;
    unsigned int uiKeyTimeCnt=0;
    unsigned char ucKeyLock=0;
    unsigned char ucListRecord=1; //记录当前扫描到了第几列
    unsigned int uiVoiceCnt=0;
    void main()
    {
     initial_myself();
     delay_long(100);
     initial_peripheral();
     while(1)
     {
      key_service();
     }
    }
    void key_scan()
    {
     /*
      矩阵按键扫描的详细过程:
      先输出某一列低电平,其它三列输出高电平,这个时候再分别判断输入的三行,
      如果发现哪一行是低电平,就说明对应的按键触发。依次分别输出另外三列中
      的某一列为低电平,在分别判断输入的三行,就可以检测完12个按键。
     */
     switch(ucKeyStep)
     {
      case 1: //按键输出低ucListRecord列低电平
       if(ucListRecord==1) //第一列低电平
       {
        key_dr1=0;
        key_dr2=1;
        key_dr3=1;
        key_dr4=1;
       }
       
       else if(ucListRecord==2) //第二列低电平
       {
        key_dr1=1;
        key_dr2=0;
        key_dr3=1;
        key_dr4=1;
       }
       
       else if(ucListRecord==3) //第三列低电平
       {
        key_dr1=1;
        key_dr2=1;
        key_dr3=0;
        key_dr4=1;
       }
       
       else if(ucListRecord==4) //第四列低电平
       {
        key_dr1=1;
        key_dr2=1;
        key_dr3=1;
        key_dr4=0;
       }
       
       uiKeyTimeCnt=0;  //延时计数器清零
       ucKeyStep++;  //切换到下一运行步骤
       break;
       
      case 2:
       uiKeyTimeCnt++;
       if(uiKeyTimeCnt>1)
       {
        uiKeyTimeCnt=0;
        ucKeyStep++;
       }
       break;
       
      case 3:
       if(key_sr1==1&&key_sr2==1&&key_sr3==1)
       {
        ucKeyStep=1; //如果没有按键按下,返回到第一个运行步骤重新开始扫描
        ucKeyLock=0;
        uiKeyTimeCnt=0;
        
        ucListRecord++; //输出到下一列
        if(ucListRecord>4)
        {
         ucListRecord=1;  //依次输出完四列后,继续从第一列开始输出低电平
        }
       }
       else if(ucKeyLock==0)
       {
        if(key_sr1==0&&key_sr2==1&&key_sr3==1)
        {
         uiKeyTimeCnt++;
         if(uiKeyTimeCnt>const_key_time)
         {
          uiKeyTimeCnt=0;
          ucKeyLock=1;
          
          if(ucListRecord==1)
          {
           ucKeySec=1;
          }
          else if (ucListRecord==2)
          {
           ucKeySec=2;
          }
          else if (ucListRecord==3)
          {
           ucKeySec=3;
          }
          else
          {
           ucKeySec=4;
          }
         }
        }
       
        else if(key_sr1==1&&key_sr2==0&&key_sr3==1)
        {
         uiKeyTimeCnt++;
         if(uiKeyTimeCnt>const_key_time)
         {
          uiKeyTimeCnt=0;
          ucKeyLock=1;
          
          if(ucListRecord==1)
          {
           ucKeySec=5;
          }
          else if (ucListRecord==2)
          {
           ucKeySec=6;
          }
          else if (ucListRecord==3)
          {
           ucKeySec=7;
          }
          else
          {
           ucKeySec=8;
          }
         }
        }
        
        else if(key_sr1==1&&key_sr2==1&&key_sr3==0)
        {
         uiKeyTimeCnt++;
         if(uiKeyTimeCnt>const_key_time)
         {
          uiKeyTimeCnt=0;
          ucKeyLock=1;
          
          if(ucListRecord==1)
          {
           ucKeySec=9;
          }
          else if (ucListRecord==2)
          {
           ucKeySec=10;
          }
          else if (ucListRecord==3)
          {
           ucKeySec=11;
          }
          else
          {
           ucKeySec=12;
          }
         }
        } 
       break; 
      }
     }
    }
    void key_service() //第三区,按键服务应用程序
    {
     switch(ucKeySec)
     {
      case 1:
       uiVoiceCnt=const_voice_short;
       ucKeySec=0;
       break;
       
      case 2:
       uiVoiceCnt=const_voice_short;
       ucKeySec=0;
       break;
      case 3:
       uiVoiceCnt=const_voice_short;
       ucKeySec=0;
       break;
      case 4:
       uiVoiceCnt=const_voice_short;
       ucKeySec=0;
       break;
      case 5:
       uiVoiceCnt=const_voice_short;
       ucKeySec=0;
       break;
      case 6:
       uiVoiceCnt=const_voice_short;
       ucKeySec=0;
       break;
      case 7:
       uiVoiceCnt=const_voice_short;
       ucKeySec=0;
       break;
      case 8:
       uiVoiceCnt=const_voice_short;
       ucKeySec=0;
       break;
      case 9:
       uiVoiceCnt=const_voice_short;
       ucKeySec=0;
       break;
      case 10:
       uiVoiceCnt=const_voice_short;
       ucKeySec=0;
       break; 
       
      case 11:
       uiVoiceCnt=const_voice_short;
       ucKeySec=0;
       break;
      case 12:
       uiVoiceCnt=const_voice_short;
       ucKeySec=0;
       break; 
     }
    }
    void T0_time() interrupt 1
    {
     TF0=0;
     TR0=0;
     
     key_scan();
     
     if(uiVoiceCnt!=0)
     {
      uiVoiceCnt--;
      beep_dr=0;
     }
     else
     {
      ;
      beep_dr=1;
     }
     
     TH0=0xf8;
     TL0=0x2f;
     TR0=1;
    }
    void delay_long(unsigned int uiDelayLong)
    {
     unsigned int i;
     unsigned int j;
     for(i=0;i<uiDelayLong;i++)
      for(j=0;j<500;j++)
       ;
    }
    void initial_myself()
    {
     beep_dr=1;
     TMOD=0x01;
     TH0=0xf8;
     TL0=0x2f;
    }
    void initial_peripheral()
    {
     EA=1;
     ET0=1;
     TR0=1;
    }
     
  • 相关阅读:
    Mysql 命令行连接
    linux下安装MongoDB数据库
    SVN 提交常见报错及解决方案
    解决 SVN Skipped 'xxx' -- Node remains in conflict
    linux svn 切换用户
    SQL基础语法
    yml
    搭建笔记(1)
    文件上传MultipartFile
    18.线程池
  • 原文地址:https://www.cnblogs.com/TheFly/p/11994558.html
Copyright © 2011-2022 走看看