微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 无线和射频 > 射频无线通信设计 > CC2541的按键产生消息的教程

CC2541的按键产生消息的教程

时间:10-02 整理:3721RD 点击:

CC2541的按键产生消息的教程


转载:http:// blog.csdn.net
最近在调试 CC2541 的按键消息,查看了好久才找到如何产生消息。
1. 首先当然将配置 I\O 配置成按键中断,然后当按键按下时,会进入中断处理函数
[html] view plain copy
HAL_ISR_FUNCTION( halKeyPort2Isr, P2INT_VECTOR )//进入中断  
{  
  HAL_ENTER_ISR();  
  if (HAL_KEY_JOY_MOVE_PXIFG & HAL_KEY_JOY_MOVE_BIT)  
  {  
    halProcessKeyInterrupt();  
  }  
  /*  
    Clear the CPU interrupt flag for Port_2  
    PxIFG has to be cleared before PxIF  
    Notes: P2_1 and P2_2 are debug lines.  
  */  
  HAL_KEY_JOY_MOVE_PXIFG = 0;  
  HAL_KEY_CPU_PORT_2_IF = 0;  
  CLEAR_SLEEP_MODE();  
  HAL_EXIT_ISR();  
[html] view plain copy
void halProcessKeyInterrupt (void)//中断处理函数  
{  
  bool valid=FALSE;  
#if defined ( CC2540_MINIDK )  
  if( HAL_KEY_SW_1_PXIFG & HAL_KEY_SW_1_BIT) /* Interrupt Flag has been set by SW1 */  
  {  
    HAL_KEY_SW_1_PXIFG = ~(HAL_KEY_SW_1_BIT); /* Clear Interrupt Flag */  
    valid = TRUE;  
  }  
  if (HAL_KEY_SW_2_PXIFG & HAL_KEY_SW_2_BIT)  /* Interrupt Flag has been set by SW2 */  
  {  
    HAL_KEY_SW_2_PXIFG = ~(HAL_KEY_SW_2_BIT); /* Clear Interrupt Flag */  
    valid = TRUE;  
  }  
#else  
  if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT)  /* Interrupt Flag has been set */  
  {  
    HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT); /* Clear Interrupt Flag */  
    valid = TRUE;  
  }  
  if (HAL_KEY_JOY_MOVE_PXIFG & HAL_KEY_JOY_MOVE_BIT)  /* Interrupt Flag has been set */  
  {  
    HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_BIT); /* Clear Interrupt Flag */  
    valid = TRUE;  
  }  
#endif  
  if (u8_wechatWriteCharFlag == UNPACK_CONTINUOUS)  
  {  
  #ifdef UART_DEBUG  
    print_msg("halProcessKeyInterrupt return \r\n" );  
  #endif  
    return;  
  }  
  if (valid)  
  {  
    osal_start_timerEx (Hal_TaskID, HAL_KEY_EVENT, HAL_KEY_DEBOUNCE_VALUE);  
  }  
}  
2. 上面程序最后会进入 Hal_TaskID 任务的 HAL_KEY_EVENT
[html] view plain copy
if (events & HAL_KEY_EVENT)  
  {  
#if (defined HAL_KEY) && (HAL_KEY == TRUE)  
    /* Check for keys */  
    HalKeyPoll();  //处理按键  
    /* if interrupt disabled, do next polling */  
    if (!Hal_KeyIntEnable)  
    {  
      osal_start_timerEx( Hal_TaskID, HAL_KEY_EVENT, 100);  
    }  
#endif  
    return events ^ HAL_KEY_EVENT;  
  }  
