本实验是基于ZStack-CC2530-2.5.1a版本的协议栈来进行实验的,整个实验需要改动hal_board_cfg.h、hal_board_cfg.h、hal_key.c、hal_key.h和自己定义的Coordinator.c这5个文件。
注意:添加自己的按键时尽量不要修改协议栈里面的按键程序,自己另行添加即可。

hal_key.h

在/ Switches (keys) /下面添加自己的按键定义

define HAL_KEY_SW_8 0x80


从Zigbee协议栈底层添加自己的按键配置 - 图1

hal_board_cfg.h

在/ S6 /

  1. #define PUSH1_BV BV(1)
  2. #define PUSH1_SBIT P0_1
  3. #if defined (HAL_BOARD_CC2530EB_REV17)
  4. #define PUSH1_POLARITY ACTIVE_LOW
  5. #elif defined (HAL_BOARD_CC2530EB_REV13)
  6. #define PUSH1_POLARITY ACTIVE_LOW
  7. #else
  8. #error Unknown Board Indentifier
  9. #endif

下面模仿/ S6 /下的程序定义自己的按键值:

  1. /* S8 */
  2. #define PUSH8_BV BV(4)//修改
  3. #define PUSH8_SBIT P0_4//修改
  4. #if defined (HAL_BOARD_CC2530EB_REV17)
  5. #define PUSH8_POLARITY ACTIVE_HIGH
  6. #elif defined (HAL_BOARD_CC2530EB_REV13)
  7. #define PUSH8_POLARITY ACTIVE_LOW
  8. #else
  9. #error Unknown Board Indentifier
  10. #endif

从Zigbee协议栈底层添加自己的按键配置 - 图2

——————————————————————————————————————————————————————-
在/ —————- Push Buttons ————— /

  1. #define HAL_PUSH_BUTTON1() (PUSH1_POLARITY (PUSH1_SBIT))
  2. #define HAL_PUSH_BUTTON2() (PUSH2_POLARITY (PUSH2_SBIT))
  3. #define HAL_PUSH_BUTTON3() (0)
  4. #define HAL_PUSH_BUTTON4() (0)
  5. #define HAL_PUSH_BUTTON5() (0)
  6. #define HAL_PUSH_BUTTON6() (0)
  7. //下定义自己的按键函数
  8. #define HAL_PUSH_BUTTON8() (PUSH8_POLARITY (PUSH8_SBIT))

从Zigbee协议栈底层添加自己的按键配置 - 图3

hal_key.c

  1. /* SW_6 is at P0.1 */
  2. #define HAL_KEY_SW_6_PORT P0
  3. #define HAL_KEY_SW_6_BIT BV(1)
  4. #define HAL_KEY_SW_6_SEL P0SEL
  5. #define HAL_KEY_SW_6_DIR P0DIR
  6. /* edge interrupt */
  7. #define HAL_KEY_SW_6_EDGEBIT BV(0)
  8. #define HAL_KEY_SW_6_EDGE HAL_KEY_FALLING_EDGE
  9. /* SW_6 interrupts */
  10. #define HAL_KEY_SW_6_IEN IEN1 /* CPU interrupt mask register */
  11. #define HAL_KEY_SW_6_IENBIT BV(5) /* Mask bit for all of Port_0 */
  12. #define HAL_KEY_SW_6_ICTL P0IEN /* Port Interrupt Control register */
  13. #define HAL_KEY_SW_6_ICTLBIT BV(1) /* P0IEN - P0.1 enable/disable bit */
  14. #define HAL_KEY_SW_6_PXIFG P0IFG /* Interrupt flag at source */
  15. 下模仿/* SW_6 is at P0.1 */建立自己的按键函数
  16. #define HAL_KEY_SW_8_PORT P0
  17. #define HAL_KEY_SW_8_BIT BV(4) //修改
  18. #define HAL_KEY_SW_8_SEL P0SEL
  19. #define HAL_KEY_SW_8_DIR P0DIR
  20. /* edge interrupt */
  21. #define HAL_KEY_SW_8_EDGEBIT BV(0)
  22. #define HAL_KEY_SW_8_EDGE HAL_KEY_FALLING_EDGE
  23. /* SW_8 interrupts */
  24. #define HAL_KEY_SW_8_IEN IEN1 /* CPU interrupt mask register */
  25. #define HAL_KEY_SW_8_IENBIT BV(5) /* Mask bit for all of Port_0 */
  26. #define HAL_KEY_SW_8_ICTL P0IEN /* Port Interrupt Control register */
  27. #define HAL_KEY_SW_8_ICTLBIT BV(4) //修改
  28. #define HAL_KEY_SW_8_PXIFG P0IFG /* Interrupt flag at source */

