CC2541的按键

转自:http://blog.csdn.net/haozi0_0/article/details/48178213


最近在调试 CC2541 的按键消息,查看了好久才找到如何产生消息。

1. 首先当然将配置 I\O 配置成按键中断,然后当按键按下时,会进入中断处理函数

[html]  view plain copy
  1. HAL_ISR_FUNCTION( halKeyPort2Isr, P2INT_VECTOR )//进入中断  
  2. {  
  3.   HAL_ENTER_ISR();  
  4.   
  5.   if (HAL_KEY_JOY_MOVE_PXIFG & HAL_KEY_JOY_MOVE_BIT)  
  6.   {  
  7.     halProcessKeyInterrupt();  
  8.   }  
  9.   
  10.   /*  
  11.     Clear the CPU interrupt flag for Port_2  
  12.     PxIFG has to be cleared before PxIF  
  13.     Notes: P2_1 and P2_2 are debug lines.  
  14.   */  
  15.   HAL_KEY_JOY_MOVE_PXIFG = 0;  
  16.   HAL_KEY_CPU_PORT_2_IF = 0;  
  17.   
  18.   CLEAR_SLEEP_MODE();  
  19.   
  20.   HAL_EXIT_ISR();  
  21.   
  22.    
[html]  view plain copy
  1. void halProcessKeyInterrupt (void)//中断处理函数  
  2. {  
  3.   bool valid=FALSE;  
  4.   
  5. #if defined ( CC2540_MINIDK )  
  6.   if( HAL_KEY_SW_1_PXIFG & HAL_KEY_SW_1_BIT) /* Interrupt Flag has been set by SW1 */  
  7.   {  
  8.     HAL_KEY_SW_1_PXIFG = ~(HAL_KEY_SW_1_BIT); /* Clear Interrupt Flag */  
  9.     valid = TRUE;  
  10.   }  
  11.   
  12.   if (HAL_KEY_SW_2_PXIFG & HAL_KEY_SW_2_BIT)  /* Interrupt Flag has been set by SW2 */  
  13.   {  
  14.     HAL_KEY_SW_2_PXIFG = ~(HAL_KEY_SW_2_BIT); /* Clear Interrupt Flag */  
  15.     valid = TRUE;  
  16.   }  
  17. #else  
  18.   if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT)  /* Interrupt Flag has been set */  
  19.   {  
  20.     HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT); /* Clear Interrupt Flag */  
  21.     valid = TRUE;  
  22.   }  
  23.   
  24.   if (HAL_KEY_JOY_MOVE_PXIFG & HAL_KEY_JOY_MOVE_BIT)  /* Interrupt Flag has been set */  
  25.   {  
  26.     HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_BIT); /* Clear Interrupt Flag */  
  27.     valid = TRUE;  
  28.   }  
  29. #endif  
  30.   if (u8_wechatWriteCharFlag == UNPACK_CONTINUOUS)  
  31.   {  
  32.   #ifdef UART_DEBUG  
  33.     print_msg("halProcessKeyInterrupt return \r\n" );  
  34.   #endif  
  35.     return;  
  36.   }  
  37.   
  38.   
  39.   if (valid)  
  40.   {  
  41.     osal_start_timerEx (Hal_TaskID, HAL_KEY_EVENT, HAL_KEY_DEBOUNCE_VALUE);  
  42.   }  
  43. }  

2. 上面程序最后会进入 Hal_TaskID 任务的 HAL_KEY_EVENT

[html]  view plain copy
  1. if (events & HAL_KEY_EVENT)  
  2.   {  
  3. #if (defined HAL_KEY) && (HAL_KEY == TRUE)  
  4.     /* Check for keys */  
  5.     HalKeyPoll();  //处理按键  
  6.   
  7.     /* if interrupt disabled, do next polling */  
  8.     if (!Hal_KeyIntEnable)  
  9.     {  
  10.       osal_start_timerEx( Hal_TaskID, HAL_KEY_EVENT, 100);  
  11.     }  
  12. #endif  
  13.     return events ^ HAL_KEY_EVENT;  
  14.   }  

3. 在 HalKeyPoll 函数中,会把按键产生的消息赋值给 keys 等参数,最后传递给 pHalKeyProcessFunction,并产生消息

[html]  view plain copy
  1. void HalKeyPoll (void)  
  2. {  
  3.   uint8 keys = 0;  
  4.   uint8 notify = 0;  
  5.   if (u8_wechatWriteCharFlag == UNPACK_CONTINUOUS)  
  6.   {  
  7.     #ifdef UART_DEBUG  
  8.     print_msg("HalKeyPoll return \r\n" );  
  9.     #endif  
  10.     return;  
  11.   }  
  12. #if defined (CC2540_MINIDK)  
  13.   if (!(HAL_KEY_SW_1_PORT & HAL_KEY_SW_1_BIT))    /* Key is active low */  
  14.   {  
  15.     keys |= HAL_KEY_SW_1;  
  16.   }  
  17.   if (!(HAL_KEY_SW_2_PORT & HAL_KEY_SW_2_BIT))    /* Key is active low */  
  18.   {  
  19.     keys |= HAL_KEY_SW_2;  
  20.   }  
  21. #else  
  22.   if (!(HAL_KEY_SW_6_PORT & HAL_KEY_SW_6_BIT))    /* Key is active low */  
  23.   {  
  24.     keys |= HAL_KEY_SW_6;  
  25.   }  
  26.   
  27.   if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT))  /* Key is active HIGH */  
  28.   {  
  29.     keys = halGetJoyKeyInput();  
  30.   }  
  31. #endif  
  32.   
  33.   /* If interrupts are not enabled, previous key status and current key status  
  34.    * are compared to find out if a key has changed status.  
  35.    */  
  36.   if (!Hal_KeyIntEnable)  
  37.   {  
  38.     if (keys == halKeySavedKeys)  
  39.     {  
  40.       /* Exit - since no keys have changed */  
  41.       return;  
  42.     }  
  43.     else  
  44.     {  
  45.       notify = 1;  
  46.     }  
  47.   }  
  48.   else  
  49.   {  
  50.     /* Key interrupt handled here */  
  51.     if (keys)  
  52.     {  
  53.       notify = 1;  
  54.     }  
  55.   }  
  56.   
  57.   /* Store the current keys for comparation next time */  
  58.   halKeySavedKeys = keys;  
  59.   
  60.   /* Invoke Callback if new keys were depressed */  
  61.   if (notify && (pHalKeyProcessFunction))   
  62.   {  
  63.     (pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL); //产生消息  
  64.   
  65.   }  
  66. }  
4. 在产生消息之前,先将回调函数设置完成

[html]  view plain copy
  1. HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);   
