1.按键初始化函数
HalKeyInit()在HalDriverInit()函数中
HalDriverInit()函数位于hal_drivers.c中
HalKeyInit()函数位于hal_key.c中
配置P0SEL功能选择寄存器、P0DIR方向寄存器
/**************************************************************************************************
* @fn HalKeyInit
*
* @brief Initilize Key Service
*
* @param none
*
* @return None
**************************************************************************************************/
void HalKeyInit( void )
{
/* Initialize previous key to 0 */
halKeySavedKeys = 0;
HAL_KEY_SW_6_SEL &= ~(HAL_KEY_SW_6_BIT); /* Set pin function to GPIO */
#if ! defined ENABLE_LED4_DISABLE_S1
HAL_KEY_SW_6_DIR &= ~(HAL_KEY_SW_6_BIT); /* Set pin direction to Input */
#endif
HAL_KEY_JOY_MOVE_SEL &= ~(HAL_KEY_JOY_MOVE_BIT); /* Set pin function to GPIO */
HAL_KEY_JOY_MOVE_DIR &= ~(HAL_KEY_JOY_MOVE_BIT); /* Set pin direction to Input */
/* Initialize callback function */
pHalKeyProcessFunction = NULL; //初始化按键回调函数位NULL
/* Start with key is not configured */
HalKeyConfigured = FALSE;
}
1.1按键定义配置
位于hal_borad_cfg.h
按键PUSH1_SBIT的IO口为P0_1、按键PUSH2_SBIT的IO口为P2_0
/* ------------------------------------------------------------------------------------------------
* Push Button Configuration
* ------------------------------------------------------------------------------------------------
*/
#define ACTIVE_LOW !
#define ACTIVE_HIGH !! /* double negation forces result to be '1' */
/* 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
位于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
/* 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
#define HAL_KEY_JOY_MOVE_BIT BV(0)
#define HAL_KEY_JOY_MOVE_SEL P2SEL
#define HAL_KEY_JOY_MOVE_DIR P2DIR
2.初始化按键任务
osal_init_system()→osalInitTasks()→zcl_GenericApp_Init()中调用RegisterForKeys(zcl_GenericApp_TaskID )
按键注册函数RegisterForKeys()位于OnBoard.c中
/*********************************************************************
* Keyboard Register function
*
* The keyboard handler is setup to send all keyboard changes to
* one task (if a task is registered).
*
* If a task registers, it will get all the keys. You can change this
* to register for individual keys.
*********************************************************************/
uint8 RegisterForKeys( uint8 task_id )
{
// Allow only the first task
if ( registeredKeysTaskID == NO_TASK_ID )
{
registeredKeysTaskID = task_id;
return ( true );
}
else
return ( false );
}
3.配置按键
main()→InitBoard( OB_READY )→HalKeyConfig(HAL_KEY_INTERRUPT_DISABLE, OnBoard_KeyCallback)
InitBoard( OB_READY )位于OnBoard.c
配置按键函数HalKeyConfig(HAL_KEY_INTERRUPT_DISABLE, OnBoard_KeyCallback)位于OnBoard.c
配置按键模式:
HAL_KEY_INTERRUPT_DISABLE为轮询模式
HAL_KEY_INTERRUPT_ENABLE为中断模式
/*********************************************************************
* @fn InitBoard()
* @brief Initialize the CC2420DB Board Peripherals
* @param level: COLD,WARM,READY
* @return None
*/
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 */
#if defined (ISR_KEYINTERRUPT)
HalKeyConfig(HAL_KEY_INTERRUPT_ENABLE , OnBoard_KeyCallback);
#else
HalKeyConfig(HAL_KEY_INTERRUPT_DISABLE, OnBoard_KeyCallback);
#endif
}
}
3.1配置按键函数
HalKeyConfig(HAL_KEY_INTERRUPT_ENABLE , OnBoard_KeyCallback)位于hal_key.c
配置中断寄存器
/**************************************************************************************************
* @fn HalKeyConfig
*
* @brief Configure the Key serivce
*
* @param interruptEnable - TRUE/FALSE, enable/disable interrupt
* cback - pointer to the CallBack function
*
* @return None
**************************************************************************************************/
void HalKeyConfig (bool interruptEnable, halKeyCBack_t cback)
{
/* Enable/Disable Interrupt or */
Hal_KeyIntEnable = interruptEnable;
/* Register the callback fucntion */
pHalKeyProcessFunction = cback; //指定按键回调函数为OnBoard_KeyCallback
/* Determine if interrupt is enable or not */
if (Hal_KeyIntEnable) //中断设置
{
/* Rising/Falling edge configuratinn */
PICTL &= ~(HAL_KEY_SW_6_EDGEBIT)