So I have this funny problem. I have a dsPIC running at 40MIPS (20MHz external crystal with PLL), and as long as I don't enable timer1 interrupt, everything seems to work fine. But when I enable it, it seems the chip just freezes after a few ms.
So the following code pulse RB7 at about 80Hz, when T1IE=0, it works like it should. With T1IE=1 however, all kinds of weird stuff happen. From my logic analyzer output, sometimes the RB7 gets latched low after 1 pulse, sometimes after 3 or 4, sometimes there is a extra long pulse.
It all seems the code is not exiting from the ISR, but I don't know why.
#include <p33fj128gp202.h>
_FOSCSEL(FNOSC_PRIPLL & IESO_ON);
_FOSC(FCKSM_CSECME & OSCIOFNC_OFF & POSCMD_HS);
volatile unsigned long TICK;
void _ISR _T1Interrupt(void)
{
/* Interrupt Service Routine code goes here */
IFS0bits.T1IF = 0; //Clear Timer1 interrupt flag
}
void clock_init()
{
CLKDIVbits.PLLPRE=2; //4x PLLPRE
PLLFBDbits.PLLDIV=0x1E; //32x PLLDIV
CLKDIVbits.PLLPOST=0; //2x PLLPOST
}
void timers_init()
{
T1CONbits.TON = 0; // Disable Timer
T1CONbits.TCS = 0; // Select internal instruction cycle clock
T1CONbits.TGATE = 0; // Disable Gated Timer mode
T1CONbits.TCKPS = 0b00; // Select 1:1 Prescaler
TMR1 = 0x00; // Clear timer register
PR1 = 40000; // Load the period value
IFS0bits.T1IF = 0; // Clear Timer1 Interrupt Flag
T1CONbits.TON = 1; // Start Timer
IEC0bits.T1IE = 1; // Enable Timer1 interrupt
}
int main(void)
{
unsigned int i,j;
unsigned int v=0;
clock_init();
timers_init();
AD1PCFGL = 0xffff; //All analog capable pins in digital mode
TRISA = 0; //All pins output
TRISB = 0; //All pins output
LATA = 0; //All pins low
LATB = 0; //All pins low
while(1)
{
for(i=0;i<65535;i++);
LATBbits.LATB7=v;
v=!v;
}
return 0;
}