[html]  view plain copy
  1. void HalKeyConfig (bool interruptEnable, halKeyCBack_t cback)  
  2. {  
  3.   /* Enable/Disable Interrupt or */  
  4.   Hal_KeyIntEnable = interruptEnable;  
  5.   
  6.   /* Register the callback fucntion */  
  7.   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>  
  8.   
  9.   /* Determine if interrupt is enable or not */  
  10.   if (Hal_KeyIntEnable)  
  11.   {  
  12. #if defined ( CC2540_MINIDK )  
  13.     HAL_KEY_SW_1_ICTL |= HAL_KEY_SW_1_ICTLBIT; /* enable interrupt generation at port */  
  14.     HAL_KEY_SW_1_PXIFG = ~(HAL_KEY_SW_1_BIT);  /* Clear any pending interrupt */  
  15.     HAL_KEY_SW_2_ICTL |= HAL_KEY_SW_2_ICTLBIT; /* enable interrupt generation at port */  
  16.     HAL_KEY_SW_2_PXIFG = ~(HAL_KEY_SW_2_BIT);  /* Clear any pending interrupt */  
  17.   
  18. #else  
  19.     /* Rising/Falling edge configuratinn */  
  20.     PICTL &= ~(HAL_KEY_SW_6_EDGEBIT);    /* Clear the edge bit */  
  21.     /* For falling edge, the bit must be set. */  
  22.   #if (HAL_KEY_SW_6_EDGE == HAL_KEY_FALLING_EDGE)  
  23.     PICTL |= HAL_KEY_SW_6_EDGEBIT;  
  24.   #endif  
  25.   
  26.   
  27.     /* Interrupt configuration:  
  28.      * - Enable interrupt generation at the port  
  29.      * - Enable CPU interrupt  
  30.      * - Clear any pending interrupt  
  31.      */  
  32.     HAL_KEY_SW_6_ICTL |= HAL_KEY_SW_6_ICTLBIT;  
  33.     HAL_KEY_SW_6_IEN |= HAL_KEY_SW_6_IENBIT;  
  34.     HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT);  
  35.   
  36.     /* Rising/Falling edge configuratinn */  
  37.     HAL_KEY_JOY_MOVE_ICTL &= ~(HAL_KEY_JOY_MOVE_EDGEBIT);    /* Clear the edge bit */  
  38.     /* For falling edge, the bit must be set. */  
  39.   #if (HAL_KEY_JOY_MOVE_EDGE == HAL_KEY_FALLING_EDGE)  
  40.     HAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_EDGEBIT;  
  41.   #endif  
  42.   
  43.   
  44.     /* Interrupt configuration:  
  45.      * - Enable interrupt generation at the port  
  46.      * - Enable CPU interrupt  
  47.      * - Clear any pending interrupt  
  48.      */  
  49.     HAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_ICTLBIT;  
  50.     HAL_KEY_JOY_MOVE_IEN |= HAL_KEY_JOY_MOVE_IENBIT;  
  51.     HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_BIT);  
  52. #endif // !CC2540_MINIDK  
  53.   
  54.     /* Do this only after the hal_key is configured - to work with sleep stuff */  
  55.     if (HalKeyConfigured == TRUE)  
  56.     {  
  57.       osal_stop_timerEx(Hal_TaskID, HAL_KEY_EVENT);  /* Cancel polling if active */  
  58.     }  
  59.   }  
  60.   else    /* Interrupts NOT enabled */  
  61.   {  
  62. #if defined ( CC2540_MINIDK )  
  63.     HAL_KEY_SW_1_ICTL &= ~(HAL_KEY_SW_1_ICTLBIT); /* don't generate interrupt */  
  64.     HAL_KEY_SW_2_ICTL &= ~(HAL_KEY_SW_2_ICTLBIT); /* don't generate interrupt */  
  65. #else  
  66.     HAL_KEY_SW_6_ICTL &= ~(HAL_KEY_SW_6_ICTLBIT); /* don't generate interrupt */  
  67.     HAL_KEY_SW_6_IEN &= ~(HAL_KEY_SW_6_IENBIT);   /* Clear interrupt enable bit */  
  68. #endif  // !CC2540_MINIDK  
  69.   
  70.     osal_set_event(Hal_TaskID, HAL_KEY_EVENT);  
  71.   }  
  72.   
  73.   /* Key now is configured */  
  74.   HalKeyConfigured = TRUE;  
  75. }  

