Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem with Interrupts

Status
Not open for further replies.

natraj20

Member level 3
Joined
Oct 28, 2010
Messages
65
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Location
Florida
Activity points
1,769
Whenever i execute a program in the PIC controller, each of the functions in the interrupt service routine gets executed once at the start or during power on reset. Let me know what should i disable to stop executing.
 

Usually the interrupt vectors are in the early locations in PIC memory so the first instruction needs to be a "goto start" or something like that where "start" is defined as the first location free after the interrupt vectors. If you don't do that it will execute the interrupt routine first. Is that the problem maybe? If not, it could be to do with the state of the interrupt pin before you enable it.

Keith
 
Thanks Keith!!

I think the problem is exactly what you described first coz the ISR gets executed before going into main function. But one more doubt what i have is the interrupt flags during power on reset will be cleared right ?? Coz in the ISR, i the function will be executed with an if condition for the INT1IF flag bit. I also tried to set a variable y in the main and put a check at the ISR as follows:

Code:
#pragma code
#pragma interrupt InterruptServiceHigh
void InterruptServiceHigh(void)
{
     if(y == 1)
     {
        if(INTCONbits.INT0IF)
	{
		x = 0;
		while(x <= 2)
			{
				PORTE = 0x01;
				Delay10KTCYx(120);	// Delay of 1 sec with 12 MHz
				PORTE = 0x02;		
				Delay10KTCYx(120);			
				x++;
			}
		PORTE = 0x00;
		INTCONbits.INT0IF = 0;
	}
     }
}

Initially, the value of y is 0. So only if it enters main, the code should be executed right ? But the interrupt functions are executed first before main.
I do not know the exact location of the start of the program main memory for this controller PIC 18F47J53.

Thanks again Keith!
 

If you are writing in C then the program locations and interrupt service routines should be taken care of for you so my earlier comment is not the cause.

So, I think the problem is more likley to be with your initialisation. Presumably you have some initialisation within "main"? I assume it must run some of that software in order that an interrupt can occur.

I must admit I am more used to assembler with the PICs, but looking at some C code I wrote, I clear interrupts before enabling them:
Code:
	_T3IF = 0;	// clear any pending interrupt
	_T3IE = 1;  // T3 enable interrupt

So maybe you should try something similar. There is a note in the PIC datasheet:

Interrupt flag bits are set when an interrupt
condition occurs, regardless of the state of
its corresponding enable bit or the global
interrupt enable bit. User software should
ensure the appropriate interrupt flag bits
are clear prior to enabling an interrupt.
This feature allows for software polling.

Keith.
 
This is the code i was referring to Keith. In this code, i'm using two external interrupts INT0 and INT2. The first statement in main() function is clearing the interrupt flag for INT0. I do not know how to address this problem. Once after execution at the power on reset, it works as per the code.

Code:
#include <p18f47j53.h>
#include <delays.h>
#include <portb.h>

#pragma config OSC = HS
#pragma config WDTEN = OFF
#pragma config XINST = OFF

int x,y;

void InterruptServiceHigh(void);
void InterruptServiceLow(void);

void main(void) 
{

	INTCONbits.INT0IF = 0;
	INTCONbits.INT0IE = 1;
	INTCON2bits.INTEDG0 = 1;

	RPINR2 = 5;

	INTCON3bits.INT2IF = 0;
	INTCON3bits.INT2IE = 1;
	INTCON3bits.INT2IP = 1;
	INTCON2bits.INTEDG2 = 1;

	INTCON2bits.RBPU = 0;
	EnablePullups();

	TRISE = 0;
	TRISBbits.TRISB0 = 1;
	TRISBbits.TRISB2 = 1;

	ANCON1bits.PCFG8 = 1;
	ANCON1bits.PCFG12 = 1;

	RCONbits.IPEN = 1;
	INTCONbits.GIE = 1;
	INTCONbits.PEIE = 1;
	
	while(1)	
	{
		
		PORTE = 0x03;	
	}

}

//  Interrupt Service Routine
//  High priority interrupt vector
#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh(void)
{
	_asm
	goto InterruptServiceHigh
	_endasm
}

//  Low priority interrupt vector
#pragma code InterruptVectorLow = 0x18
void InterruptVectorLow(void)
{
	_asm
	goto InterruptServiceLow
	_endasm
}

// Interrupt pragma for high priority
#pragma code
#pragma interrupt InterruptServiceHigh
void InterruptServiceHigh(void)
{
	if(INTCONbits.INT0IF)
	{
		x = 0;
		while(x <= 2)
			{
				PORTE = 0x01;
				Delay10KTCYx(120);	// Delay of 1 sec with 12 MHz
				PORTE = 0x02;		
				Delay10KTCYx(120);			
				x++;
			}
		PORTE = 0x00;
		INTCONbits.INT0IF = 0;
	}

	if(INTCON3bits.INT2IF)
	{
		x = 0;			
		while(x <= 2)
			{
				PORTE = 0x00;			
				Delay10KTCYx(120);	// Delay of 1 sec with 12 MHz			
				PORTE = 0x03;		
				Delay10KTCYx(120);			
				x++;
			}
		PORTE = 0x00;
		INTCON3bits.INT2IF = 0;
	}  // return from high priority interrupt
}

//  Interrupt pragma for low priority
#pragma code
#pragma interrupt InterruptServiceLow
void InterruptServiceLow(void)
{
	// function statements

	// return from low priority interrupt
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top