DSPIC30f timer1 not trigger everytime on POR

interruptspictimer

I am using timer1 on DSPIC30f4011 to generate an interrupt every one millisecond. However, the timer1 ISR is not executed every time on POR (2 out of 10 times). If I turn on the power for the chip and use MCLR (switch it to high or low) the timer will work everytime on a MCLR reset. Is there anything that I need to clear in the software? I already reset the timer1 flag.

unsigned char t2=0;
unsigned char  t1=0;
unsigned char t0=0;
void main() {
  ADPCFG=0xff;
  //timer1 setting##########
  T1IF_bit= 0;                     //clear flag 
  IEC0.T1IE         = 1;           //enable timer1 interrupt
  IPC0=0b0100000000000000;         //priority
  PR1                 = 7373;      //load
  //###########


  UART2_Init(115200);             //output something to computer


  ////////
  T1CON.TON=1;  ///start timer1
  ////////
  while(1){

    UART_Write(t2);
    UART_Write(t1);
    UART_Write(t0); 
  }
}
void Timer1Interrupt() iv IVT_ADDR_T1INTERRUPT{

  T1IF_bit= 0;  //clear the flag
  t0++;         //increment something
  if(t0==255){t1++;}
  if(t1==255){t2++;}
}

Best Answer

If it only sometimes starts, it is probably a hardware problem. Check that the power supply rise time is within spec for the PIC. If not, a external reset circuit may be required. The power up timer and brown out detect (if there is one) may also help.

It doesn't seem that the firmware is all that relevant to this, but there are a number of issues with it. Hard coded constant, like 7373, magically picked out of the air are a really bad idea. Also, your 3-byte increment routine in the interrupt is broken. It's funny how you have to stand on your head to increment 3 bytes in C when this would be simpler in assembler. I haven't done a interrupt routine in C30, so I'm not sure about this, but I don't see any obvious way the compiler is supposed to know this is a interrupt routine and therefore end with a RETFIE instruction instead of RETURN. Isn't there some pragma you have to mutter to the compiler for it to produce a valid interrupt routine?