[html]  view plain copy
  1. void OnBoard_KeyCallback ( uint8 keys, uint8 state )  //产生消息  
  2. {  
  3.   uint8 shift;  
  4.   (void)state;  
  5.     #ifdef UART_DEBUG  
  6.     print_msg("OnBoard_KeyCallback \r\n" );  
  7.     #endif  
  8.   if (u8_wechatWriteCharFlag == UNPACK_CONTINUOUS  ||  
  9.     ((SwitchBackgroud == EEVO_enterBackground) || (SwitchBackgroud == EEVO_sleep)))  
  10.   {  
  11.     #ifdef UART_DEBUG  
  12.     print_msg("KeyCallback return \r\n" );  
  13.     #endif  
  14.   
  15.     return;  
  16.   }  
  17.   // shift key (S1) is used to generate key interrupt  
  18.   // applications should not use S1 when key interrupt is enabled  
  19.   shift = (OnboardKeyIntEnable == HAL_KEY_INTERRUPT_ENABLE) ? false : ((keys & HAL_KEY_SW_6) ? true : false);  
  20.   
  21.   if ( OnBoard_SendKeys( keys, shift ) != SUCCESS )  
  22.   {  
  23.     // Process SW1 here  
  24.     if ( keys & HAL_KEY_SW_1 )  // Switch 1  
  25.     {  
  26.     }  
  27.     // Process SW2 here  
  28.     if ( keys & HAL_KEY_SW_2 )  // Switch 2  
  29.     {  
  30.     }  
  31.     // Process SW3 here  
  32.     if ( keys & HAL_KEY_SW_3 )  // Switch 3  
  33.     {  
  34.     }  
  35.     // Process SW4 here  
  36.     if ( keys & HAL_KEY_SW_4 )  // Switch 4  
  37.     {  
  38.     }  
  39.     // Process SW5 here  
  40.     if ( keys & HAL_KEY_SW_5 )  // Switch 5  
  41.     {  
  42.     }  
  43.     // Process SW6 here  
  44.     if ( keys & HAL_KEY_SW_6 )  // Switch 6  
  45.     {  
  46.     }  
  47.   }  
  48.   
  49.   /* If any key is currently pressed down and interrupt  
  50.      is still enabled, disable interrupt and switch to polling */  
  51.   if( keys != 0 )  
  52.   {  
  53.     if( OnboardKeyIntEnable == HAL_KEY_INTERRUPT_ENABLE )  
  54.     {  
  55.       OnboardKeyIntEnable = HAL_KEY_INTERRUPT_DISABLE;  
  56.       HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);  
  57.     }  
  58.   }  
  59.   /* If no key is currently pressed down and interrupt  
  60.      is disabled, enable interrupt and turn off polling */  
  61.   else  
  62.   {  
  63.     if( OnboardKeyIntEnable == HAL_KEY_INTERRUPT_DISABLE )  
  64.     {  
  65.       OnboardKeyIntEnable = HAL_KEY_INTERRUPT_ENABLE;  
  66.       HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);  
  67.     }  
  68.   }  



最近在调试 CC2541 的按键消息,查看了好久才找到如何产生消息。


1. 首先当然将配置 I\O 配置成按键中断,然后当按键按下时,会进入中断处理函数

[html]  view plain copy
  1. HAL_ISR_FUNCTION( halKeyPort2Isr, P2INT_VECTOR )//进入中断  
  2. {  
  3.   HAL_ENTER_ISR();  
  4.   
  5.   if (HAL_KEY_JOY_MOVE_PXIFG & HAL_KEY_JOY_MOVE_BIT)  
  6.   {  
  7.     halProcessKeyInterrupt();  
  8.   }  
  9.   
  10.   /*  
  11.     Clear the CPU interrupt flag for Port_2  
  12.     PxIFG has to be cleared before PxIF  
  13.     Notes: P2_1 and P2_2 are debug lines.  
  14.   */  
  15.   HAL_KEY_JOY_MOVE_PXIFG = 0;  
  16.   HAL_KEY_CPU_PORT_2_IF = 0;  
  17.   
  18.   CLEAR_SLEEP_MODE();  
  19.   
  20.   HAL_EXIT_ISR();  
  21.   
  22.    
[html]  view plain copy
  1. void halProcessKeyInterrupt (void)//中断处理函数  
  2. {  
  3.   bool valid=FALSE;  
  4.   
  5. #if defined ( CC2540_MINIDK )  
  6.   if( HAL_KEY_SW_1_PXIFG & HAL_KEY_SW_1_BIT) /* Interrupt Flag has been set by SW1 */  
  7.   {  
  8.     HAL_KEY_SW_1_PXIFG = ~(HAL_KEY_SW_1_BIT); /* Clear Interrupt Flag */  
  9.     valid = TRUE;  
  10.   }  
  11.   
  12.   if (HAL_KEY_SW_2_PXIFG & HAL_KEY_SW_2_BIT)  /* Interrupt Flag has been set by SW2 */  
  13.   {  
  14.     HAL_KEY_SW_2_PXIFG = ~(HAL_KEY_SW_2_BIT); /* Clear Interrupt Flag */  
  15.     valid = TRUE;  
  16.   }  
  17. #else  
  18.   if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT)  /* Interrupt Flag has been set */  
  19.   {  
  20.     HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT); /* Clear Interrupt Flag */  
  21.     valid = TRUE;  
  22.   }  
  23.   
  24.   if (HAL_KEY_JOY_MOVE_PXIFG & HAL_KEY_JOY_MOVE_BIT)  /* Interrupt Flag has been set */  
  25.   {  
  26.     HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_BIT); /* Clear Interrupt Flag */  
  27.     valid = TRUE;  
  28.   }  
  29. #endif  
  30.   if (u8_wechatWriteCharFlag == UNPACK_CONTINUOUS)  
  31.   {  
  32.   #ifdef UART_DEBUG  
  33.     print_msg("halProcessKeyInterrupt return \r\n" );  
  34.   #endif  
  35.     return;  
  36.   }  
  37.   
  38.   
  39.   if (valid)  
  40.   {  
  41.     osal_start_timerEx (Hal_TaskID, HAL_KEY_EVENT, HAL_KEY_DEBOUNCE_VALUE);  
  42.   }  
  43. }  