3. 在 HalKeyPoll 函数中,会把按键产生的消息赋值给 keys 等参数,最后传递给 pHalKeyProcessFunction,并产生消息
[html] view plain copy
void HalKeyPoll (void)  
{  
  uint8 keys = 0;  
  uint8 notify = 0;  
  if (u8_wechatWriteCharFlag == UNPACK_CONTINUOUS)  
  {  
    #ifdef UART_DEBUG  
    print_msg("HalKeyPoll return \r\n" );  
    #endif  
    return;  
  }  
#if defined (CC2540_MINIDK)  
  if (!(HAL_KEY_SW_1_PORT & HAL_KEY_SW_1_BIT))    /* Key is active low */  
  {  
    keys |= HAL_KEY_SW_1;  
  }  
  if (!(HAL_KEY_SW_2_PORT & HAL_KEY_SW_2_BIT))    /* Key is active low */  
  {  
    keys |= HAL_KEY_SW_2;  
  }  
#else  
  if (!(HAL_KEY_SW_6_PORT & HAL_KEY_SW_6_BIT))    /* Key is active low */  
  {  
    keys |= HAL_KEY_SW_6;  
  }  
  if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT))  /* Key is active HIGH */  
  {  
    keys = halGetJoyKeyInput();  
  }  
#endif  
  /* 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;  
    }  
    else  
    {  
      notify = 1;  
    }  
  }  
  else  
  {  
    /* Key interrupt handled here */  
    if (keys)  
    {  
      notify = 1;  
    }  
  }  
  /* Store the current keys for comparation next time */  
  halKeySavedKeys = keys;  
  /* Invoke Callback if new keys were depressed */  
  if (notify && (pHalKeyProcessFunction))   
  {  
    (pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL); //产生消息  
  }  
}  
4. 在产生消息之前,先将回调函数设置完成
[html] view plain copy
HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);   
[html] view plain copy
void HalKeyConfig (bool interruptEnable, halKeyCBack_t cback)  
{  
  /* Enable/Disable Interrupt or */  
  Hal_KeyIntEnable = interruptEnable;  
  /* Register the callback fucntion */  
  pHalKeyProcessFunction = cback;  <span style="font-family: Arial, Helvetica, sans-serif;">// </span><span style="font-family: Arial, Helvetica, sans-serif;">pHalKeyProcessFunction  == </span><span style="font-family: Arial, Helvetica, sans-serif;">OnBoard_KeyCallback</span>  
  /* Determine if interrupt is enable or not */  
  if (Hal_KeyIntEnable)  
  {  
#if defined ( CC2540_MINIDK )  
    HAL_KEY_SW_1_ICTL |= HAL_KEY_SW_1_ICTLBIT; /* enable interrupt generation at port */  
    HAL_KEY_SW_1_PXIFG = ~(HAL_KEY_SW_1_BIT);  /* Clear any pending interrupt */  
    HAL_KEY_SW_2_ICTL |= HAL_KEY_SW_2_ICTLBIT; /* enable interrupt generation at port */  
    HAL_KEY_SW_2_PXIFG = ~(HAL_KEY_SW_2_BIT);  /* Clear any pending interrupt */  
#else  
    /* Rising/Falling edge configuratinn */  
    PICTL &= ~(HAL_KEY_SW_6_EDGEBIT);    /* Clear the edge bit */  
    /* For falling edge, the bit must be set. */  
  #if (HAL_KEY_SW_6_EDGE == HAL_KEY_FALLING_EDGE)  
    PICTL |= HAL_KEY_SW_6_EDGEBIT;  
  #endif  
    /* Interrupt configuration:  
     * - Enable interrupt generation at the port  
     * - Enable CPU interrupt  
     * - Clear any pending interrupt  
     */  
    HAL_KEY_SW_6_ICTL |= HAL_KEY_SW_6_ICTLBIT;  
    HAL_KEY_SW_6_IEN |= HAL_KEY_SW_6_IENBIT;  
    HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT);  
    /* Rising/Falling edge configuratinn */  
    HAL_KEY_JOY_MOVE_ICTL &= ~(HAL_KEY_JOY_MOVE_EDGEBIT);    /* Clear the edge bit */  
    /* For falling edge, the bit must be set. */  
  #if (HAL_KEY_JOY_MOVE_EDGE == HAL_KEY_FALLING_EDGE)  
    HAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_EDGEBIT;  
  #endif  
    /* Interrupt configuration:  
     * - Enable interrupt generation at the port  
     * - Enable CPU interrupt  
     * - Clear any pending interrupt  
     */  
    HAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_ICTLBIT;  
    HAL_KEY_JOY_MOVE_IEN |= HAL_KEY_JOY_MOVE_IENBIT;  
    HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_BIT);  
