EmbeddedRelated.com
Forums
Memfault Beyond the Launch

help needed with lpc2106 timer for simple delay function

Started by voltz56 March 30, 2010
Iv been trying unsuccessfully for the a few days to write a delay function using timer0, I have looked at the Hitex book and NXP examples but I just cant seem to get it work.

This is where I'm at, at the moment..

void delayMs()
{

SCB_VPBDIV = 0x00000002;// set pclk to 30MHz

T0_PR = 0x00007530; // Load Prescaler register for 1Msec tick
T0_TCR = 0x00000002; // Reset counter and prescaler
T0_TCR = 0x00000001; //enable timer

while ( T0_TC >= T0_PR );
T0_TCR &= ~(1<<0); /* Stop timer */

}

If anyone has any suggestions... or knows of any relevant documentation or tutorials, that would be a great help.
Thanks

An Engineer's Guide to the LPC2100 Series

voltz56 wrote:
> Iv been trying unsuccessfully for the a few days to write a delay
> function using timer0, I have looked at the Hitex book and NXP examples
> but I just cant seem to get it work.
>
> This is where I'm at, at the moment..
>
> void delayMs()
> {
>
> SCB_VPBDIV = 0x00000002;// set pclk to 30MHz
>
> T0_PR = 0x00007530; // Load Prescaler register for 1Msec tick
> T0_TCR = 0x00000002; // Reset counter and prescaler
> T0_TCR = 0x00000001; //enable timer
>
> while ( T0_TC >= T0_PR );
> T0_TCR &= ~(1<<0); /* Stop timer */
>
> }
>
> If anyone has any suggestions... or knows of any relevant documentation
> or tutorials, that would be a great help.

Take a break and deep breath. Then read the UM once more carefully. Hint: You
might want to loop until TC > 0

--

Timo

--- In l..., tike64@... wrote:
>
> voltz56 wrote:
> > Iv been trying unsuccessfully for the a few days to write a delay
> > function using timer0, I have looked at the Hitex book and NXP examples
> > but I just cant seem to get it work.
> >
> > This is where I'm at, at the moment..
> >
> > void delayMs()
> > {
> >
> > SCB_VPBDIV = 0x00000002;// set pclk to 30MHz
> >
> > T0_PR = 0x00007530; // Load Prescaler register for 1Msec tick
> > T0_TCR = 0x00000002; // Reset counter and prescaler
> > T0_TCR = 0x00000001; //enable timer
> >
> > while ( T0_TC >= T0_PR );
> > T0_TCR &= ~(1<<0); /* Stop timer */
> >
> > }
> >
> > If anyone has any suggestions... or knows of any relevant documentation
> > or tutorials, that would be a great help.
>
> Take a break and deep breath. Then read the UM once more carefully. Hint: You
> might want to loop until TC > 0
>
> --
>
> Timo
>

Thanks Timo,

I have it working now, although how... is a little unorthodox.

I noticed that the counter wasn't being reset by

T0_TCR = 0x00000002;

So I had to make TC 0 after the wait loop.

void delay_1sec()
{
SCB_VPBDIV = 0x00000002;// set pclk to 30MHz

T0_PR = 0x00007530; // Load Prescaler register for 1Msec tick
T0_TCR = 0x00000002; // Reset counter and prescaler
T0_TCR = 0x00000001; //enable timer

while ( T0_TC < 0x03e8 );//1000ms
T0_TC = 0;// T0_TCR = 0x00000002, not resetting so...
T0_TCR &= ~(1<<0); // Stop timer

}

voltz56 wrote:
> I noticed that the counter wasn't being reset by
>
> T0_TCR = 0x00000002;

UM says that the counter is reset on the next positive edge of PCLK. My
immediate thought was that with your code

> T0_TCR = 0x00000002; // Reset counter and prescaler
> T0_TCR = 0x00000001; //enable timer

the Counter Reset bit might not have enough time for PCLK edge to occur
reliably. But I can't say for sure if that is the phenomenom. Maybe you could
debug a little more and tell us what you find. You could, for example, try a
little delay between the lines or something else.

Make sure, that the definition of T0_TCR has the volatile attribute. Otherwise
the compiler may optimize away the first line.

--

Timo

--- In l..., tike64@... wrote:
>
> voltz56 wrote:
> > I noticed that the counter wasn't being reset by
> >
> > T0_TCR = 0x00000002;
>
> UM says that the counter is reset on the next positive edge of PCLK. My
> immediate thought was that with your code
>
> > T0_TCR = 0x00000002; // Reset counter and prescaler
> > T0_TCR = 0x00000001; //enable timer
>
> the Counter Reset bit might not have enough time for PCLK edge to occur
> reliably. But I can't say for sure if that is the phenomenom. Maybe you could
> debug a little more and tell us what you find. You could, for example, try a
> little delay between the lines or something else.
>
> Make sure, that the definition of T0_TCR has the volatile attribute. Otherwise
> the compiler may optimize away the first line.
>
> --
>
> Timo
>

It seems that the timer needs to be enabled for a reset to take place.
So changing
T0_TCR = 0x00000002;
to
T0_TCR = 0x00000003;

appears to have rectified my problem.


Memfault Beyond the Launch