lpcware

Timer Library Bug

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by ElectroNerd on Sun Feb 03 12:08:32 MST 2013
Does the "timer" library in "Examples\NXP\LPC1000\LPC17xx\NXP_LPCXpresso1769_MCB1700_2011-02-11.zip" have a bug for the interval calculation?

[B]My Calculations[/B]

In system_LPC17xx.c, it gives the following defines:

[B]#define PLL0CFG_Val           0x00 00 000B[/B]
#define PLL1_SETUP            1
#define PLL1CFG_Val           0x00000023
[B]#define CCLKCFG_Val           0x00000003[/B]
#define USBCLKCFG_Val         0x00000000
#define PCLKSEL0_Val          0x00000000
[B]#define PCLKSEL1_Val          0x00000000[/B]

[B]#define XTAL        (12000000UL)        /* Oscillator frequency               */[/B]


Which means:

N = 1
M = 12
Fin = 12 MHz
Clock Divide = 4

Fcco = (2 x M x Fin)/N = (2 x 12 x 12e6)/1 = 288 MHz

CCLK (CPU clock) = 288/4 = 72 MHz

PCLK (Peripheral Clock) = CCLK/4 = 18 MHz


[B]timer.c[/B]

// This function delays in milli-seconds (ms)

void delayMs(uint8_t timer_num, uint32_t delayInMs)
{
  if ( timer_num == 0 )
  {
LPC_TIM0->TCR = 0x02;/* reset timer */
LPC_TIM0->PR  = 0x00;/* set prescaler to zero */
LPC_TIM0->MR0 = delayInMs * (9000000 / 1000-1);
LPC_TIM0->IR  = 0xff;/* reset all interrrupts */
LPC_TIM0->MCR = 0x04;/* stop timer on match */
LPC_TIM0->TCR = 0x01;/* start timer */
 
/* wait until delay time has elapsed */
while (LPC_TIM0->TCR & 0x01);
  }
  else if ( timer_num == 1 )
  {
LPC_TIM1->TCR = 0x02;/* reset timer */
LPC_TIM1->PR  = 0x00;/* set prescaler to zero */
LPC_TIM1->MR0 = delayInMs * ([B]9000000[/B] / 1000-1);
LPC_TIM1->IR  = 0xff;/* reset all interrrupts */
LPC_TIM1->MCR = 0x04;/* stop timer on match */
LPC_TIM1->TCR = 0x01;/* start timer */
 
/* wait until delay time has elapsed */
while (LPC_TIM1->TCR & 0x01);
  }
  return;
}


Can someone explain why the code implies a 9 MHz PCLK instead of 18 MHz? Shouldn't it be corrected to:

LPC_TIM1->MR0 = delayInMs * ([B]18000000[/B] / 1000-1);


Also, I have seen "SystemCoreClock" being used as a reference for calculating delay intervals - is this equivalent to CCLK (in this instance, 72 MHz)?

Outcomes