我试图在没有板子按动按钮等的情况下在家测试中断功能。我放弃了代码,复制了一个示例,但仍然无法使它正常工作。下面是示例代码。
在while循环中,它将位设置为ON,但不会调用中断。我在中断处设定了休息时间。也许无法测试?
我的最终结果是我需要测试是否按下了button1和button2。我知道如何在不中断的情况下做到这一点,但需要上课。
教授希望我们使用中断。感谢您的帮助!
P18F452
unsigned short sync_flag;
char bad_synch; // variable for detecting bad synchronization
int cnt = 0; // Global variable cnt
void Interrupt() {
// This is external INT0 interrupt (for sync start)
// - once we get falling edge on RB0 we are disabling INT0 interrupt
if (INT0IF_bit && INT0IE_bit) {
cnt = 0;
sync_flag = 1;
INT0IF_bit = 0;
INT0IE_bit = 0;
PORTD = 0xFF;
}
}
void main() {
ADCON1 = 0x0F; // AD converter off
CMCON = 7;
sync_flag = 0; // sync_flag is set when falling edge on RB0 is detected
while(1){
TRISB = 0xFF; // Set PB0 as input
TRISD = 0x00; // Set PortD as output
PORTD = 0x00; // Starting value for PortD
INTEDG0_bit = 0; // Interrupt on falling edge on RB0
INT0IF_bit = 0; // Clear INT0IF
INT0IE_bit = 0; // turn OFF interrupt on INT0
GIE_bit = 1; // enable GIE
while(1){
bad_synch = 0; // set bad synchronization variable to zero
sync_flag = 0; // reseting sync flag
INT0IF_bit = 1;
INT0IE_bit = 1; // enable external interrupt on RB0 (start sync procedure)
}
}
}