lpcware

Strange behavior of LPC43xx Timer0/1/2/3 interrupt

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by jokn on Fri Jan 04 11:54:11 MST 2013
Strange behavior of LPC43xx Timer0/1/2/3 interrupt

I'm just testing the timer0 of lpc4330 and I was wondering about that yet it seems at first ran twice as fast as expected.
I spend a lot of time and finally I found out that the problem is clearing the interrupt request bit (LPC_TIMER0->IR)

If my interrupt service routine is very short and especially has not subroutine call , clearing of IR bits fails and the interrupt repeats again.
In the second  run of the interrupt clearing of the IR bit is successful.

void TIMER0_IRQHandler (void)
{
if(msec)msec--;
   LPC_TIMER0->IR = 1;  // this fails in the first run of interrupt
                       // on second run the bit will be cleared
}

It doesn’t mutter if I insert a delay like this,

   for (icnt = 0; icnt < 100; ++icnt);
   LPC_TIMER0->IR = 1;

Or like this

  LPC_TIMER0->IR = 1;
  for (icnt = 0; icnt < 100; ++icnt);

But if I insert a dummy subroutine call, the problem disappears

void dummy () {}

   dummy();
   LPC_TIMER0->IR = 1;

Does anyone have an explanation for the strange behavior?

My timer initialization looks as follows.

void LPC_Timer0_Init (void)
{
  LPC_TIMER0->TCR = 2;          // Reset and Disable Timer
  LPC_TIMER0->CTCR = 0;
  LPC_TIMER0->CCR = 0;          // No Capture
  LPC_TIMER0->TC = 0;           // Counter = 0
  LPC_TIMER0->PC = 0;           // Prescale Counter = 0
  LPC_TIMER0->PR = 0;           // Prescale Register = 0 (no Prescaler)

  LPC_TIMER0->MR[0] = SystemCoreClock / 1000 ;
  LPC_TIMER0->MCR = 3;          // Interrupt & Reset TC on match
  LPC_TIMER0->EMR = 0;
  LPC_TIMER0->IR = 0xFFFFFFFF;  // Clear interrupt pending
  NVIC_EnableIRQ(TIMER0_IRQn);
  LPC_TIMER0->TCR = 1;          // Start Timer0

}

Outcomes