⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keys.c

📁 基于89S52的电子时钟
💻 C
字号:

#include "main.h"
#include "KEYS.h"
KEYBoard  Key;

/*********************************************************************************************/
void KEYBoardInit(void)
{
 Key.Value=0;			   //Key初始化
 Key.OldValue=0;
 Key.NewValue=0;
 Key.PressedTime=0;
}
///////////////////////////////////////////////////////////////////////////////////////////////

/*********************************************************************************************/
void ScanKey(void)                //键盘扫描
{ 
  P3 = 255; //51按键扫描必须先写1, 
  if(Key.Value)return;         //如果上次按键还未处理,则直接返回,不扫描新的按键

  if(KEY1IN && KEY2IN )    //如果输入全部是高,则按键处于释放状态
  {
     if(!(Key.OldValue))                        //如果上次无按键按下
     {
     return;                           //返回
     }
    else                                //如果上次有按键按下
     {
	   if(Key.LongKey==1)
	   {
	    Key.LongKey=0;
	    Key.PressedTime=0;
	    }
      
	   if(Key.PressedTime>70)        //按键时间超过一秒,长按键码
        {
         Key.Value=Key.OldValue+0x80;   //长按键码等于普通按键码加0x80
        }	
        
		else if(Key.PressedTime>1)
        {
        Key.Value=Key.OldValue;        //按键值
        }
        Key.OldValue=0;
       }
      return;
     }
 else                                  //按键按下
  {
    Key.NewValue=0;
   if(!KEY1IN)Key.NewValue=KEY1;        //KEY1键按下
   if(!KEY2IN)Key.NewValue=KEY2;        //KEY2键按下
   //if(!KEY3IN)Key.NewValue=KEY3;        //KEY3键按下
   //if(!KEY4IN)Key.NewValue=KEY4;        //KEY4键按下

   if(Key.NewValue==Key.OldValue)         //如果连续两次扫描到一样的键,即按键按下长于10ms
    { 
     if(Key.PressedTime>180)                   //如果按键按下超过2s
      {
	   Key.LongKey=1;
       Key.PressedTime-=6;                     //则自动按键。相当于每隔50ms按一次
       Key.Value=Key.NewValue;
       return;
      }
     else
      {  							
       Key.PressedTime++;                     //按键按下计时加一
      }
    }
   else
    {
     Key.PressedTime=0;                       //如果本次按下跟上次按下不一样,则按键按下时间清零
    }
   Key.OldValue=Key.NewValue;                 //保存本次按键值
  }
}
///////////////////////////////////////////////////////////////////////////////////////////////
																							 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -