EmbeddedRelated.com
Forums

Timer interrupts on LPC2138

Started by ylavoie66 May 12, 2006
Has anyone ever experienced a similar problem with timers on the
LPC2138?

I want to get a timer to generate an interrupt at every 1ms (system
tick). I configured the prescaler so the timer would count with a 1ms
period, and the match register set so the timer would generate an
interrupt every 1ms.

This resulted in a 2ms system tick ??!!

As a test, I changed my prescaler, so the counter would count on a 1us
period, and interrupt every 1000us, which is the same as 1mm here in
Canada :)

I am still waiting for an answer from Philips, but it seems that
setting a match of "1" is not working properly...
Yves Lavoie

An Engineer's Guide to the LPC2100 Series

---- Original Message ----
From: "ylavoie66"
To:
Sent: Saturday, May 13, 2006 4:59 AM
Subject: [lpc2000] Timer interrupts on LPC2138

> Has anyone ever experienced a similar problem with timers on the
> LPC2138?
>
> I want to get a timer to generate an interrupt at every 1ms (system
> tick). I configured the prescaler so the timer would count with a 1ms
> period, and the match register set so the timer would generate an
> interrupt every 1ms.
>
> This resulted in a 2ms system tick ??!!
>
> As a test, I changed my prescaler, so the counter would count on a 1us
> period, and interrupt every 1000us, which is the same as 1mm here in
> Canada :)
>
> I am still waiting for an answer from Philips, but it seems that
> setting a match of "1" is not working properly...

You should set the match register to one less than the desired period. The
timer goes to 0 the next count _after_ the match, so if you set the match
register to e.g. 4, the timer will count 0, 1, 2, 3, 4, 0, 1, 2, 3, 4...
giving a period of 5. Try a match value of 0.

Karl Olsen





>
> As a test, I changed my prescaler, so the counter would count on a
1us
> period, and interrupt every 1000us, which is the same as 1mm here in

> Canada :)

Always thought Canadians were odd. They even have a different speed of
light over there !

Sorry :),
Bruce






--- In l..., "Karl Olsen" wrote:
> You should set the match register to one less than the desired
period. The
> timer goes to 0 the next count _after_ the match, so if you set
the match
> register to e.g. 4, the timer will count 0, 1, 2, 3, 4, 0, 1, 2,
3, 4...
> giving a period of 5. Try a match value of 0.
>
> Karl Olsen
>
Thanks Karl.

Is it something you already tried and tested?
Philips documentation does not clearly state that. Then again it
won't be the first time an IC manufacturer gives incomplete
documentation :(

Regards,

Yves Lavoie

--- In l..., "ylavoie66" wrote:
>
> --- In l..., "Karl Olsen" wrote:
> >
> >
> > You should set the match register to one less than the desired
> period. The
> > timer goes to 0 the next count _after_ the match, so if you set
> the match
> > register to e.g. 4, the timer will count 0, 1, 2, 3, 4, 0, 1, 2,
> 3, 4...
> > giving a period of 5. Try a match value of 0.
> >
> > Karl Olsen
> >
>
>
> Thanks Karl.
>
> Is it something you already tried and tested?
> Philips documentation does not clearly state that. Then again it
> won't be the first time an IC manufacturer gives incomplete
> documentation :(
>
> Regards,
>
> Yves Lavoie
>

Figure 46 in the datasheet is quite specific. The counter goes to
zero on the PCLK following the match and more specifically, at the
terminal count of the prescale counter.

Figure 47 shows more detail of the stop on match and interrupt on
match. In this case everything happens at the end of the first step
of the prescale counter.

Richard





--- In l..., "ylavoie66" wrote:

> Is it something you already tried and tested?
> Philips documentation does not clearly state that. Then again it
> won't be the first time an IC manufacturer gives incomplete
> documentation :(
>
> Regards,
>
> Yves Lavoie
>

Yves,

The following code generates a 1ms "tick" interrupt, which I believe is
what you're looking for. It assumes a 60 MHz system. The manual is
quite clear about the need to subtract one from the desired value when
setting the counter value.

Hope this helps: it's been tried an tested on a LPC2134 (pretty much
the same as the LPC2138). Note that the function to initialise the
timer is called once on system startup, and assumes (all) interrupts
are disabled.

Brendan

>>>>>>>>>>>>>> CODE EXAMPLE <<<<<<<<<<<<<<<<<<<<

/* TIMER counter value for 1ms tick */

#define TIMER_TICK_1_MS (59999) /* VPB at 60 MHz clock */

/* system tick count in milliseconds */

static volatile
int tick_count;

/*
* Interrupt handler for 1ms "tick"
*
* The handler will be called from interrupt processing context
*/

static
void tick_isr(void)

{
/* reset the interrupt */

REG(TIMER1_IR) = 0x01;

/* bump the millisecond conter */

tick_count++;

/* call RTOS to handle tick */

RtosTick();

return;
}

/*
* Start "tick" interupt for 1ms system "tick" using TIMER 1
*/

static
void tick_init(void)

{
/* ensure timer 1 is off */

TIMER1_TCR = 0x00;

/* set timer counter and prescale counter */

TIMER1_TC = 0x0;
TIMER1_PC = 0x0;

/* set prescale and match values to give 1 ms "tick" */

TIMER1_PR = 0x0;
TIMER1_MR0 = TIMER_TICK_1_MS;

/* set control to interrupt on MR0 match,
and reset to 0 (i.e. "tick") */

TIMER1_MCR = 0x3;

/* vector TIMER 1 as mid-level priority and enabled */

VICVectCntl11 = 0x25;

/* vector TIMER 1 interrupt address */

VICVectAddr11 = (unsigned int) tick_isr;

/* enable TIMER 1 in VIC */

VICIntEnable = 0x0020;

/* go start timer 1... */

TIMER1_TCR = 0x1;

return;
}

>>>>>>>>>>>>>> END CODE EXAMPLE <<<<<<<<<<<<<<<<<<<<

Thanks to everybody.

The group was, once again, very usefull
Yves Lavoie