在调试时,发现如果采用轮询方式,中断会被连续的触发,只有加上预定义ISR_KEYINTERRUPT时,中断才是正确的。另外,发现如果采用轮询方式以及某个IO口定义为下拉方式,程序就无法组网了。
YiKai Chen:
采用轮询方式,中断会被连续的触发、照理來說不可能、請問你說的中断会被连续的触发指的是那個函式?
user5281211:
回复 YiKai Chen:
这是我的配置部分:,位于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,确认只可能来自中断函数
YiKai Chen:
回复 user5281211:
一般不會在中断函数中等待起到消抖作用、另外也不該在中断函数中調用HalUARTWrite、建議你參考Z-Stack 內hal_key.c對SW6的實作
user5281211:
回复 YiKai Chen:
那请问怎么才能起到消抖作用
YiKai Chen:
回复 user5281211:
hal_key.c內有标註debounce 的部分你看一下、基本上就是發动一timer event在一段時間後再來讀按鍵值
Susan Yang:
回复 user5281211:
NOTE: If polling is used, the hal_driver task schedules the KeyRead()to occur every 100ms.This should be long enough to naturallydebounce the keys.The KeyRead() function remembers the keystate of the previous poll and will only return a non-zerovalue if the key state changes.NOTE: If interrupts are used, the KeyRead() function is scheduled25ms after the interrupt occurs by the ISR.This delay is usedfor key debouncing.The ISR disables any further Key interruptuntil KeyRead() is executed.KeyRead() will re-enable Keyinterrupts after executing.Unlike polling, when interruptsare enabled, the previous key state is not remembered.Thismeans 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 bythe ISR.Therefore, the joystick movements will only be detectedduring a pushbutton interrupt caused by S1 or the center joystickpushbutton.NOTE: When a switch like S1 is pushed, the S1 signal goes from a normallyhigh state to a low state.This transition is typically clean.Theduration of the low state is around 200ms.When the signal returnsto the high state, there is a high likelihood of signal bounce, whichcauses a unwanted interrupts.Normally, we would set the interruptedge to falling edge to generate an interrupt when S1 is pushed, butbecause of the signal bounce, it is better to set the edge to risingedge to generate an interrupt when S1 is released.The debounce logiccan then filter out the signal bounce.The result is that we typicallyget only 1 interrupt per button push.This mechanism is not totallyfoolproof because occasionally, signal bound occurs during the fallingedge as well.A similar mechanism is used to handle the joystickpushbutton on the DB.For the EB, we do not have independent controlof the interrupt edge for the S1 and center joystick pushbutton.Asa result, only one or the other pushbuttons work reasonably well withinterrupts.The default is the make the S1 switch on the EB work morereliably.
Susan Yang:
回复 user5281211:
NOTE: If polling is used, the hal_driver task schedules the KeyRead()to occur every 100ms.This should be long enough to naturallydebounce the keys.The KeyRead() function remembers the keystate of the previous poll and will only return a non-zerovalue if the key state changes.NOTE: If interrupts are used, the KeyRead() function is scheduled25ms after the interrupt occurs by the ISR.This delay is usedfor key debouncing.The ISR disables any further Key interruptuntil KeyRead() is executed.KeyRead() will re-enable Keyinterrupts after executing.Unlike polling, when interruptsare enabled, the previous key state is not remembered.Thismeans 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 bythe ISR.Therefore, the joystick movements will only be detectedduring a pushbutton interrupt caused by S1 or the center joystickpushbutton.NOTE: When a switch like S1 is pushed, the S1 signal goes from a normallyhigh state to a low state.This transition is typically clean.Theduration of the low state is around 200ms.When the signal returnsto the high state, there is a high likelihood of signal bounce, whichcauses a unwanted interrupts.Normally, we would set the interruptedge to falling edge to generate an interrupt when S1 is pushed, butbecause of the signal bounce, it is better to set the edge to risingedge to generate an interrupt when S1 is released.The debounce logiccan then filter out the signal bounce.The result is that we typicallyget only 1 interrupt per button push.This mechanism is not totallyfoolproof because occasionally, signal bound occurs during the fallingedge as well.A similar mechanism is used to handle the joystickpushbutton on the DB.For the EB, we do not have independent controlof the interrupt edge for the S1 and center joystick pushbutton.Asa result, only one or the other pushbuttons work reasonably well withinterrupts.The default is the make the S1 switch on the EB work morereliably.