car-eye 车辆管理开源平台 MCU 系统之 按键扫描处理

臧俊杰
2023-12-01

在MCU系统中按键处理经常要处理扫描,去抖等操作。按键一般是在定时器开动以后进行扫描处理,如没个一段时间进入

一个按键处理,判断按键的状态当按键持续时间达到一定数值的时候才认为是按键被按下:

void Bsp_Key_Check(void)
{
 static u32 keytime= 0;
 
 if(CheckSysTick((u32 *)&keytime, 20)) //每隔20 ms进入按键处理
 { 
  GetSysTick((u32 *)&keytime);
  Key2GroupCheck();
     Key1GroupCheck();

}

}

按键处理:

void Key2GroupCheck(void)
{
 u16 AdcVal;
 AdcVal = KeyAdcValue[ADC_CALL_CHAN];

 if(AdcVal > ADC_RELEASE_VAL)
 {
  TeleKey.CurState = RELEASE_NULL;
  //MyPrintf("AdcVal [%d]\r\n\r\n",AdcVal);
 }else if((AdcVal < ADC_KEY0_H_VAL) && (AdcVal > ADC_KEY0_L_VAL))
 {
  TeleKey.CurState  = FMON_KEY;
 }
 else if((AdcVal < ADC_KEY1_H_VAL) && (AdcVal > ADC_KEY1_L_VAL))
 {
  TeleKey.CurState = HOME_KEY;
 }
 else if((AdcVal < ADC_KEY2_H_VAL) && (AdcVal > ADC_KEY2_L_VAL))
 { 
  TeleKey.CurState =     CANCEL_KEY;
 }
 else if(AdcVal < ADC_KEY3_VAL)
 {
  TeleKey.CurState =   FMON_KEY;
 }
 //MyPrintf("AdcVal [%d]\r\n\r\n",AdcVal);
 switch(TeleKey.State)
 {
 case RELEASE_NULL:

  if(TeleKey.CurState == RELEASE_NULL)
  {
   TeleKey.aCnt = 0;
   TeleKey.vCnt = 0;
   TeleKey.Sendtofg = 0;
  }else
  {
     
   TeleKey.aCnt = 0;
   TeleKey.vCnt = 0;
   TeleKey.Sendtofg = 0;
   TeleKey.State = TeleKey.CurState ;
  }
  break;
 default:
     if(TeleKey.CurState == TeleKey.State)
  {
   if(TeleKey.aCnt < 5)
   {
    TeleKey.aCnt ++;
   }
   else
   {
    if(TeleKey.Sendtofg == 0)
    {  
     
     TeleKey.Sendtofg = 0xFF;
     KeyState(TeleKey.CurState);
    
     MyPrintf("TeleKey [%d]\r\n\r\n",TeleKey.CurState); 
 #ifdef FM_MODULE
     if(TeleKey.CurState == FMON_KEY)
     {
      if(g_DeviceStatus.FMStatus == DEVICE_OFF )
      {
       MyPrintf("FM ON [%d]\r\n\r\n",TeleKey.CurState);
       AUDIO_MUTE_EN ;
       FM_ON;
       SendFMStatus(ON);
       TimeDly(30); // 150ms//
       Tuner_Init();
       Bsp_Laba_Ctrl(FM_START);
       AUDIO_MUTE_DIS;       
       g_DeviceStatus.FMStatus = DEVICE_ON;
      

      }else
      {
       MyPrintf("FM OFF [%d]\r\n\r\n",TeleKey.CurState);       
       Bsp_Laba_Ctrl(FM_END);
       FM_OFF;
       SendFMStatus(OFF);
       Delay(100);
       g_DeviceStatus.FMStatus = DEVICE_OFF;
      }
     
     } 
#endif      
    }

   }
  }else
  {
   if(TeleKey.vCnt < 3)
   {
    TeleKey.vCnt ++;
   }
   else
   {
    Trace("松开\r\n");
    TeleKey.aCnt = 0;
    TeleKey.vCnt = 0;
    TeleKey.Sendtofg = 0;
    TeleKey.State = RELEASE_NULL;
   } 
  } 
 
  break;
 }   
}


按键处理的去抖动,和扫描完成后一个按键数值才被确定。

相关代码下载:car-eye开源平台网址:https://github.com/Car-eye-admin/ 有关car-eye技术咨询可以加QQ群590411159















 类似资料: