2015-04-15 63 views
-2

我在使用MPLAB 8.89和c18編譯器時遇到pic18f4550中斷問題。中斷問題pic18f4550

問題是,如果引腳改變狀態,中斷不會進入。我可以看到狀態變化,但它沒有改變。但是,如果將中斷編寫爲一箇中斷並進入例程。

這是我的代碼,任何想法?

////// Autonomated Velocity Transmssion for a Bicycle of 9 gears///// 

#include <p18f4550.h> 

//////////////////////////////////////////// Variables ///////////////////////////////////////////////// 
unsigned int sensors=0;    // Counter of the sensor in the star 
unsigned int sensorw=0;    // Counter of the sensor in the wheel 
unsigned int rpms=0;     // Rpms of the star 
unsigned int rpmw=0;     // Rpms of the wheel 
unsigned int velw; 

//////////////////////////////////////////// Interruption functions ////////////////////////////////////////////////// 
void Llanta (void);     //Wheel 
void Estrella (void);    //Star 


/////////////////////////////////////// High Priority Interruptions ///////////////////////////////////////////////// 

#pragma code high_vector=0x08 
void high_interrupt (void) 
{ 
    _asm nop _endasm 
} 

//////////////////////////////////////////// Low Priority Interruptions ////////////////////////////////////////////////// 

#pragma code low_vector=0x18 
void low_interrupt (void) 
{ 
    if(INTCON3bits.INT1IF==1){  //interruption for INT1 on rising edge 
     _asm goto Llanta _endasm //Wheel 
    } 

    if(INTCONbits.RBIF==1) {  //Interruption for change on RB port change 
     _asm goto Estrella _endasm //Star 
    } 

} 


#pragma code     
#pragma interruptlow Llanta 
void Llanta (void)    //Counts when the hall sensor of the wheel is activated 
{  
    INTCON3bits.INT1IF=0;   //Turn off flag 
    sensorw++;     //add 
} 


#pragma interruptlow Estrella   //Counts when the hall sensor of the star is activated 
void Estrella (void) 
{ 
    INTCONbits.RBIF=0;     //Turn off flag 
    if(PORTBbits.RB4 == 1){  
     sensors++;      //Add 
    } 
} 

////////////////////////////////////////////// Main program ///////////////////////////////////////////////////////////////////// 
void main (void) 
{ 
    unsigned int temp16;  
    OSCCON=0b01100000;   //Oscillator 4 MHz 

    //Pins Configuration 
    TRISBbits.RB1=1;   //input sensor wheel 
    TRISBbits.RB4=1;   //input sensor star 
    TRISB=0xFF;     //All as inputs 

    T1CON=0b01000001;   // Oscillator 4 MHz, timer 1   
    ///////////////////////// Priorities //////////////////////////////// 

    RCONbits.IPEN=1;   //Enable priority levels 
    INTCON=0b11001000;   //Enable high, low interrupts and RB port change interrupt 
    INTCON2=0b01110000;   //Pull up disabled, interrupt on rising edge, and low priority on rb change 
    INTCON3=0b00011000;   //low priority interruption, and enables external interruptions. 
    ADCON1=0x0F;    //digital input 
    do{ 
     INTCON3bits.INT1IF=1; // This was made to make a test. i put this as one, the interruption works and it goes where it should go. 
    } 
    while(1); 
} 
+0

請妥善縮進代碼中的端口。這是無法讀取的,所以我不會爲讀它而煩惱。如果您使用Tab鍵縮進,則應在按Tab時將代碼編輯器設置爲注入空格。 – Lundin

+0

對不起,縮進,我只是改變代碼,使其簡單的中斷目的。 –

+0

'INTCON3 = 0b00011000; 'INT1IE'和'INT2IE'都被置位,這意味着'INT2'外部中斷被使能。但是相應的標誌不處理是中斷函數。可以'INTCON3 = 0b00001000;'解決問題? – francis

回答

1

檢查下面的一些想法

  1. 檢查你的配置位,更具體的CONFIG3H: CONFIGURATION REGISTER 3PBADEN

    PBADEN: PORTB A/D Enable bit 
    
    1 = PORTB<4:0> pins are configured as analog input channels on Reset 
    0 = PORTB<4:0> pins are configured as digital I/O on Reset 
        If set to analog it won't register your pin status changes. 
    
  2. 確保其它的端口B引腳不浮,將它們接地或將它們變成輸出。手冊指出

    RBIF: RB Port Change Interrupt Flag bit(1) 
    1 = At least one of the RB7:RB4 pins changed state (must be cleared in software) 
    0 = None of the RB7:RB4 pins have changed state 
    
    Note 1: A mismatch condition will continue to set this bit. 
    Reading PORTB, and then waiting one additional instruction cycle, 
    will end the mismatch condition and allow the bit to be cleared. 
    
  3. 我還要清除中斷標誌位作爲最後一條指令不能退出中斷作爲第一

  4. 手冊之前還指出,

    「的中斷功能的變化特徵是 推薦用於喚醒按鍵操作和操作 ,其中PORTB僅用於電平變化中斷功能 在使用 中斷時不建議輪詢PORTB不斷變化的功能「。

您是輪詢/讀取你的中斷