Search code examples
cembeddedinterruptpicpic18

PIC18f4550 external interrupts is not working


while using interrupts in pic18f4550, I get stuck in external interrupts, and the push button (RB0) is not Inverting (Toggle) the value at PORTD, I used timer0 interrupts which work perfectly, but I can't figure out what the issue is it is with this code, help me to identify the problem, thanks

//the working of External Interrupt0 of PIC18F4550
#include <xc.h>
#define _XTAL_FREQ 4000000 // 4MHz, used by the delay_ms() function
void main()
{   
    TRISBbits.RB0 =1;        // push button
    TRISD=0;                // Configure PortD as output port
    INTCONbits.PEIE = 1;
    INTCONbits.INT0IF =0;   // Clear flag INT0
    INTCONbits.INT0IE =1;    //Enable INT0 
    INTCON2bits.INTEDG0 =1; ; // Set Falling Edge Trigger for INT0
    INTCONbits.GIE=1; // Enable The Global Interrupt
    
    while(1)
    {
    LATD=0x55; //Set some value at PortD
    }
}

void __interrupt() isr(void) // Interrupt ISR
{
    if(INTCONbits.INT0IF ==1){
    INTCONbits.INT0IF=0; // Clear the interrupt 0 flag
    LATD=~LATD; // Invert (Toggle) the value at PortD
    __delay_ms(1000); // Delay for 1 sec
    }
} ```

Solution

  • In default state the port RB0 is switched to analog. If you want use it as digital port you had to change the settings:

    ADCON1 = 0x03;
    

    look into the datasheet page 266

    If you don't have an external pull up on RB0, you had to enable the internal ones with the INTCON2 register.