zigbee 协议栈 按键驱动

来源:互联网 发布:淘宝退货被拒收怎么办 编辑:程序博客网 时间:2024/05/21 17:58

1、在hal_key.h中增加下面代码,其中第一、二个WDS--是我修改原有的,最后一个WDS--是我增加的。如果增加自己的按键则,像我最后一个WDS-- 增加一样就好了,上面两个WDS--可以不理

/* Switches (keys) */
#define HAL_KEY_SW_1 0x12  // WDS--
#define HAL_KEY_SW_2 0x02  // Joystick right
#define HAL_KEY_SW_5 0x04  // Joystick center
#define HAL_KEY_SW_4 0x08  // Joystick left
#define HAL_KEY_SW_3 0x10  // Joystick down
#define HAL_KEY_SW_6 0x11  // WDS--
#define HAL_KEY_SW_7 0x40  // Button S2 if available
 
#define HAL_KEY3   0x13  //WDS--


2、在hal_board_cfg.h中增加以下代码,如WDS-- ADD 是我增加的按键

/* S1 */
#define PUSH1_BV          BV(1)
#define PUSH1_SBIT        P0_1


#if defined (HAL_BOARD_CC2530EB_REV17)
  #define PUSH1_POLARITY    ACTIVE_HIGH
#elif defined (HAL_BOARD_CC2530EB_REV13)
  #define PUSH1_POLARITY    ACTIVE_LOW
#else
  #error Unknown Board Indentifier
#endif

/* Joystick Center Press */
#define PUSH2_BV          BV(0)
#define PUSH2_SBIT        P2_0
#define PUSH2_POLARITY    ACTIVE_HIGH


//WDS-- ADD
//P0.4
#define PUSH5_BV          BV(4)
#define PUSH5_SBIT        P0_4
#if defined (HAL_BOARD_CC2530EB_REV17)
  #define PUSH5_POLARITY    ACTIVE_HIGH
#elif defined (HAL_BOARD_CC2530EB_REV13)
  #define PUSH5_POLARITY    ACTIVE_LOW
#else
  #error Unknown Board Indentifier
#endif


3、在hal_board_cfg.h中增加以下代码,如WDS-- ADD 是我增加的

/* ----------- 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)


//WDS-- ADD
#define HAL_PUSH_BUTTON5()        (PUSH5_POLARITY (PUSH5_SBIT))


4、在hal_key.c中增加以下代码,如WDS-- ADD 是我增加的

#define HAL_KEY_RISING_EDGE   0
#define HAL_KEY_FALLING_EDGE  1


#define HAL_KEY_DEBOUNCE_VALUE  25
#define HAL_KEY_POLLING_VALUE   100


/* CPU port interrupt */
#define HAL_KEY_CPU_PORT_0_IF P0IF//WDS-- 端口0中断标志
#define HAL_KEY_CPU_PORT_2_IF P2IF//WDS-- 端口2中断标志


/* SW_6 is at P0.1 */
#define HAL_KEY_SW_6_PORT   P0  //WDS-- 端口P0
#define HAL_KEY_SW_6_BIT    BV(1)//按WDS-- 键所在IO口为P0_1
#define HAL_KEY_SW_6_SEL    P0SEL//WDS-- 普通IO
#define HAL_KEY_SW_6_DIR    P0DIR//WDS-- 方向输入


/* edge interrupt */
#define HAL_KEY_SW_6_EDGEBIT  BV(0)
#define HAL_KEY_SW_6_EDGE     HAL_KEY_FALLING_EDGE//WDS--  中断边沿触发 PICTL寄存器




/* 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 */


/* Joy stick move at P2.0 */
#define HAL_KEY_JOY_MOVE_PORT   P2 //WDS-- 端口P0
#define HAL_KEY_JOY_MOVE_BIT    BV(0)//WDS-- 按键所在IO口为P2_o
#define HAL_KEY_JOY_MOVE_SEL    P2SEL//WDS-- 普通IO
#define HAL_KEY_JOY_MOVE_DIR    P2DIR//WDS-- 方向输入


/* edge interrupt */
#define HAL_KEY_JOY_MOVE_EDGEBIT  BV(3)
#define HAL_KEY_JOY_MOVE_EDGE     HAL_KEY_FALLING_EDGE


/* Joy move interrupts */
#define HAL_KEY_JOY_MOVE_IEN      IEN2  /* CPU interrupt mask register */
#define HAL_KEY_JOY_MOVE_IENBIT   BV(1) /* Mask bit for all of Port_2 */
#define HAL_KEY_JOY_MOVE_ICTL     P2IEN /* Port Interrupt Control register */
#define HAL_KEY_JOY_MOVE_ICTLBIT  BV(0) /* P2IENL - P2.0<->P2.3 enable/disable bit */
#define HAL_KEY_JOY_MOVE_PXIFG    P2IFG /* Interrupt flag at source */


//WDS-- ADD --BUTTON
//P0.4
#define HAL_KEY1_PORT   P0  //WDS-- 端口P0
#define HAL_KEY1_BIT    BV(4)//按WDS-- 键所在IO口为P0_4
#define HAL_KEY1_SEL    P0SEL//WDS-- 普通IO
#define HAL_KEY1_DIR    P0DIR//WDS-- 方向输入
/* edge interrupt */
#define HAL_KEY1_EDGEBIT  BV(0)//WDS-- 什么意思 ???
#define HAL_KEY1_EDGE     HAL_KEY_FALLING_EDGE//WDS--  中断边沿触发 PICTL寄存器
/* SW_6 interrupts */
#define HAL_KEY1_IEN      IEN1  /* CPU interrupt mask register */
#define HAL_KEY1_IENBIT   BV(5) /* Mask bit for all of Port_0 *///WDS-- 什么意思 ???
#define HAL_KEY1_ICTL     P0IEN /* Port Interrupt Control register */
#define HAL_KEY1_ICTLBIT  BV(4) /* P0IEN - P0.4 enable/disable bit */
#define HAL_KEY1_PXIFG    P0IFG /* Interrupt flag at source */


5、在hal_key.c中增加以下代码,如WDS-- ADD 是我增加的,可把原有的屏蔽了


   if(HAL_PUSH_BUTTON5())//WDS--
  {
   keys |= HAL_KEY3; 
  }

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);
    }
  }

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





0 0
原创粉丝点击