2013-10-17 45 views
1

我在下面有一些代碼有一個小問題,我不知道如何解決。基本上發生了什麼是我的高ISR在設置標誌後運行兩次。它只運行兩次並且一致。該子程序只能運行一次,因爲當RB上的輸入發生變化時該標誌被設置,並且該程序在一次改變爲RB輸入後運行兩次。該測試是在MPLAB v8.6中使用工作簿功能進行的。爲什麼RB中斷例程運行兩次?

#include <p18f4550.h> 
#include <stdio.h> 

void init(void) 
{ 
    RCONbits.IPEN =1;  //allows priority  
    INTCONbits.GIE = 1;  //allows interrupts 
    INTCONbits.PEIE = 1; //allows peripheral interrupts 
    INTCONbits.RBIF = 0; //sets flag to not on 
    INTCONbits.RBIE = 1; //enables RB interrupts 
    INTCON2bits.RBPU = 1; //enable pull up resistors 
    INTCON2bits.RBIP = 1; //RB interrupts is high priority 
    PORTB = 0x00; 
    TRISBbits.RB7 = 1; //enable RB7 as an input so we can throw interrupts when it changes. 
} 

#pragma code 
#pragma interrupt high_isr 
void high_isr(void) 
{ 
    if(INTCONbits.RBIF == 1) 
    { 
     INTCONbits.RBIF = 0; 
     //stuff 
    } 
} 

#pragma code  
#pragma code high_isr_entry = 0x08 
void high_isr_entry(void) 
{_asm goto high_isr _endasm} 


void main(void) 
{ 
    init(); 
    while(1); 
} 
+0

你確保清除中斷標誌在你//填充碼? –

+0

是的,我應該將其包含在我的原始代碼中。編輯! –

+0

不知道會發生什麼,但如果你壓縮併發送給我你的項目,我可以在我的機器上嘗試。查看我的個人資料獲取電子郵件 –

回答

2

的RB7中斷標誌是基於一個比較最後鎖存值和該引腳的當前狀態的設置。在數據表中,「引腳與PORTB最後一次讀取時鎖存的舊值進行比較,RB7:RB4的'不匹配'輸出進行」或「運算,生成帶有標誌位的RB端口電平變化中斷。

要清除不匹配情況,數據表繼續說「任何讀取或寫入PORTB(除了使用MOVFF(ANY),PORTB指令的 ),這將結束不匹配條件。

然後您等待一個Tcy(執行nop指令),然後清除該標誌。這記錄在the datasheet的第116頁。

所以在這種情況下最簡單的解決辦法是在中斷程序中聲明一個虛擬變量,並將其設置爲RB7是這樣的:

#pragma interrupt high_isr 
void high_isr(void) 
{ 
    unsigned short dummy; 

    if(INTCONbits.RBIF == 1) 
    { 
     dummy = PORTBbits.RB7; // Perform read before clearing flag 
     Nop(); 
     INTCONbits.RBIF = 0; 
     // rest of your routine here 
     // ...