2. 上面程序最后会进入 Hal_TaskID 任务的 HAL_KEY_EVENT

[html]  view plain copy
  1. if (events & HAL_KEY_EVENT)  
  2.   {  
  3. #if (defined HAL_KEY) && (HAL_KEY == TRUE)  
  4.     /* Check for keys */  
  5.     HalKeyPoll();  //处理按键  
  6.   
  7.     /* if interrupt disabled, do next polling */  
  8.     if (!Hal_KeyIntEnable)  
  9.     {  
  10.       osal_start_timerEx( Hal_TaskID, HAL_KEY_EVENT, 100);  
  11.     }  
  12. #endif  
  13.     return events ^ HAL_KEY_EVENT;  
  14.   }  

3. 在 HalKeyPoll 函数中,会把按键产生的消息赋值给 keys 等参数,最后传递给 pHalKeyProcessFunction,并产生消息

[html]  view plain copy
  1. void HalKeyPoll (void)  
  2. {  
  3.   uint8 keys = 0;  
  4.   uint8 notify = 0;  
  5.   if (u8_wechatWriteCharFlag == UNPACK_CONTINUOUS)  
  6.   {  
  7.     #ifdef UART_DEBUG  
  8.     print_msg("HalKeyPoll return \r\n" );  
  9.     #endif  
  10.     return;  
  11.   }  
  12. #if defined (CC2540_MINIDK)  
  13.   if (!(HAL_KEY_SW_1_PORT & HAL_KEY_SW_1_BIT))    /* Key is active low */  
  14.   {  
  15.     keys |= HAL_KEY_SW_1;  
  16.   }  
  17.   if (!(HAL_KEY_SW_2_PORT & HAL_KEY_SW_2_BIT))    /* Key is active low */  
  18.   {  
  19.     keys |= HAL_KEY_SW_2;  
  20.   }  
  21. #else  
  22.   if (!(HAL_KEY_SW_6_PORT & HAL_KEY_SW_6_BIT))    /* Key is active low */  
  23.   {  
  24.     keys |= HAL_KEY_SW_6;  
  25.   }  
  26.   
  27.   if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT))  /* Key is active HIGH */  
  28.   {  
  29.     keys = halGetJoyKeyInput();  
  30.   }  
  31. #endif  
  32.   
  33.   /* If interrupts are not enabled, previous key status and current key status  
  34.    * are compared to find out if a key has changed status.  
  35.    */  
  36.   if (!Hal_KeyIntEnable)  
  37.   {  
  38.     if (keys == halKeySavedKeys)  
  39.     {  
  40.       /* Exit - since no keys have changed */  
  41.       return;  
  42.     }  
  43.     else  
  44.     {  
  45.       notify = 1;  
  46.     }  
  47.   }  
  48.   else  
  49.   {  
  50.     /* Key interrupt handled here */  
  51.     if (keys)  
  52.     {  
  53.       notify = 1;  
  54.     }  
  55.   }  
  56.   
  57.   /* Store the current keys for comparation next time */  
  58.   halKeySavedKeys = keys;  
  59.   
  60.   /* Invoke Callback if new keys were depressed */  
  61.   if (notify && (pHalKeyProcessFunction))   
  62.   {  
  63.     (pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL); //产生消息  
  64.   
  65.   }  
  66. }  
