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

📄 keypad.c

📁 GM5621原代码
💻 C
📖 第 1 页 / 共 4 页
字号:
//
// FUNCTION:   void InitGPIODriver (BYTE ROM* Bp_Data )
// USAGE:      First configure GPIO of keypad then retrieve the converted data
// INPUT:      Bp_Data - pointer to keypad structure
// OUTPUT:     none
// GLOBALS:    none
// USED_REGS:  none
//
//******************************************************************************
gmt_KEY_INDEX far InitGPIODriver(BYTE ROM* Bp_Data )
{
   BYTE B_KeysMask = GetMaskGPIO((WBK_KEYPAD_DEFINITION ROM*)Bp_Data);
   WORD W_Address = W_ArrayOfGPIODirAddress[((WBK_KEYPAD_DEFINITION ROM*)Bp_Data)->B_ChanNumber];
   msg("W_Address = 0x%x",W_Address);
   msg("B_KeysMask = 0x%x",B_KeysMask);
   gm_WriteRegByte(W_Address, gm_ReadRegByte(W_Address) & ~B_KeysMask);
   msg("InitGPIODriver",0);
   return 0;
}


//******************************************************************************
//
// FUNCTION:   BYTE GetLGPIOValue (WBK_KEYPAD_DEFINITION ROM* Bp_Data )
// USAGE:      This function reads data from current GPIO
// INPUT:      Bp_Data - pointer to keypad structure
// OUTPUT:     physical GPIO key data
// GLOBALS:    none
// USED_REGS:  none
//
//******************************************************************************
BYTE GetLGPIOValue(WBK_KEYPAD_DEFINITION ROM* Bp_Data )
{
   BYTE B_KeysMask = GetMaskGPIO((WBK_KEYPAD_DEFINITION ROM*)Bp_Data);
   BYTE B_Channel = ((WBK_KEYPAD_DEFINITION ROM*)Bp_Data)->B_ChanNumber;

   //msg("B_KeysMask 0x%x",B_KeysMask);
   #if KeyPolarity
      return (gm_ReadRegByte(W_ArrayOfGPIOInputAddress[B_Channel]) & B_KeysMask);
   #else
      return (~gm_ReadRegByte(W_ArrayOfGPIOInputAddress[B_Channel]) & B_KeysMask);
   #endif
}

//******************************************************************************
//
// FUNCTION:   gmt_KEY_INDEX GetGPIOValue (WBK_KEYPAD_DEFINITION ROM* Bp_Data )
// USAGE:      This function returns logical data from current GPIO
// INPUT:      Bp_Data - pointer to keypad structure
// OUTPUT:     logical GPIO key data
// GLOBALS:    none
// USED_REGS:  none
//
//******************************************************************************
gmt_KEY_INDEX far GetGPIOValue(BYTE ROM* Bp_Data)
{
   BYTE B_PhKey = 0;
   gmt_KEY_INDEX KeyIndex;

   B_PhKey = GetLGPIOValue((WBK_KEYPAD_DEFINITION ROM*)Bp_Data);
   //if (B_PhKey) msg("B_PhKey 0x%x",B_PhKey);
   KeyIndex = TranslateKey(B_PhKey, (WBK_KEYPAD_DEFINITION ROM*)Bp_Data);
   //if (KeyIndex) msg("KeyIndex 0x%x",KeyIndex);
   return KeyIndex;
}
#endif   // WBK_FUNCTION_GETGPIOVALUE_USED

#ifdef   WBK_FUNCTION_GETADCVALUE_USED
static void __near ADCDelay(void)
{
// function calls alone take about 7us.
}

//******************************************************************************
//
// FUNCTION:   BYTE far InitADCDriver (WORD B_Channel)
// USAGE:      First configure the low bandwidth ADC of keypad then retrieve the converted data
// INPUT:      B_Channel
// OUTPUT:     0
// GLOBALS:    none
// USED_REGS:  none
//
//******************************************************************************
#pragma argsused
gmt_KEY_INDEX far InitADCDriver(BYTE ROM* BpData)
{
   msg("InitADCDriver",0);
   return 0;
}

//******************************************************************************
//
// FUNCTION:   BYTE far GetLBAdcValue (WORD B_Channel)
// USAGE:      First configure the low bandwidth ADC of keypad then retrieve the converted data
// INPUT:      B_Channel
// OUTPUT:     ADC key data
// GLOBALS:    none
// USED_REGS:  none
//
//******************************************************************************
BYTE GetLBAdcValue(BYTE B_Channel)
{
   BYTE B_Key = 0;
   BYTE B_Ctrl;
   
   //
   // 1) Configure LBW ADC channel 1, 2 and then 3 to handle keyboard key input
   // 2) Program the Low Bandwidth ADC control register to start conversion.
   //
   B_Ctrl = LBADC_EN | DIV_BY_1  | B_Channel;    //sweng0125
   gm_WriteRegByte (LOW_BW_ADC_CTRL, B_Ctrl);
   gm_WriteRegByte (LOW_BW_ADC_CTRL, B_Ctrl | LBADC_START_CONV);

   //
   // When conversion done, copy the conversion result to the BYTE pointer.
   //
   // LOW_BW_ADC_STATUS needs 3TCLKs to clear after a LBADC_START_CONV,
   // add delay to give time for LOW_BW_ADC_STATUS to read correctly.
   ADCDelay();
   while(((gm_ReadRegByte (LOW_BW_ADC_STATUS)) & BIT0)==0)  continue;

   B_Key = (BYTE) gm_ReadRegByte (LOW_BW_ADC_RESULT); // Read key data


   //
   // Reset the Low Bandwidth ADC control register to idle.
   //
   gm_WriteRegByte (LOW_BW_ADC_CTRL, B_Ctrl);

   #if   DEBUG_SCAN_CODE
   if(B_Key)
   {
      msg("ScanCode = %d", B_Key);
   }
   #endif

   return B_Key;
}


//******************************************************************************
//
// FUNCTION:   gmt_KEY_INDEX GetADCValue (WBK_KEYPAD_DEFINITION ROM* Bp_Data)
// USAGE:      This function returns logical key from current low ADC channel
// INPUT:      Bp_Data - pointer to keypad structure
// OUTPUT:     logical key data
// GLOBALS:    none
// USED_REGS:  none
//
//******************************************************************************
gmt_KEY_INDEX far GetADCValue(BYTE ROM* Bp_Data)
{
   BYTE B_PhKey;
   gmt_KEY_INDEX KeyIndex;
      
// msg("GetADCValue Channel = %d",((WBK_KEYPAD_DEFINITION ROM*)Bp_Data)->B_ChanNumber);   

   B_PhKey = GetLBAdcValue(((WBK_KEYPAD_DEFINITION ROM*)Bp_Data)->B_ChanNumber);

   KeyIndex = TranslateKey(B_PhKey, (WBK_KEYPAD_DEFINITION ROM*)Bp_Data);

   #if DEBUG_KEYPAD
      if(KeyIndex)
      {
         msg("B_PhKey %d",B_PhKey); 
         msg("KeyIndex %d",KeyIndex);  
      }
   #endif
   return KeyIndex;
}
#endif   // WBK_FUNCTION_GETADCVALUE_USED


//******************************************************************************
//
// FUNCTION:   gmt_KEY_INDEX TranslateKey (BYTE B_PhVal,  WBK_KEYPAD_DEFINITION ROM* Sp_KeyData)
// USAGE:      This function translates physical value to logical key
// INPUT:      Sp_KeyData - pointer to keypad structure
//          B_PhVal - physical value from keypad
// OUTPUT:     logical key data
// GLOBALS:    none
// USED_REGS:  none
//
//******************************************************************************
gmt_KEY_INDEX far  TranslateKey(BYTE B_PhVal,  WBK_KEYPAD_DEFINITION ROM* Sp_KeyData)
{
   BYTE B_CntKeyIndex = Sp_KeyData->B_KeysNumber;
   WBK_ST_KeyConvertType ROM* Sp_Translate = (WBK_ST_KeyConvertType ROM*)(Sp_KeyData->Bp_KeyValues);

   
   while(B_CntKeyIndex)
   {
      if (abs(B_PhVal - Sp_Translate->Key_Ph) <= Sp_KeyData->B_Delta)
      {
         return Sp_Translate->Key_Log;
      }
      Sp_Translate++;
      B_CntKeyIndex--;
   }

   return Key_None;
}


