您的位置:控制工程论坛网论坛 » 自动化软件 » 面板按键扫描程序

xilinxue

xilinxue   |   当前状态:在线

总积分:16186  2024年可用积分:0

注册时间: 2008-06-26

最后登录时间: 2020-03-22

空间 发短消息加为好友

面板按键扫描程序

xilinxue  发表于 2008/11/14 18:46:10      889 查看 1 回复  [上一主题]  [下一主题]

手机阅读

/*###############################################################################
**文件:ScanKey.c
**说明:面板按键扫描程序.
**###############################################################################
*/
#include "Global.h"


#ifdef Use_Key_UpAndDown_Active
       bit                 b_Key_ActiveAtDown;
       bit                 b_Key_ActiveAtDownOK;
       //unsigned char idata g_Key_CounterAtDown;
       unsigned char       g_Key_CounterAtDown;
#endif //Use_Key_UpAndDown_Active


#ifdef Use_Front_Keyboard    /*面板按键扫描程序.*/
       #define C_Key_RepeatTimer_Start         (0x20)
       #define C_Key_RepeatTimer_Repeating     (0x03)
       #define C_Keyboard_Range_MIN            (0x41) /*65*/
       #define C_PopUpKey_ActiveTime           (0x10)


       unsigned char g_NewKey;
       unsigned char g_NewKeyBak;
       bit           b_KeyStatus;
       unsigned char g_KeyRepeatTimer;
       bit           b_KeyPressFirstTime;



       #ifdef Use_Keyboard_IO
       sbit Pin_Key_1=CPU_Pin_16;
       //sbit Pin_Key_2=CPU_Pin_15;
       //sbit Pin_Key_3=CPU_Pin_34;
       //sbit Pin_Key_4=CPU_Pin_35;
       #endif //Use_Keyboard_IO



/********************************************************************************
**函数:
**功能:
**参数:
**返回:
**备注:
*********************************************************************************
*/
bit GetKey(void)
{


    #ifdef Use_Keyboard_IO        /*IO口驱动的面板按键驱动实体.<========经常改动.*/
    //unsigned char i;
    Pin_Key_1=HIGH;
    if (Pin_Key_1==LOW)
    {
         g_NewKey=0;
         goto Code_GetKey_HaveKey;
    }
    Pin_Key_1=LOW;
    b_KeyStatus=C_KeyStatus_Pop_Up;
    return (FALSE);
Code_GetKey_HaveKey:
    b_KeyStatus=C_KeyStatus_Press_Down;
    return (TRUE);
    #endif //Use_Keyboard_IO


    return (FALSE);


}//GetKey()------------------------------end.


#endif //Use_Front_Keyboard



#ifdef Use_Sys_UI
void ScanKey(void)
{
//unsigned char idata i;
unsigned char i;
    #ifdef Use_Key_UpAndDown_Active      /*a local variable.*/
//unsigned char idata j;
unsigned char j;
    #endif //Use_Key_UpAndDown_Active


    if (b_KeyStatus==C_KeyStatus_Pop_Up)
    {
          /*按一下的键.*/
          if (GetKey())
          {
                if (b_KeyPressFirstTime)
                {
                      /*再次检测到按下的键,此时确认按键有效.*/
                      b_KeyPressFirstTime=FALSE;
                      if (g_NewKey==g_NewKeyBak)
                      {
                            g_KeyRepeatTimer=C_Key_RepeatTimer_Start;
                            g_SystemKeyCode=C_Keyboard_Range_MIN+g_NewKey;
                            b_SystemRepeatKeyOK=FALSE;
                            b_SystemKeyOK=TRUE;
                            b_Key_ComeFrom=C_Key_ComeFrom_Keyboard;
                      }
                }//if (b_KeyPressFirstTime)-------------------
                else
                {
                      /*第一次检测到按下的键.*/
                      b_KeyPressFirstTime=TRUE;
                      b_KeyStatus=C_KeyStatus_Pop_Up;
                      g_NewKeyBak=g_NewKey;
                      g_NewKey=C_NewKey_NULL;
                }//if (b_KeyPressFirstTime)-------else---------
          }//if (GetKey())---------------
    }//if (b_KeyStatus==C_KeyStatus_Pop_Up)--------
    else
    {
          /*连发和长按的键.*/
          if (GetKey())
          {
               
                //=====================================================================
                //repeat keys checking.
                //=====================================================================
                for (i=0;i<C_Key_RepeatKey_Number;i++)
                {
                       if (Tab_KeyBoardRepeatKey[i]==g_SystemKeyCode)
                       {
                             g_KeyRepeatTimer--;
                             if (g_KeyRepeatTimer==0x00)
                             {
                                   g_KeyRepeatTimer=C_Key_RepeatTimer_Repeating;
                                   b_SystemKeyOK=TRUE;
                                   b_Key_ComeFrom=C_Key_ComeFrom_Keyboard;
                                   b_SystemRepeatKeyOK=TRUE;
                             }
                       }//if (Tab_KeyBoardRepeatKey[i]==g_SystemKeyCode)-------
                }//for (i=0;i<C_Key_RepeatKey_Number;i++)------
          }//if (GetKey())---------
          g_NewKey=C_NewKey_NULL;
          g_NewKeyBak=C_NewKey_NULL;
    }//if (b_KeyStatus==C_KeyStatus_Pop_Up)-------else-------------
    return;


}//ScanKey()------------------------------------------------------end.
#endif //Use_Sys_UI

1楼 0 0 回复
  • xilinxue

    xilinxue   |   当前状态:在线

    总积分:16186  2024年可用积分:0

    注册时间: 2008-06-26

    最后登录时间: 2020-03-22

    空间 发短消息加为好友

    xilinxue   发表于 2008/11/14 18:46:10

     就在后面跟帖。我希望大家能多多交流才能得到更好的提高,也希望高手能多多指教,谢谢了
    2楼 回复本楼

    引用 xilinxue 2008/11/14 18:46:10 发表于2楼的内容

总共 , 当前 /