4. 在产生消息之前,先将回调函数设置完成

[html]  view plain copy
  1. HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);   
[html]  view plain copy
  1. void HalKeyConfig (bool interruptEnable, halKeyCBack_t cback)  
  2. {  
  3.   /* Enable/Disable Interrupt or */  
  4.   Hal_KeyIntEnable = interruptEnable;  
  5.   
  6.   /* Register the callback fucntion */  
  7.   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>  
  8.   
  9.   /* Determine if interrupt is enable or not */  
  10.   if (Hal_KeyIntEnable)  
  11.   {  
  12. #if defined ( CC2540_MINIDK )  
  13.     HAL_KEY_SW_1_ICTL |= HAL_KEY_SW_1_ICTLBIT; /* enable interrupt generation at port */  
  14.     HAL_KEY_SW_1_PXIFG = ~(HAL_KEY_SW_1_BIT);  /* Clear any pending interrupt */  
  15.     HAL_KEY_SW_2_ICTL |= HAL_KEY_SW_2_ICTLBIT; /* enable interrupt generation at port */  
  16.     HAL_KEY_SW_2_PXIFG = ~(HAL_KEY_SW_2_BIT);  /* Clear any pending interrupt */  
  17.   
  18. #else  
  19.     /* Rising/Falling edge configuratinn */  
  20.     PICTL &= ~(HAL_KEY_SW_6_EDGEBIT);    /* Clear the edge bit */  
  21.     /* For falling edge, the bit must be set. */  
  22.   #if (HAL_KEY_SW_6_EDGE == HAL_KEY_FALLING_EDGE)  
  23.     PICTL |= HAL_KEY_SW_6_EDGEBIT;  
  24.   #endif  
  25.   
  26.   
  27.     /* Interrupt configuration:  
  28.      * - Enable interrupt generation at the port  
  29.      * - Enable CPU interrupt  
  30.      * - Clear any pending interrupt  
  31.      */  
  32.     HAL_KEY_SW_6_ICTL |= HAL_KEY_SW_6_ICTLBIT;  
  33.     HAL_KEY_SW_6_IEN |= HAL_KEY_SW_6_IENBIT;  
  34.     HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT);  
  35.   
  36.     /* Rising/Falling edge configuratinn */  
  37.     HAL_KEY_JOY_MOVE_ICTL &= ~(HAL_KEY_JOY_MOVE_EDGEBIT);    /* Clear the edge bit */  
  38.     /* For falling edge, the bit must be set. */  
  39.   #if (HAL_KEY_JOY_MOVE_EDGE == HAL_KEY_FALLING_EDGE)  
  40.     HAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_EDGEBIT;  
  41.   #endif  
  42.   
  43.   
  44.     /* Interrupt configuration:  
  45.      * - Enable interrupt generation at the port  
  46.      * - Enable CPU interrupt  
  47.      * - Clear any pending interrupt  
  48.      */  
  49.     HAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_ICTLBIT;  
  50.     HAL_KEY_JOY_MOVE_IEN |= HAL_KEY_JOY_MOVE_IENBIT;  
  51.     HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_BIT);  
  52. #endif // !CC2540_MINIDK  
  53.   
  54.     /* Do this only after the hal_key is configured - to work with sleep stuff */  
  55.     if (HalKeyConfigured == TRUE)  
  56.     {  
  57.       osal_stop_timerEx(Hal_TaskID, HAL_KEY_EVENT);  /* Cancel polling if active */  
  58.     }  
  59.   }  
  60.   else    /* Interrupts NOT enabled */  
  61.   {  
  62. #if defined ( CC2540_MINIDK )  
  63.     HAL_KEY_SW_1_ICTL &= ~(HAL_KEY_SW_1_ICTLBIT); /* don't generate interrupt */  
  64.     HAL_KEY_SW_2_ICTL &= ~(HAL_KEY_SW_2_ICTLBIT); /* don't generate interrupt */  
  65. #else  
  66.     HAL_KEY_SW_6_ICTL &= ~(HAL_KEY_SW_6_ICTLBIT); /* don't generate interrupt */  
  67.     HAL_KEY_SW_6_IEN &= ~(HAL_KEY_SW_6_IENBIT);   /* Clear interrupt enable bit */  
  68. #endif  // !CC2540_MINIDK  
  69.   
  70.     osal_set_event(Hal_TaskID, HAL_KEY_EVENT);  
  71.   }  
  72.   
  73.   /* Key now is configured */  
  74.   HalKeyConfigured = TRUE;  
  75. }  

