1

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
    }
} ```
2
  • 1
    Is __delay_ms() a valid thing to call from an ISR? On many systems, this would just cause the system to lock up. Commented Jun 21, 2023 at 15:54
  • It's valid but a really bad practice. Commented Jun 22, 2023 at 7:15

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.