PIC External Interrupt Problems

Discussion in 'Embedded Systems and Microcontrollers' started by crazyengineer, Sep 29, 2012.

  1. crazyengineer

    Thread Starter Member

    Dec 29, 2010
    156
    2
    Hello again. I'm trying to do a pushbutton example using external interrupts using INT0 for a pic18f2420. However, it seems like it doesn't activate even though I connect it to ground and vdd.

    Code ( (Unknown Language)):
    1. #include <p18F2420.h>
    2. #include <delays.h>
    3. #include <portb.h>
    4. #pragma config OSC = INTIO67
    5. #pragma config WDT = OFF
    6. #pragma config PBADEN = OFF
    7.  
    8.  
    9. void ISR(void);
    10. void CONFIG(void);
    11.  
    12. #pragma code isr = 0x08
    13. #pragma interrupt ISR
    14. void ISR(void)
    15. {
    16.     if(INTCONbits.INT0IF)
    17.     {
    18.         PORTB |=0x02;  
    19.        
    20.     }
    21.     else
    22.     {
    23.         PORTB &=~0x02;
    24.     }
    25.     INTCONbits.INT0IF=0;
    26. }
    27. #pragma code
    28.  
    29.  
    30.  
    31. void main()
    32. {
    33.     CONFIG();
    34.    
    35.     while(1){}
    36.  
    37. }
    38.  
    39.  
    40.  
    41.  
    42.  
    43. void CONFIG(void)
    44. {
    45.     TRISB=0x01;                     // Set everything but RB0 as output pins
    46.     PORTB=0x00;
    47.     INTCONbits.INT0IE=1;            //  Enable external interrupt on INT0
    48.     INTCON2bits.RBPU=0;             //  Pull ups on Portb are enable   
    49.     INTCON2bits.INTEDG0=1;          // Activates on raising edge
    50.     INTCONbits.GIE=1;               //  Enable global interrupts
    51. }
    52.  
     
  2. nigelwright7557

    Senior Member

    May 10, 2008
    487
    71
    If i remember correctly there is also a global interupt enable too.
     
  3. crazyengineer

    Thread Starter Member

    Dec 29, 2010
    156
    2
    Doesn't INTCONbits.GIE=1 enable the global interrupt?
     
  4. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,907
    2,168
    You could poll the INTCONbits.INT0IF flag to see if it's set in the while loop. Check the IPEN bit, try setting it to 1 and enable the high interrupt.

    Another thing is optimization. With no user program variables used in the while loop it might just optimize out your ISR code. Adding a assembly instruction to the ISR (nop) at the start will turn off optimization for that section
     
    Last edited: Sep 30, 2012
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Which C compiler and version do you use? And also read the datasheet. It will tell you which flags that need to be set. Have you done some debugging in MPLAB. You can easy use the simulator in MPLAB to debug this. By setting a breakpoint inside the ISR as one example
     
Loading...