分析一下代码。
1、 初始化IO为输入,且初始一些全局变量
void HalKeyInit( void )
{
halKeySavedKeys = 0; // Initialize previous key to 0.
P0SEL &= ~(BV(1)); /* Set pin function to GPIO */
P0DIR &= ~(BV(1)); /* Set pin direction to Input */
/* Initialize callback function */
pHalKeyProcessFunction = NULL;
/* Start with key is not configured */
HalKeyConfigured = FALSE;
}
2、 开启中断
//如果打开中断,就不开启定时器,否则开启定时器
void HalKeyConfig (bool interruptEnable, halKeyCBack_t cback)
{
/* Enable/Disable Interrupt or */
Hal_KeyIntEnable = interruptEnable;
/* Register the callback fucntion */
pHalKeyProcessFunction = cback;
/* Determine if interrupt is enable or not */
if (Hal_KeyIntEnable)
{
/* Rising/Falling edge configuratinn */
PICTL |= BV(0); //bit0:设置PO端口的中断触发模式 0: Rising edge on input gives interrupt. 1: Falling edge on input gives interrupt.
P0IEN |= BV(1); /* enable interrupt generation at port */
IEN1 |= BV(5); /* enable CPU interrupt */
P0IFG = ~(BV(1)); /* Clear any pending interrupt */
/* 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 */
{
P0IEN &= ~(BV(1)); /* don't generate interrupt */
IEN1 &= ~(BV(5)); /* Clear interrupt enable bit */
osal_set_event(Hal_TaskID, HAL_KEY_EVENT);
}
/* Key now is configured */
HalKeyConfigured = TRUE;
}
3、IO中断产生,延时任务,去键抖动。
4、 事件的处理地方
5、 回调函数的分析
6、 发送事件