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 external interrupts

Status
Not open for further replies.

Swys

Junior Member level 1
Joined
Jul 22, 2008
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,412
I really hope someone can help me on this. I have to count pulses from an external source. For some reason, only the first pulse is counted. Can someone please have a look at my code to see what I am doing wrong?

I am using the MPLAB C18 compiler with a PIC18F13K50

Here are my interrupt functions:
Code:
#pragma code high_vector=0x08
void interrupt_at_high_vector(void)
{
	_asm
		GOTO high_isr
	_endasm
}
#pragma code

#pragma interrupt high_isr
void high_isr(void)
{
	if ((INTCONbits.INT0IF == 1) &  (INTCONbits.INT0IE == 1))
	{
		//Pulse received
		INTCONbits.INT0IF = 0;
		flow_int = 1;        //Flag indicating when interrupt occurred
		
		
		//PORTCbits.RC0 = 1;
	}
}

This is how my PIC is set up:

Code:
void SetupPIC(void)
{
	//Setup ports
	TRISA = 0x00;
	TRISB = 0x00;
	TRISC = 0x00;
	
	PORTA = 0;
	PORTB = 0;
	PORTC = 0;	
	
	//Disable all analog inputs
	ANSEL = 0x00;
	ANSELH = 0x00;
	
	//Setup USART
	TRISBbits.TRISB5 = 1;
	TRISBbits.TRISB7 = 1;
	
	//SetupUART();
	
	OpenUSART(	USART_TX_INT_OFF	&
				USART_RX_INT_ON		&
				USART_ASYNCH_MODE	&
				USART_EIGHT_BIT		&
				USART_CONT_RX		&
				USART_BRGH_HIGH,
				129);
				
	PIE1bits.RCIE = 1;		//Enable USART receive interrupt
	PIR1bits.RCIF = 0;		//Clear USART receive interrupt flag
	
	//Setup FRAM
	init_FRAM();
	
	//Setup ADC
	init_ADC();
	TRISCbits.TRISC6 = 0;
	
	//Setup Timers
	//Timer0
	T0CON = 0b00000111;		//Timer stopped;16-bit timer;
							//Internal instruction cycle clock;
							//Timer0 prescaler assigned;
							//1:256 prescale value
	INTCONbits.TMR0IE = 1;	//Enable Timer0 interrupts
	INTCONbits.TMR0IF = 0;	//Clear Timer- interrupt flag
	
	//Setup external interrupts INT0 and INT1
	
	INTCONbits.INT0IF = 0;	//Clear the interrupt flag
	INTCONbits.INT0IE = 1;	//Enable external interrupt INT0
	INTCON2bits.INTEDG0 = 0;//Interrupt on the falling edge
	PORTCbits.RC0 = 1;		//Pull HIGH to create falling edge
							//on contact
	
	INTCON3bits.INT1IE = 1;	//Enable external interrupt INT1
	INTCON2bits.INTEDG1 = 0;//Interrupt on the falling edge
	INTCON3bits.INT1IF = 0;	//Clear the interrupt flag
	PORTCbits.RC1 = 1;
	
	INTCONbits.PEIE = 1;	
	INTCONbits.GIE = 1;
	
}
In my main function, I am just checking the flow_int flag and outputting some text on the UART when the flag is set. Please help, since I don't have much time to solve this?

P.S. I've tried it with PORTCbits.RC0 = 1 and PORTCbits.RC0 = 0
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,861
Maybe a silly question, but are you clearing the 'flow_int' flag after checking it in your main routine?
 

Swys

Junior Member level 1
Joined
Jul 22, 2008
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,412
Not silly, I've overlooked something like that quite a few times, but yes, I am clearing that flag
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,861
I cant see where you have set up INT0, RC0 as an input?
 

    Swys

    Points: 2
    Helpful Answer Positive Rating

Swys

Junior Member level 1
Joined
Jul 22, 2008
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,412
Thanks, that was the problem. I didn't realize it should be an input. I changed it...and it works!

Thanks guys!
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top