按键长按与短按处理程序[C语言]--通过测试
#define PRESSED 0
#define Long_cnt 65534 //長鍵判斷值
#define Short_cnt 250 //短鍵判斷值,消抖动
void Key_Pressed(void)
{
unsigned int counter=0; //用做判断按键长按,短按的计数器
/********************key3_3*********************/
if(Key_up==PRESSED)
{
     while(Key_up==PRESSED)          //计数,用来判断是长按还是短按
     {
  
      if( (counter++) > Long_cnt) //counter>65534:长按,
        {
     Key_Code=KEY_FP_UP;             //KEY1长按时返回的键值
     
           return;
        }
     }
     if(counter<=Long_cnt && counter>Short_cnt)   //counter<=65534:短按,counter>20:消抖动
     {   
   
      Key_Code=KEY_FP_UP;             //KEY1短按时返回的键值
    return;        
     } 
}
/********************key3_5*********************/
if(Key_down==PRESSED)
{
     while(Key_down==PRESSED)          //计数,用来判断是长按还是短按
     {
  
      if( (counter++) > Long_cnt) //counter>65534:长按,
        {
     Key_Code=KEY_FP_DOWN;             //KEY1长按时返回的键值
    // counter=0;
           return;
        }
     }
     if(counter<=Long_cnt && counter>Short_cnt)   //counter<=65534:短按,counter>20:消抖动
     {   
   
      Key_Code=KEY_FP_DOWN;             //KEY1短按时返回的键值
   // counter=0;
           return;        
     } 
}
/********************key3_7*********************/
if(Key_start==PRESSED)
{
     while(Key_start==PRESSED)          //计数,用来判断是长按还是短按
     {
  
      if( (counter++) > Long_cnt) //counter>65534:长按
        {
     Key_Code=KEY_FP_ENTER;             //KEY1长按时返回的键值
   //   counter=0;
           return;
        }
     }
     if(counter<=Long_cnt && counter>Short_cnt)   //counter<=65534:短按,counter>20:消抖动
     {   
     
         Key_Code=KEY_FP_SELECT;             //KEY1短按时返回的键值
   // counter=0;
           return;        
     } 
}
/*****************************************************/
}