[html]  view plain copy
  1. void OnBoard_KeyCallback ( uint8 keys, uint8 state )  //产生消息  
  2. {  
  3.   uint8 shift;  
  4.   (void)state;  
  5.     #ifdef UART_DEBUG  
  6.     print_msg("OnBoard_KeyCallback \r\n" );  
  7.     #endif  
  8.   if (u8_wechatWriteCharFlag == UNPACK_CONTINUOUS  ||  
  9.     ((SwitchBackgroud == EEVO_enterBackground) || (SwitchBackgroud == EEVO_sleep)))  
  10.   {  
  11.     #ifdef UART_DEBUG  
  12.     print_msg("KeyCallback return \r\n" );  
  13.     #endif  
  14.   
  15.     return;  
  16.   }  
  17.   // shift key (S1) is used to generate key interrupt  
  18.   // applications should not use S1 when key interrupt is enabled  
  19.   shift = (OnboardKeyIntEnable == HAL_KEY_INTERRUPT_ENABLE) ? false : ((keys & HAL_KEY_SW_6) ? true : false);  
  20.   
  21.   if ( OnBoard_SendKeys( keys, shift ) != SUCCESS )  
  22.   {  
  23.     // Process SW1 here  
  24.     if ( keys & HAL_KEY_SW_1 )  // Switch 1  
  25.     {  
  26.     }  
  27.     // Process SW2 here  
  28.     if ( keys & HAL_KEY_SW_2 )  // Switch 2  
  29.     {  
  30.     }  
  31.     // Process SW3 here  
  32.     if ( keys & HAL_KEY_SW_3 )  // Switch 3  
  33.     {  
  34.     }  
  35.     // Process SW4 here  
  36.     if ( keys & HAL_KEY_SW_4 )  // Switch 4  
  37.     {  
  38.     }  
  39.     // Process SW5 here  
  40.     if ( keys & HAL_KEY_SW_5 )  // Switch 5  
  41.     {  
  42.     }  
  43.     // Process SW6 here  
  44.     if ( keys & HAL_KEY_SW_6 )  // Switch 6  
  45.     {  
  46.     }  
  47.   }  
  48.   
  49.   /* If any key is currently pressed down and interrupt  
  50.      is still enabled, disable interrupt and switch to polling */  
  51.   if( keys != 0 )  
  52.   {  
  53.     if( OnboardKeyIntEnable == HAL_KEY_INTERRUPT_ENABLE )  
  54.     {  
  55.       OnboardKeyIntEnable = HAL_KEY_INTERRUPT_DISABLE;  
  56.       HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);  
  57.     }  
  58.   }  
  59.   /* If no key is currently pressed down and interrupt  
  60.      is disabled, enable interrupt and turn off polling */  
  61.   else  
  62.   {  
  63.     if( OnboardKeyIntEnable == HAL_KEY_INTERRUPT_DISABLE )  
  64.     {  
  65.       OnboardKeyIntEnable = HAL_KEY_INTERRUPT_ENABLE;  
  66.       HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);  
  67.     }  
  68.   }  





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值