External interrupt code for PIC18LF47k40 not working

Thread Starter

paple13

Joined Feb 2, 2023
5
I wrote a external interrupt code for PIC18LF47k40 on MPLAB X IDE to toggle a LED when rising edge will be occurred. It is not working, but the same type code for PIC18F452 is working fine, only I modified the Register. Can you please help me why my below code for PIC18LF47K0 is not working?

below my code:

C:
#define _XTAL_FREQ 16000000
#include <xc.h>
#include <pic18lf47k40.h>
#include <stdio.h>

void main(void)
{
     TRISDbits.TRISD6=0;             //RD pin as output declaration
     PORTDbits.RD6=1;               

     TRISBbits.TRISB2=1;        /* Make INT2 pin as an input pin*/
     ANSELBbits.ANSELB2 = 0;        //making digital input
     INTCONbits.GIE=1;            /* Enable Global Interrupt*/ 
     INTCONbits.INT2EDG=1;      /* Set Interrupt on rising Edge*/
     PIE0bits.INT2IE=0;        /* Enable INT2 external interrupt*/
     PIE0bits. IOCIE=1;          //enable interrout on change
     PIR0bits.INT2IF=0;           // Clear the interrupt flag
   
while(1)
{
}

}


void __interrupt() isr(void)
{        
        PORTDbits.RD6^=0;    //making togle RD6 pin   
        PIR0bits.INT2IF=0;  // Clear the interrupt fla         
}
Please help me to solve this. Thanks

Mod edit: code tags - JohnInTX
 
Last edited by a moderator:

geekoftheweek

Joined Oct 6, 2013
1,183
I would eliminate this line
Code:
PIE0bits. IOCIE=1; //enable interrout on change
It's not needed for INT0, INT1, and INT2... if you do need it for something else then you will need to add checking in your interrupt routine to make sure you are working with the right interrupt.

Also when setting outputs always use LATx instead of PORTx.

Instead of:
Code:
PORTDbits.RD6^=0; //making togle RD6 pin
do
Code:
LATDbits.RD6^=0; //making togle RD6 pin
I don't have any links on hand, but there are possible issues with the way the PIC works with the PORTx registers internally.

EDIT:

Also
Code:
PIE0bits.INT2IE=0; /* Enable INT2 external interrupt*/
needs to be
Code:
PIE0bits.INT2IE=1; /* Enable INT2 external interrupt*/
 

Thread Starter

paple13

Joined Feb 2, 2023
5
I would eliminate this line
Code:
PIE0bits. IOCIE=1; //enable interrout on change
It's not needed for INT0, INT1, and INT2... if you do need it for something else then you will need to add checking in your interrupt routine to make sure you are working with the right interrupt.

Also when setting outputs always use LATx instead of PORTx.

Instead of:
Code:
PORTDbits.RD6^=0; //making togle RD6 pin
do
Code:
LATDbits.RD6^=0; //making togle RD6 pin
I don't have any links on hand, but there are possible issues with the way the PIC works with the PORTx registers internally.

EDIT:

Also
Code:
PIE0bits.INT2IE=0; /* Enable INT2 external interrupt*/
needs to be
Code:
PIE0bits.INT2IE=1; /* Enable INT2 external interrupt*/
thanks for your suggestion. now the code is working. actually it was my mistake. i didn't notice that.
 
Last edited:
Top