从Zigbee协议栈底层添加自己的按键配置 - 图4

注意

  1. void HalKeyPoll (void)中的
  2. // if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT)) /* Key is active HIGH */
  3. // {
  4. // keys = halGetJoyKeyInput();
  5. // }
  6. /* If interrupts are not enabled, previous key status and current key status
  7. * are compared to find out if a key has changed status.
  8. */
  9. // if (!Hal_KeyIntEnable)
  10. // {
  11. // if (keys == halKeySavedKeys)
  12. // {
  13. /* Exit - since no keys have changed */
  14. // return;
  15. // }
  16. /* Store the current keys for comparation next time */
  17. // halKeySavedKeys = keys;
  18. // }
  19. // else
  20. // {
  21. /* Key interrupt handled here */
  22. // }

从Zigbee协议栈底层添加自己的按键配置 - 图5
全部注释掉,因为它会对我们设定的按键产生干扰,具体情况我也不知道…
然后再在内模仿:

  1. if (HAL_PUSH_BUTTON1())
  2. {
  3. keys |= HAL_KEY_SW_6;
  4. }

添加 :

  1. if (HAL_PUSH_BUTTON8())
  2. {
  3. keys |= HAL_KEY_SW_8;
  4. }

从Zigbee协议栈底层添加自己的按键配置 - 图6

OnBard.c

  1. void InitBoard( uint8 level )
  2. {
  3. if ( level == OB_COLD )
  4. {
  5. // IAR does not zero-out this byte below the XSTACK.
  6. *(uint8 *)0x0 = 0;
  7. // Interrupts off
  8. osal_int_disable( INTS_ALL );
  9. // Check for Brown-Out reset
  10. ChkReset();
  11. }
  12. else // !OB_COLD
  13. {
  14. /* Initialize Key stuff */
  15. HalKeyConfig(HAL_KEY_INTERRUPT_DISABLE, OnBoard_KeyCallback); //修改此处
  16. }
  17. }

改为:

  1. HalKeyConfig(HAL_KEY_INTERRUPT_ENABLE, OnBoard_KeyCallback);
  2. 记得在任务初始化函数中加入
  3. RegisterForKeys( GenericApp_TaskID ); //注册按键事件

从Zigbee协议栈底层添加自己的按键配置 - 图7

最后再在Coordinator.c中的

  1. uint16 GenericApp_ProcessEvent( uint8 task_id, uint16 events )
  2. 添加事件及其处理函数
  3. case KEY_CHANGE:
  4. GenericApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );

再在

  1. static void GenericApp_HandleKeys( uint8 shift, uint8 keys )
  2. {
  3. zAddrType_t dstAddr;
  4. if ( keys & HAL_KEY_SW_1 )
  5. {
  6. }
  7. if ( keys & HAL_KEY_SW_2 )
  8. {
  9. }
  10. if ( keys & HAL_KEY_SW_3 )
  11. {
  12. }
  13. if ( keys & HAL_KEY_SW_4 )
  14. {
  15. }
  16. if ( keys & HAL_KEY_SW_8 ) //添加自己的按键及其处理函数
  17. {
  18. HalLedSet(HAL_LED_1, HAL_LED_MODE_FLASH);
  19. }
  20. }

最后烧到开发板中即可,祝大家实验成功。


qrcode_for_gh_e95b474fcf08_344.jpg