This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

关于外部中断采用轮询和中断模式所造成的一个问题

Other Parts Discussed in Thread: Z-STACK

在调试时,发现如果采用轮询方式,中断会被连续的触发,只有加上预定义ISR_KEYINTERRUPT时,中断才是正确的。另外,发现如果采用轮询方式以及某个IO口定义为下拉方式,程序就无法组网了。

  • 采用轮询方式,中断会被连续的触发、照理來說不可能、請問你說的中断会被连续的触发指的是那個函式?

  • 这是我的配置部分:,位于mian函数中
    P0SEL &=0x7F;// 0111 1111 设置P0_7为普通IO口
    P0DIR &=0x7F;// 0111 1111 设置P0_7为输入
    P0IEN |=0x80; //1000 0000 设置P0_7为中断方式

    P0SEL &=0xBF;// 1011 1111设置P0_6为普通IO口
    P0DIR &=0xBF;// 1011 1111设置P0_6为输入
    P0IEN |=0x40; //0100 0000 设置P0_6为中断方式

    P0INP &=0x3F;// 0011 1111 设置P0_6、P0_7为上、下拉输入
    P2INP &=0xDF;// 1101 1111 上拉



    PICTL |=0x01;//0000 0001设置P0口下降沿触发
    P0IFG=0x00;//清中断标志位
    P0IF = 0;
    IEN1 |=0x20; //0010 0000允许P0口中断
    EA=1;//打开CPU总中断

    这是我的中断函数
    #pragma vector=P0INT_VECTOR
    __interrupt void sdfs(void)
    {
    if(P0IFG&0x40)// 0100 0000 P0组的第6位 P0_6引发了外部中断
    {
    MicroWait(5000);//等待起到消抖作用
    if(P0_6==0)//说明确实是P0_6触发了外部中断
    {
    //PCON = 0x00; //清0,让系统进入正常工作模式
    HalUARTWrite(HAL_UART_PORT_0,"0",1);//调试用
    }
    }
    if(P0IFG&0x80)// 1000 0000 P0组的第7位 P0_7引发了外部中断
    {
    MicroWait(5000);//等待起到消抖作用
    if(P0_7==0)//说明确实是P0_7触发了外部中断
    {
    //PCON = 0x00; //清0,让系统进入正常工作模式
    HalUARTWrite(HAL_UART_PORT_0,"7",1);//调试用
    }
    }
    P0IFG = 0;
    P0IF = 0;
    }
    调试时发现,一上电,串口不断收到30,确认只可能来自中断函数
  • 一般不會在中断函数中等待起到消抖作用、另外也不該在中断函数中調用HalUARTWrite、建議你參考Z-Stack 內hal_key.c對SW6的實作
  • 那请问怎么才能起到消抖作用
  • hal_key.c內有标註debounce 的部分你看一下、基本上就是發动一timer event在一段時間後再來讀按鍵值
  •  NOTE: If polling is used, the hal_driver task schedules the KeyRead()
           to occur every 100ms.  This should be long enough to naturally
           debounce the keys.  The KeyRead() function remembers the key
           state of the previous poll and will only return a non-zero
           value if the key state changes.
    
     NOTE: If interrupts are used, the KeyRead() function is scheduled
           25ms after the interrupt occurs by the ISR.  This delay is used
           for key debouncing.  The ISR disables any further Key interrupt
           until KeyRead() is executed.  KeyRead() will re-enable Key
           interrupts after executing.  Unlike polling, when interrupts
           are enabled, the previous key state is not remembered.  This
           means that KeyRead() will return the current state of the keys
           (not a change in state of the keys).
    
     NOTE: If interrupts are used, the KeyRead() fucntion is scheduled by
           the ISR.  Therefore, the joystick movements will only be detected
           during a pushbutton interrupt caused by S1 or the center joystick
           pushbutton.
    
     NOTE: When a switch like S1 is pushed, the S1 signal goes from a normally
           high state to a low state.  This transition is typically clean.  The
           duration of the low state is around 200ms.  When the signal returns
           to the high state, there is a high likelihood of signal bounce, which
           causes a unwanted interrupts.  Normally, we would set the interrupt
           edge to falling edge to generate an interrupt when S1 is pushed, but
           because of the signal bounce, it is better to set the edge to rising
           edge to generate an interrupt when S1 is released.  The debounce logic
           can then filter out the signal bounce.  The result is that we typically
           get only 1 interrupt per button push.  This mechanism is not totally
           foolproof because occasionally, signal bound occurs during the falling
           edge as well.  A similar mechanism is used to handle the joystick
           pushbutton on the DB.  For the EB, we do not have independent control
           of the interrupt edge for the S1 and center joystick pushbutton.  As
           a result, only one or the other pushbuttons work reasonably well with
           interrupts.  The default is the make the S1 switch on the EB work more
           reliably.

  •  NOTE: If polling is used, the hal_driver task schedules the KeyRead()
           to occur every 100ms.  This should be long enough to naturally
           debounce the keys.  The KeyRead() function remembers the key
           state of the previous poll and will only return a non-zero
           value if the key state changes.
    
     NOTE: If interrupts are used, the KeyRead() function is scheduled
           25ms after the interrupt occurs by the ISR.  This delay is used
           for key debouncing.  The ISR disables any further Key interrupt
           until KeyRead() is executed.  KeyRead() will re-enable Key
           interrupts after executing.  Unlike polling, when interrupts
           are enabled, the previous key state is not remembered.  This
           means that KeyRead() will return the current state of the keys
           (not a change in state of the keys).
    
     NOTE: If interrupts are used, the KeyRead() fucntion is scheduled by
           the ISR.  Therefore, the joystick movements will only be detected
           during a pushbutton interrupt caused by S1 or the center joystick
           pushbutton.
    
     NOTE: When a switch like S1 is pushed, the S1 signal goes from a normally
           high state to a low state.  This transition is typically clean.  The
           duration of the low state is around 200ms.  When the signal returns
           to the high state, there is a high likelihood of signal bounce, which
           causes a unwanted interrupts.  Normally, we would set the interrupt
           edge to falling edge to generate an interrupt when S1 is pushed, but
           because of the signal bounce, it is better to set the edge to rising
           edge to generate an interrupt when S1 is released.  The debounce logic
           can then filter out the signal bounce.  The result is that we typically
           get only 1 interrupt per button push.  This mechanism is not totally
           foolproof because occasionally, signal bound occurs during the falling
           edge as well.  A similar mechanism is used to handle the joystick
           pushbutton on the DB.  For the EB, we do not have independent control
           of the interrupt edge for the S1 and center joystick pushbutton.  As
           a result, only one or the other pushbuttons work reasonably well with
           interrupts.  The default is the make the S1 switch on the EB work more
           reliably.