1. 程式人生 > >從Zigbee協議棧底層新增自己的按鍵配置

從Zigbee協議棧底層新增自己的按鍵配置

#define PUSH1_BV          BV(1) #define PUSH1_SBIT        P0_1 #if defined (HAL_BOARD_CC2530EB_REV17)   #define PUSH1_POLARITY    ACTIVE_LOW #elif defined (HAL_BOARD_CC2530EB_REV13)   #define PUSH1_POLARITY    ACTIVE_LOW #else   #error Unknown Board Indentifier #endif 下面模仿/* S6 */下的程式定義自己的按鍵值: /* S8 */ #define PUSH8_BV            BV(4)//修改 #define PUSH8_SBIT        P0_4//修改 #if defined (HAL_BOARD_CC2530EB_REV17)   #define PUSH8_POLARITY    ACTIVE_HIGH #elif defined (HAL_BOARD_CC2530EB_REV13)   #define PUSH8_POLARITY    ACTIVE_LOW #else   #error Unknown Board Indentifier #endif 圖2:
------------------------------------------------------------------------------------------------------------- 在/* ----------- Push Buttons ---------- */ #define HAL_PUSH_BUTTON1()        (PUSH1_POLARITY (PUSH1_SBIT)) #define HAL_PUSH_BUTTON2()        (PUSH2_POLARITY (PUSH2_SBIT)) #define HAL_PUSH_BUTTON3()        (0) #define HAL_PUSH_BUTTON4()        (0) #define HAL_PUSH_BUTTON5()        (0) #define HAL_PUSH_BUTTON6()        (0) 下定義自己的按鍵函式 #define HAL_PUSH_BUTTON8()        (PUSH8_POLARITY (PUSH8_SBIT))
圖3: ---------------------------------------------------------------------------------------------------------------- hal_key.c
在/* SW_6 is at P0.1 */ #define HAL_KEY_SW_6_PORT   P0 #define HAL_KEY_SW_6_BIT    BV(1) #define HAL_KEY_SW_6_SEL    P0SEL #define HAL_KEY_SW_6_DIR    P0DIR /* edge interrupt */ #define HAL_KEY_SW_6_EDGEBIT  BV(0) #define HAL_KEY_SW_6_EDGE     HAL_KEY_FALLING_EDGE /* SW_6 interrupts */ #define HAL_KEY_SW_6_IEN      IEN1  /* CPU interrupt mask register */ #define HAL_KEY_SW_6_IENBIT   BV(5) /* Mask bit for all of Port_0 */ #define HAL_KEY_SW_6_ICTL     P0IEN /* Port Interrupt Control register */ #define HAL_KEY_SW_6_ICTLBIT  BV(1) /* P0IEN - P0.1 enable/disable bit */ #define HAL_KEY_SW_6_PXIFG    P0IFG /* Interrupt flag at source */ 下模仿/* SW_6 is at P0.1 */建立自己的按鍵函式 #define HAL_KEY_SW_8_PORT   P0
#define HAL_KEY_SW_8_BIT    BV(4)    //修改 #define HAL_KEY_SW_8_SEL    P0SEL #define HAL_KEY_SW_8_DIR    P0DIR /* edge interrupt */ #define HAL_KEY_SW_8_EDGEBIT  BV(0) #define HAL_KEY_SW_8_EDGE     HAL_KEY_FALLING_EDGE /* SW_8 interrupts */ #define HAL_KEY_SW_8_IEN      IEN1  /* CPU interrupt mask register */
#define HAL_KEY_SW_8_IENBIT   BV(5) /* Mask bit for all of Port_0 */ #define HAL_KEY_SW_8_ICTL     P0IEN /* Port Interrupt Control register */ #define HAL_KEY_SW_8_ICTLBIT  BV(4)   //修改 #define HAL_KEY_SW_8_PXIFG    P0IFG /* Interrupt flag at source */ 圖4:
------------------------------------------------------------------------------------------------------------- 注意:將 void HalKeyPoll (void)中的 //  if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT))  /* Key is active HIGH */ //  { //    keys = halGetJoyKeyInput(); //  }   /* If interrupts are not enabled, previous key status and current key status    * are compared to find out if a key has changed status.    */  // if (!Hal_KeyIntEnable) //  { //    if (keys == halKeySavedKeys) //    {       /* Exit - since no keys have changed */ //      return; //    }     /* Store the current keys for comparation next time */ //    halKeySavedKeys = keys;  // } //  else  // {     /* Key interrupt handled here */  // } 圖5:
全部註釋掉,因為它會對我們設定的按鍵產生干擾,具體情況我也不知道... 然後再在內模仿:   if (HAL_PUSH_BUTTON1())
  {
    keys |= HAL_KEY_SW_6;
  }
新增  : if (HAL_PUSH_BUTTON8())   {     keys |= HAL_KEY_SW_8;   } 圖6:
------------------------------------------------------------------------------------------------------------- OnBard.c
將 void InitBoard( uint8 level )
{
  if ( level == OB_COLD )
  {
    // IAR does not zero-out this byte below the XSTACK.
    *(uint8 *)0x0 = 0;
    // Interrupts off
    osal_int_disable( INTS_ALL );
    // Check for Brown-Out reset
    ChkReset();
  }
  else  // !OB_COLD
  {
    /* Initialize Key stuff */
    HalKeyConfig(HAL_KEY_INTERRUPT_DISABLE, OnBoard_KeyCallback);//修改此處
  }
}
改為: HalKeyConfig(HAL_KEY_INTERRUPT_ENABLE, OnBoard_KeyCallback);
記得在任務初始化函式中加入 RegisterForKeys( GenericApp_TaskID );     //註冊按鍵事件
圖7:
最後再在Coordinator.c中的 uint16 GenericApp_ProcessEvent( uint8 task_id, uint16 events ) 新增事件及其處理函式 case KEY_CHANGE:           GenericApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys ); 再在 static void GenericApp_HandleKeys( uint8 shift, uint8 keys ) {   zAddrType_t dstAddr;    if ( keys & HAL_KEY_SW_1 )     {     }     if ( keys & HAL_KEY_SW_2 )     {     }     if ( keys & HAL_KEY_SW_3 )     {     }     if ( keys & HAL_KEY_SW_4 )     {     }     if ( keys & HAL_KEY_SW_8 ) //新增自己的按鍵及其處理函式     {       HalLedSet(HAL_LED_1, HAL_LED_MODE_FLASH);     }   } 最後燒到開發板中即可,祝大家實驗成功。