zoukankan      html  css  js  c++  java
  • 独立按键的双击按键触发------类似于鼠标的双击

    #include "REG52.H"
    #define const_voice_short 40
    #define const_key_time1 20
    #define const_key_time2 20
    #define const_interval_time1 200 //连续两次按键之间的有效时间差
    #define const_interval_time2 200 //如果超过这个时间,则视为无效
    void initial_myself();
    void initial_peripheral();
    void delay_long(unsigned int uiDelayLong);
    void T0_time();
    void key_scan();
    void key_service();
    sbit key_sr1=P0^0;
    sbit key_sr2=P0^1;
    sbit key_gnd_dr=P0^4;
    sbit beep_dr=P1^5;
    unsigned char ucKeySec=0;
    unsigned int uiKeyTimeCnt1=0;
    unsigned char ucKeyLock1=0;
    unsigned char ucKeyTouchCnt1=0;  //按键按下的次数记录
    unsigned int uiKeyIntervalCnt1=0; //按键间隔的时间计数器
    unsigned int uiKeyTimeCnt2=0;
    unsigned char ucKeyLock2=0;
    unsigned char ucKeyTouchCnt2=0;
    unsigned int uiKeyIntervalCnt2=0;
    unsigned int uiVoiceCnt=0;
    void main()
    {
     initial_myself();
     delay_long(100);
     initial_peripheral();
     while(1)
     {
      key_service();
     }
    }
    void key_scan()
    {
     if(key_sr1==1)
     {
      ucKeyLock1=0;
      uiKeyTimeCnt1=0;
      if(ucKeyTouchCnt1>0) //之前已经有按键触发过一次了,再来一次就构成了双击
      {
       uiKeyIntervalCnt1++; //按键间隔的时间计数器累加
       if(uiKeyIntervalCnt1>const_interval_time1) //超过最大允许的间隔时间
       {
        uiKeyIntervalCnt1=0; //时间计数器清零
        ucKeyTouchCnt1=0;  //清零按键按下去的次数
       }
      }
     }
     else if(ucKeyLock1==0) //有按键按下,且是第一次被按下
     {
      uiKeyTimeCnt1++;
      if(uiKeyTimeCnt1>const_key_time1)
      {
       uiKeyTimeCnt1=0;
       ucKeyLock1=1; //自锁按键置位,避免一直触发
       uiKeyIntervalCnt1=0; //按键有效间隔的时间计数器清零
       ucKeyTouchCnt1++;
       if(ucKeyTouchCnt1>1) //连续被按了两次以上
       {
        ucKeyTouchCnt1=0; //统计按键次数清零
        ucKeySec=1;   //触发1号按键
       }
      }
     }
     
     
     if(key_sr2==1)
     {
      ucKeyLock2=0;
      uiKeyTimeCnt2=0;
      if(ucKeyTouchCnt2>0) //之前已经有按键触发过一次了,再来一次就构成了双击
      {
       uiKeyIntervalCnt2++; //按键间隔的时间计数器累加
       if(uiKeyIntervalCnt2>const_interval_time2) //超过最大允许的间隔时间
       {
        uiKeyIntervalCnt2=0; //时间计数器清零
        ucKeyTouchCnt2=0;  //清零按键按下去的次数
       }
      }
     }
     else if(ucKeyLock2==0) //有按键按下,且是第一次被按下
     {
      uiKeyTimeCnt2++;
      if(uiKeyTimeCnt2>const_key_time2)
      {
       uiKeyTimeCnt2=0;
       ucKeyLock2=1; //自锁按键置位,避免一直触发
       uiKeyIntervalCnt2=0; //按键有效间隔的时间计数器清零
       ucKeyTouchCnt2++;
       if(ucKeyTouchCnt2>1) //连续被按了两次以上!!!这很关键!!!
       {
        ucKeyTouchCnt2=0; //统计按键次数清零
        ucKeySec=1;   //触发1号按键
       }
      }
     }
    }
    void key_service()
    {
     switch(ucKeySec)
     {
      case 1:  //1号键, 双击
       uiVoiceCnt=const_voice_short;
       ucKeySec=0;
       break;
      case 2:
       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()
    {
     key_gnd_dr=0;
     beep_dr=1;
     TMOD=0x01;
     TH0=0xf8;
     TL0=0x2f;
    }
    void initial_peripheral()
    {
     EA=1;
     ET0=1;
     TR0=1;
    }
     
     
  • 相关阅读:
    Eclipse汉化
    Sublime Text 3 插件安装
    HTML5中canvas的save和restore方法
    No module ata_piix found的解决方法
    在虚拟机和主机之间共享文件夹
    C语言:文件操作
    ubuntu下的第一个脚本file.sh
    解析java源文件
    Compile Java Codes in Linux Shell instead of Ant Script
    Eclipse的XML编辑器解决方案
  • 原文地址:https://www.cnblogs.com/TheFly/p/11963953.html
Copyright © 2011-2022 走看看