//******************************************************************************
//
// FUNCTION:   gmt_KEY_INDEX GetKey (void)
// USAGE:      This function returns logical key from all keypads
// INPUT:      none
// OUTPUT:     logical key data
// GLOBALS:    none
// USED_REGS:  none
//
//******************************************************************************
gmt_KEY_INDEX far GetKey(void)
{
   BYTE        B_CntKeypad;
   BYTE        B_CountKey = 0;
   
   gmt_KEY_INDEX  B_CntKeyIndex = Key_None;
   gmt_KEY_INDEX  GlobalKeyArray[WB_NUMBER_OF_INPUT_SOURCE];

   BYTE*       B_IndexPressedKey = GlobalKeyArray;
   BYTE*       Bp_CntKeypad = B_IndexPressedKey;

   for(B_CntKeypad = 0; B_CntKeypad < WBK_NumberOfKeypads; B_CntKeypad++)
   {
      WBK_KEYPAD_DEFINITION ROM* Sp_KeyData = (WBK_KEYPAD_DEFINITION ROM*)WBK_Keypads[B_CntKeypad];
      B_CntKeyIndex = ((KeypadFunc)(Sp_KeyData->GetKeyValue))((BYTE ROM*) Sp_KeyData);
     
      *Bp_CntKeypad = B_CntKeyIndex;

      if (B_CntKeyIndex)
      {
         B_CountKey++;
         B_IndexPressedKey = Bp_CntKeypad;
      }

      Bp_CntKeypad++;
   }

#if WBK_NUMBER_OF_MULTIPLE_PORTS_VALUES

   if (B_CountKey > 1)
   {
      B_CntKeyIndex = Key_None;

      if(WBK_NumberMultiplePortsValues)
      {
         BYTE B_CountI;

         WBK_ST_KeyTranslateType ROM* Sp_Translate = (WBK_ST_KeyTranslateType ROM*)(WBK_MultiplePortsValuesTranslation);

         for (B_CountI = 0; B_CountI < WBK_NumberMultiplePortsValues; B_CountI++)
         {
            BYTE B_Found = 0;
            BYTE B_CountJ;

            Bp_CntKeypad = GlobalKeyArray;

            for (B_CountJ = 0; B_CountJ < WB_NUMBER_OF_INPUT_SOURCE; B_CountJ++)
            {
               if ( Sp_Translate->B_ArrayKeys[B_CountJ] == (BYTE) * Bp_CntKeypad) B_Found ++;
               Bp_CntKeypad ++;
            }

            msg("B_Found 0x%x",B_Found);

            if (B_Found == WB_NUMBER_OF_INPUT_SOURCE)
            {
               B_CntKeyIndex = Sp_Translate->Key_Log;
               break;
            }
            Sp_Translate++;
         }
      }
   }
   else
   {
      B_CntKeyIndex = (BYTE) *B_IndexPressedKey ;
   }
#else
   B_CntKeyIndex = (BYTE) *B_IndexPressedKey;
#endif

   #if DEBUG_KEYPAD
      if(B_CntKeyIndex)
         msg("B_CntKeyIndex %d",B_CntKeyIndex);
   #endif   

   return B_CntKeyIndex;
}

//******************************************************************************
//
// FUNCTION:   gmt_KEY_INDEX KeypadInit (void)
// USAGE:      This function inits all keypads
// INPUT:      none
// OUTPUT:     none
// GLOBALS:    none
// USED_REGS:  none
//
//******************************************************************************
void far KeypadInit(void)
{
   BYTE B_CntKeypad;
   WBK_KEYPAD_DEFINITION ROM* ROM* Sp_KeyData = (WBK_KEYPAD_DEFINITION ROM* ROM*)WBK_Keypads;
   for(B_CntKeypad = WBK_NumberOfKeypads; B_CntKeypad ; B_CntKeypad--)
   {
      if((*Sp_KeyData)->InitKeypad)
      ((KeypadFunc)((*Sp_KeyData)->InitKeypad))((BYTE ROM*) Sp_KeyData);
      Sp_KeyData++;
   }
}



#else
//******************************************************************************
//  L O C A L    D E F I N I T I O N S
//******************************************************************************
#define  DEBUG_KEYPAD   0

#if DEBUG_KEYPAD && DEBUG_MSG
   #define  msg(a,b) gm_Print((const char far *)a,b)
#else
   #define msg(a,b)
#endif



#if defined(PORT_ADC_1_USED) || defined(PORT_ADC_2_USED) || defined(PORT_ADC_3_USED)
   #define  LBADC_USED     1
#else
   #define LBADC_USED      0
#endif

#if defined(PORT_GPIO_1_USED) || defined(PORT_GPIO_2_USED)
   #if GPIOKEYPAD_ALLOWED
      #define  USE_GPIO_KEYPAD      1
   #else
      #define  USE_GPIO_KEYPAD      0
   #endif
#else
   #define USE_GPIO_KEYPAD    0
#endif

