您的位置:首页 > 其它

【原创】zstack - 协议栈程序精简日志-IO口外部中断试验

2015-05-23 16:27 393 查看

【原创】zstack - 协议栈程序精简日志-IO口外部中断试验

闲来无事测试zstack协议栈的记录。

说明:本次试验端口P0.6 ,硬件上与按键电路相同,触发电平为低电平,下降沿触发。由于该端口与按键端口同属P0口,硬件电路相同,所以,程序可以完全参考按键程序部分。

本实验程序如下:

hal_key.c中添加如下程序:

/*******************ISR_TEST 外部中断试验**************************************************************/
/* ISR_TEST is at P0.6 ,外部中断试验*/
#define HAL_ISR_TEST_PORT   P0
#define HAL_ISR_TEST_BIT    BV(6)
#define HAL_ISR_TEST_SEL    P0SEL
#define HAL_ISR_TEST_DIR    P0DIR

#define HAL_ISR_TEST_EDGEBIT  BV(0) //bit 0 :Port 0, inputs 7 to 0 interrupt configuration.
#define HAL_ISR_TEST_EDGE     0x01 //HAL_KEY_FALLING_EDGE //下降沿触发

/* ISR_TEST interrupts */
#define HAL_ISR_TEST_IEN      IEN1  /* CPU interrupt mask register ,总中断控制位*/
#define HAL_ISR_TEST_IENBIT   BV(5) /* Mask bit for all of Port_0 */
#define HAL_ISR_TEST_ICTL     P0IEN /* Port Interrupt Control register */
#define HAL_ISR_TEST_ICTLBIT  BV(6) /* P0IEN - P0.6  enable/disable bit */
#define HAL_ISR_TEST_PXIFG    P0IFG /* Interrupt flag at source */
/******************************************************************************************/


函数HalKeyInit添加如下部分:

void HalKeyInit( void )
{
  /* Initialize previous key to 0 */
  halKeySavedKeys = 0;

  HAL_KEY_SW_1_SEL &= ~(HAL_KEY_SW_1_BIT);    /* Set pin function to GPIO */
  HAL_KEY_SW_1_DIR &= ~(HAL_KEY_SW_1_BIT);    /* Set pin direction to Input */

  HAL_KEY_SW_2_SEL &= ~(HAL_KEY_SW_2_BIT);    /* Set pin function to GPIO */
  HAL_KEY_SW_2_DIR &= ~(HAL_KEY_SW_2_BIT);    /* Set pin direction to Input */

  /***************外部中断试验************************************/
  HAL_ISR_TEST_SEL &= ~(HAL_ISR_TEST_BIT);    /* Set pin function to GPIO */
  HAL_ISR_TEST_DIR &= ~(HAL_ISR_TEST_BIT);
  /****************************************************/
  /* Initialize callback function */
  pHalKeyProcessFunction  = NULL;

  /* Start with key is not configured */
  HalKeyConfigured = FALSE;
}
函数HalKeyConfig 修改如下:
void HalKeyConfig (bool interruptEnable, halKeyCBack_t cback)
{
。。。。。
  if (Hal_KeyIntEnable)
  {
    /* Rising/Falling edge configuratinn */

    PICTL &= ~(HAL_KEY_SW_1_EDGEBIT | HAL_KEY_SW_2_EDGEBIT );    /* Clear the edge bit */
    PICTL |= (HAL_KEY_SW_1_EDGE | HAL_KEY_SW_2_EDGE );   /*原程序有问题,只是清除了bit,并没有控制*/
    /* For falling edge, the bit must be set. */
。。。。。。
    HAL_ISR_TEST_ICTL |= HAL_ISR_TEST_ICTLBIT;
    HAL_ISR_TEST_IEN |= HAL_ISR_TEST_IENBIT;
    HAL_ISR_TEST_PXIFG = ~(HAL_ISR_TEST_BIT);
。。。。。。
  }
  else    /* Interrupts NOT enabled */
  {
。。。。。。
    HAL_ISR_TEST_ICTL &= ~(HAL_ISR_TEST_ICTLBIT); /* don't generate interrupt */
    HAL_ISR_TEST_IEN &= ~(HAL_ISR_TEST_IENBIT);   /* Clear interrupt enable bit */
    osal_start_timerEx (Hal_TaskID, HAL_KEY_EVENT, HAL_KEY_POLLING_VALUE);    /* Kick off polling */
  }
。。。。。。
}


P0口中断程序中修改如下:

HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR )
{

  if ((HAL_KEY_SW_1_PXIFG & HAL_KEY_SW_1_BIT) || (HAL_KEY_SW_2_PXIFG & HAL_KEY_SW_2_BIT) )
  {
    halProcessKeyInterrupt();
  }

  if(HAL_ISR_TEST_PXIFG & HAL_ISR_TEST_BIT) {
      HAL_TOGGLE_LED1();
      HAL_ISR_TEST_PXIFG = ~(HAL_ISR_TEST_BIT);
  }
  /*
    Clear the CPU interrupt flag for Port_0
    PxIFG has to be cleared before PxIF
  */
  HAL_KEY_SW_1_PXIFG = 0;
  HAL_KEY_SW_2_PXIFG = 0;//中断标志全部清零
  HAL_KEY_CPU_PORT_0_IF = 0;
}


至此完成。

z-stack 协议栈中断程序说明:

(1)首先要配置控制中断的寄存器,使用IO中断则配置IO中断相关寄存器,使用定时器中断则配置定时器中断相关寄存器。这个根据具体需要没什么好说的,下面以IO口中断为例。

(2)P0口的中断处理例程如下:

HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR )
{
    。。。。。。
}


式中halKeyPort0Isr 表示中断处理函数名,P0INT_VECTOR 就是P0口的中断向量号。定义如下:

#define  P0INT_VECTOR   VECT( 13, 0x6B )   /*  Port 0 Inputs                               */

HAL_ISR_FUNCTION的定义如下:
#define _PRAGMA(x) _Pragma(#x)
#define HAL_ISR_FUNC_DECLARATION(f,v)   _PRAGMA(vector=v) __near_func __interrupt void f(void)
#define HAL_ISR_FUNC_PROTOTYPE(f,v)     _PRAGMA(vector=v) __near_func __interrupt void f(void)
#define HAL_ISR_FUNCTION(f,v)           HAL_ISR_FUNC_PROTOTYPE(f,v); HAL_ISR_FUNC_DECLARATION(f,v)


这么多其实就是下面这句话:

#pragma vector=P0INT_VECTOR
__interrupt void halKeyPort0Isr( void )
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: