PIC C18 Timer Interrupt Problems

Thread Starter

crazyengineer

Joined Dec 29, 2010
156
Hello again. I'm doing a timer interrupt example using the PIC. Right now I have it configured to turn on and off the LED every 500ms. I'm using the PIC18F2420 on a 20Mhz crystal connected to 22pf capacitors. However, it seems like it's either not activating the interrupt or its taking a long time to do so. Here's the code

Rich (BB code):
//===============================================================================================
//	Includes, Defines, and Global Variables
//===============================================================================================
/*	Includes */
#include <p18F2420.h>
#include <delays.h>
#include <portb.h>
#include <usart.h>
#include <stdio.h>
#include <stdlib.h>
#include <timers.h>
/*	PIC Configuratings */
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config PBADEN = OFF
#pragma config LVP = OFF 
#pragma config PWRT = OFF 
#pragma config DEBUG= ON
#pragma config BOREN = OFF

int counter=0;
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Function Prototypes
//===============================================================================================
void PIC_CONFIG(void);
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Interrupt Handler
//===============================================================================================
#pragma interrupt ISR
void ISR (void)										//	Main Interrupt handler
{
	if(INTCONbits.TMR0IF==1)
	{
		INTCONbits.TMR0IF=0;
		PORTCbits.RC4=~(PORTCbits.RC4);
		WriteTimer0(0xD9DA);
	}
		
}


#pragma code InterruptVectorHigh = 0x08				//	This function simply jumps to the ISR code shown above.
void InterruptVectorHigh (void)
{
	_asm
	goto ISR //jump to interrupt routine
	_endasm
}
#pragma code
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Main Loop
//===============================================================================================
void main(void)
{
PIC_CONFIG();
	while(1);

}
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	PIC Configuration
//===============================================================================================
void PIC_CONFIG(void)
{
	/*	Setting	PIC to use interrupts	*/
	RCONbits.IPEN=1;					//	Enable interrupt priority
	//	Setup Timer 0 for interrupts
	INTCONbits.TMR0IE=1;
	INTCONbits.TMR0IF=0;
	INTCON2bits.TMR0IP=1;
	OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_EXT & T0_EDGE_FALL & T0_PS_1_256);
	WriteTimer0(0xD9DA);
	INTCONbits.GIE=1;					//	Enable global interrupts
	TRISC=0x00;							//	Configure all PORTC pins as outputs
	PORTC=0x00;							//	Turns off all PORTC ports
}
 

ErnieM

Joined Apr 24, 2011
8,415
Do you have the external clock running on T13CKI? That is what you are saying to use when you call for T0_SOURCE_EXT in OpenTimer0().

From the fine manual:
T0_SOURCE_EXT: Transition on TxCKI pin acts as source of clock


If so, drop a breakpoint on the "if(INTCONbits.TMR0IF==1)" line of the ISR and run the program. Go for coffee. Come back and see if the breakpoint was hit.


If not, you have another set-up problem where the interrupt is not set.

As you can set a stimulus clock input on that pin MPLAB sim would also catch this problem without any hardware at all.
 

spinnaker

Joined Oct 29, 2009
7,830
I used THE pickit3 in MPLAB
Those are the tools you are using not the procedure you used to debug.

I am having the exact same issue as you. This is how I plan to debug.

1. Set breakpoint in ISR and see if the breakpoint gets called.
2. If it works then check my code and datasheet again.
3. If it fails switch to internal clock for the timer.
4. If it works then check my xtal and cap connections. Go back to step 1,
5. If it fails check my code and datasheet again.

Don't ASSUME that the openTimer function is working. Check microchip's code too.
 

ErnieM

Joined Apr 24, 2011
8,415
You mean do I have an external oscillator running on RA7 and RA6?
No.

When I ask "Do you have the external clock running on T13CKI?" I am asking if you have an external (not internal, separate and distinct from the PIC) clock (multivibrator or such square or pulse wave source) connected to pin 11, also known as the T13CKI input pin to the Timer 1.

I ask because THAT is the clock source you picked in your code to drive Timer 1.
 
Top