#define  DEBUG_IR_CODE     0 // used to display IR scan codes out GProbe.

#define DEBUG_SCAN_CODE    0 // used to display scan codes out GProbe.
#if DEBUG_SCAN_CODE
   static BYTE B_PreKeyCode = 0xff;
#endif

#define DEBUG_INPUT        0
#define DEBUG_OSD_KEYPAD   0

//******************************************************************************
//  G L O B A L    V A R I A B L E S
//******************************************************************************
//
// 16 bits position mask.
//
WORD ROM Wa_KeyMapMask[16]       =  {0x0001, 0x0002, 0x0004, 0x0008,
                            0x0010, 0x0020, 0x0040, 0x0080,
                            0x0100, 0x0200, 0x0400, 0x0800,
                            0x1000, 0x2000, 0x4000, 0x8000};
//
// Key mapping table.
//
ST_KEY_MAP_TABLE              Sta_KeyMapTbl[NUMBER_OF_KEYS];


//******************************************************************************
//  S T A T I C    V A R I A B L E S
//******************************************************************************
static gmt_KEY_INDEX    B_ScanCode[NUMBER_OF_INPUT_SOURCE];
#if USE_GPIO_KEYPAD | LBADC_USED | defined(PORT_IR_USED)
   static BYTE             B_LastKeyType = 0;
#endif


#ifdef PORT_IR_USED
    WORD IRKey;
    static BYTE   IrIndex;
    static WORD   IrLWord;
    volatile WORD W_IrPacketCounter;

#if IR_DRIVER != USE_RC5
    volatile BYTE B_IrClockCounter;
    #if IR_DEVICE_LENGTH
       static WORD   IrAddress;
    #endif
#else
    BYTE B_EdgeState;
    WORD W_IRPeriodTIme;
#endif

    
#endif

//******************************************************************************
//  S T A T I C    F U N C T I O N    P R O T O T Y P E S                   
//******************************************************************************
static void __near ADCDelay(void);

//******************************************************************************
//  C O D E
//******************************************************************************

#ifdef PORT_IR_USED
void far interrupt IREdgeInterrupt(void);
void far interrupt IRTimerInt(void);

#if IR_DRIVER != USE_RC5
//******************************************************************************
//
// FUNCTION:    IRInit(DWORD D_CpuClock)
// USAGE:       Initialization of infrared reciever.
//          Use Mcu timer1 and external interrupt.
//
// INPUT:       D_CpuClock - CPU clock speed
// OUTPUT:      None
// GLOBALS:    None
// USED_REGS:  IRQ_CONFIG, I2CON, IMASK, T1CMPA
//
//******************************************************************************
void far IRInit(DWORD D_CpuClock)
{
   //Setup Int2 interrupt register
    gm_WriteRegByte(IRQ_CONFIG, GPIO_IRQ_IN_EN);
    WRITE_PCB_REG(I2CON, 0);

    IrIndex = 0;
    IrLWord = 0;

   SET_VECTOR(INT2_VECTOR, IREdgeInterrupt);
   CLEAR_PCB_REG_BITS(IMASK, I2);

   //Setup Timer1 time out values
   {
        DWORD D_Tmo = D_CpuClock/40000L; // Generate 0.1ms timeout 
        WRITE_PCB_REG(T1CMPA, D_Tmo);           // Configure timer1 value
        SET_VECTOR(TIMER1_VECTOR, IRTimerInt);
   }
}


//******************************************************************************
//
// FUNCTION:    interrupt IRStartInterrupt(void)
// USAGE:       Rising edge interrupt service routine
//
// INPUT:       None
// OUTPUT:      None
// GLOBALS:    None
// USED_REGS:  T1CNT, T1CON, IMASK, EOI
//
//******************************************************************************
void far interrupt IREdgeInterrupt(void)
{
   if(B_IrClockCounter > IR_LEADER)
    {
      IrIndex = 0;
        IrLWord = 0;
       #if IR_DRIVER == USE_HT6222_RC 
         W_IrPacketCounter = 0;
        #endif
    }
    else
    {
      #if IR_REPEAT
      if(B_IrClockCounter > IR_REPEAT)
        {
           W_IrPacketCounter = 0;
        }
        else
        #endif
         IrIndex++;
    }

    #if IR_DRIVER != USE_HT6222_RC
        W_IrPacketCounter = 0;
    #endif

    if( (B_IrClockCounter < IR_LOGIC0_MAX) && (B_IrClockCounter > IR_LOGIC0_MIN))

⌨️ 快捷键说明

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