PIC External Interrupt Problems

Thread Starter

crazyengineer

Joined Dec 29, 2010
156
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.

Rich (BB code):
#include <p18F2420.h>
#include <delays.h>
#include <portb.h>
#pragma config OSC = INTIO67
#pragma config WDT = OFF
#pragma config PBADEN = OFF


void ISR(void);
void CONFIG(void);

#pragma code isr = 0x08
#pragma interrupt ISR
void ISR(void)
{
	if(INTCONbits.INT0IF)
	{
		PORTB |=0x02;	
		
	}
	else
	{
		PORTB &=~0x02;
	}
	INTCONbits.INT0IF=0;
}
#pragma code



void main()
{
	CONFIG();
	
	while(1){}

}





void CONFIG(void)
{
	TRISB=0x01;						// Set everything but RB0 as output pins
	PORTB=0x00;
	INTCONbits.INT0IE=1;			//	Enable external interrupt on INT0
	INTCON2bits.RBPU=0;				//	Pull ups on Portb are enable	
	INTCON2bits.INTEDG0=1;			// Activates on raising edge
	INTCONbits.GIE=1;				//	Enable global interrupts
}
 

nsaspook

Joined Aug 27, 2009
13,080
Doesn't INTCONbits.GIE=1 enable the global interrupt?
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.

Note: Interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding
enable bit or the global enable bit. User software should ensure the appropriate interrupt flag bits are clear
prior to enabling an interrupt. This feature allows for software polling.
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:

t06afre

Joined May 11, 2009
5,934
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
 
Top