#endif // !CC2540_MINIDK  
    /* Do this only after the hal_key is configured - to work with sleep stuff */  
    if (HalKeyConfigured == TRUE)  
    {  
      osal_stop_timerEx(Hal_TaskID, HAL_KEY_EVENT);  /* Cancel polling if active */  
    }  
  }  
  else    /* Interrupts NOT enabled */  
  {  
#if defined ( CC2540_MINIDK )  
    HAL_KEY_SW_1_ICTL &= ~(HAL_KEY_SW_1_ICTLBIT); /* don't generate interrupt */  
    HAL_KEY_SW_2_ICTL &= ~(HAL_KEY_SW_2_ICTLBIT); /* don't generate interrupt */  
#else  
    HAL_KEY_SW_6_ICTL &= ~(HAL_KEY_SW_6_ICTLBIT); /* don't generate interrupt */  
    HAL_KEY_SW_6_IEN &= ~(HAL_KEY_SW_6_IENBIT);   /* Clear interrupt enable bit */  
#endif  // !CC2540_MINIDK  
    osal_set_event(Hal_TaskID, HAL_KEY_EVENT);  
  }  
  /* Key now is configured */  
  HalKeyConfigured = TRUE;  
}  
[html] view plain copy
void OnBoard_KeyCallback ( uint8 keys, uint8 state )  //产生消息  
{  
  uint8 shift;  
  (void)state;  
    #ifdef UART_DEBUG  
    print_msg("OnBoard_KeyCallback \r\n" );  
    #endif  
  if (u8_wechatWriteCharFlag == UNPACK_CONTINUOUS  ||  
    ((SwitchBackgroud == EEVO_enterBackground) || (SwitchBackgroud == EEVO_sleep)))  
  {  
    #ifdef UART_DEBUG  
    print_msg("KeyCallback return \r\n" );  
    #endif  
    return;  
  }  
  // shift key (S1) is used to generate key interrupt  
  // applications should not use S1 when key interrupt is enabled  
  shift = (OnboardKeyIntEnable == HAL_KEY_INTERRUPT_ENABLE) ? false : ((keys & HAL_KEY_SW_6) ? true : false);  
  if ( OnBoard_SendKeys( keys, shift ) != SUCCESS )  
  {  
    // Process SW1 here  
    if ( keys & HAL_KEY_SW_1 )  // Switch 1  
    {  
    }  
    // Process SW2 here  
    if ( keys & HAL_KEY_SW_2 )  // Switch 2  
    {  
    }  
    // Process SW3 here  
    if ( keys & HAL_KEY_SW_3 )  // Switch 3  
    {  
    }  
    // Process SW4 here  
    if ( keys & HAL_KEY_SW_4 )  // Switch 4  
    {  
    }  
    // Process SW5 here  
    if ( keys & HAL_KEY_SW_5 )  // Switch 5  
    {  
    }  
    // Process SW6 here  
    if ( keys & HAL_KEY_SW_6 )  // Switch 6  
    {  
    }  
  }  
  /* If any key is currently pressed down and interrupt  
     is still enabled, disable interrupt and switch to polling */  
  if( keys != 0 )  
  {  
    if( OnboardKeyIntEnable == HAL_KEY_INTERRUPT_ENABLE )  
    {  
      OnboardKeyIntEnable = HAL_KEY_INTERRUPT_DISABLE;  
      HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);  
    }  
  }  
  /* If no key is currently pressed down and interrupt  
     is disabled, enable interrupt and turn off polling */  
  else  
  {  
    if( OnboardKeyIntEnable == HAL_KEY_INTERRUPT_DISABLE )  
    {  
      OnboardKeyIntEnable = HAL_KEY_INTERRUPT_ENABLE;  
      HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);  
    }  
  }  

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top