LPC1769 Timer with 1ms Interrupt

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LPC1769 Timer with 1ms Interrupt

6,120 Views
thoret
Contributor II

Hi,

I need a Timer with 1 ms Interrupt.

Is it right that the LPC1769 works/counts with 96 MHz default?

        LPC_SC->PCONP |= 1 << 1; //Power up Timer

         LPC_SC->PCLKSEL0 |= 1<<1; //100 MHz
        LPC_SC->PCLKSEL0 |= 1<<1; //96 MHz
        LPC_TIM0->MR0 = 1000; //1ms
        LPC_TIM0->MCR |= 1 << 0; // Interrupt on Match0 compare
        LPC_TIM0->MCR |= 1 << 1; // Reset timer on Match 0
        LPC_TIM0->TCR |= 1 << 1; // Reset Timer0

        NVIC_EnableIRQ(TIMER0_IRQn);

I test it with LED ans set " LPC_TIM0->MR0 = 1000000; //1s". It seems that the LED blink 1s on and off

But I didn't understand the mathematik behind. How can I calculate 1 second with 96MHz und 1000000?

If I didn't set "LPC_SC->PCLKSEL0 |= 1<<1; //96 MHz" It should be the same, because I think 96 is default. But the LED blink different.

Could you explain me the calculation. With settins I nedd for 1ms , or for example for 1µs.

Labels (2)
0 Kudos
5 Replies

2,808 Views
sai_sumanth
Contributor I

bro i hope you are doing wrong in intializations???

and next thing is it can easily done by PRESCALAR and MR0

where here you take PCLK=CCLK

then at this time you need to set PRESCALAR Register=999999--->(999999+1)==>1000000--->1us

now set MR0=1000.

finally 1us * 1000= 1ms 

its so simple!!!!!!!!

0 Kudos

2,878 Views
rons
Contributor II

Yes, a typo.

Timer clock frequency = 100MHz, or .01usec per count or 10nsec per count

A 10msec period of time / .01usec per count = .01/.01usec = 1000000  = MR0

A 1msec period of time / .01usec per count = .001/.01usec = 100000 = MR0

If MR0=1000 then the period would be  .01usec x 1000 = 10usec, not 1 msec,

so jeremyZhou is in error by only a factor of 100.

Or use the systick timer on chip or the Repetitive Interrupt Timer (RIT)

/*
* System Tick Timer values for a 10msec interrupt (100 interrupts per sec)
*
* CPU clock(CCLK) = 100MHz
* RELOAD = number of system clocks -1, (CCLK/100) - 1.
* or
* RELOAD = STCALIB, the factory programmed value
*/
void SysTick_Timer_Init(void)
{
systick_count = 0;
STRELOAD = STCALIB & STCALIB_TENMS_MASK;
STCTRL = STCTRL_CPUCLK | STCTRL_TICKINT | STCTRL_ENABLE;
}

void SysTick_Handler(void)
{
systick_count++;
}

0 Kudos

2,878 Views
thoret
Contributor II

Thank you.

I understand the clock setting and the math.

But:

Number of counts per interrupt = .01 / 100 nsec per count = 100000 counts.

With 5 zeros isn't it?

And for a Interrupt with 1 ms:

LPC_TIM0->MR0 = 10000;   //1ms   ???

0 Kudos

2,878 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Thore,

I think R Sudjian made a mistake, it's the 10nsec per count when the frequency of the Timer is 100 MHz.

So the LPC_TIM0->MR0 = 1000; is the correct for a interrupt with 1 ms.
Have a great day,
Ping

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

2,878 Views
rons
Contributor II

   LPC_SC->PCLKSEL0 |= 1<<1; //100 MHz
   LPC_SC->PCLKSEL0 |= 1<<1; //96 MHz

Your code is setting the clock division for the watch dog timer.  The timer0 position is at bits 3-2 (div << 2).

The default for timer 0 is CCLK/4 so you need to write "01" to bits 3-2 (0x01 << 2) for 100MHz. So the two

lines above don't make any sense.

The math is easy.

1.You set the clock to the timer with PCLKSEL0.

2. Start the timer and it will count up to your match value and interrupt.

3. The timer resets and counts up again.

For 10msec interrupt:

Timer 0 clock = 100MHz clock that would be 100nsec per count.

Desired count timer is 10msec = .01sec,

Every Timer0 count will equal 100nsec.

Number of counts per interrupt = .01 sec / 100nsec per count = 1000000 counts.  MR0 = 1000000.