Unable to get the timer working correctly in LPC2148

I have written code for delay using timer in LPC2148. But I am not able to get the desired time delay. 1 Sec delay if I give, it looks like 100 msec (Ddd not calculate correctly). Somewhere something is wrong it looks. Anybody can help? I have written code comments and I hope it is good enough. Also is it good idea to disable and enable timer as in delayus() or allow timer to run continuously instead reset the T0TC? Which one is efficient? I felt clearing T0TC might be efficient since it takes only one instruction cycle.

// Timer resolution is 10 micro Second with PCLK = 15 MHz.

void initTimer0(void) { /*PLL0 has been setup with CCLK = 60Mhz and PCLK = 15Mhz.*/ // 150 count @15 MHz means 10 usec. So TC gets incremented every 10 usec

T0CTCR = 0x0; // Select the timer mode. T0PR = 150-1; //(Value in Decimal!) - Increment T0TC at every 150 clock cycles

//Count begins from zero hence subtracting 1

T0TCR = 0x02; //Reset Timer }

// Function below is used to give a delay in micro seconds. // Delay in 10 usec multiples is possible void delayus(unsigned int microSecond) //Using Timer0 { // In case the value is not multiple of 10, make it multile of 10 by adding the difference

if((microSecond % 10)!=0) { microSecond = microSecond + (10-microSecond%10); }

T0TCR = 0x02; //Reset Timer

T0TCR = 0x01; //Enable timer // Reset the Timer counter

// T0TC=0; // Need not reset since we are disabling the timer itself and then enabling

// Probably we need not disable timer, instead we could reset this TOTC.

while(T0TC < (microSecond/10)); //PR is loaded such that TC updates every 10 microsecond.

// So the T0TC count value required = microsecond/10

T0TCR = 0x00; //Disable timer }

// The following function gives delay in milliseconds. void delayMS(unsigned int milliseconds) //Using Timer0 { delayus( milliseconds*1000); }

--------------------------------------- Posted through

formatting link

Reply to
shankariamma
Loading thread data ...

Am 07.04.2015 um 13:34 schrieb shankariamma:

What do you mean "looks like"? How about you sit down and make an actual _measurement_ of your delay?

In the majority of situations, you need neither. You can just leave the timer running in the background.

How many ticks of that size are in one whole second? Are you sure the time can really count all that far?

Have you even tried any delays shorter than 1 second? E.g. is there a difference between a single delayms(1000) and, say, 10 calls of delayms(100), or 100 calls of delayms(10)?

And what exactly, do you think, will the rest of your hardware think of your CPU doing _nothing_at_all_, for an entire second? Might there be, dunno, a "watchdog" out there that would forbid such things?

The above is quite a complete waste of effort.

Sure that's actually true, i.e. that the timer doesn't retain its previous value and starts from there?

Reply to
Hans-Bernhard Bröker

Hello, Appreciate your time in going through the code. I am new to Embedded Programming. It would be good if you give suggestions as well in addition to asking only questions. If I am expert programmer, why should I ask these simple questions?

I want 10 usec or 100 usec resolution. But not able to get this done.

--------------------------------------- Posted through

formatting link

Reply to
shankariamma

No one is going to write your code for you (unless you hire them as a consultant, of course). This is especially true when you are a beginner

- it is important that you /learn/ what you are doing, not just copy it from someone else without understanding. That is why Hans-Bernhard took the time to ask you important questions, designed to make you think about the code and perhaps fix things yourself.

I can give you one further suggestion - assuming your timer is 32-bit (/you/ will have to check this), run it at 1 MHz to count microseconds. Then you can drop all the messy and inefficient divide by 10 code. I recommend you also calculate the limit time of any counter, to be sure that it can handle any delay you might need - post your answer here and we can confirm it for you.

Also, if you are going to use Usenet, please get yourself a proper newsreader and newsserver instead of the crappy "EmbeddedRelated" interface. Then you can post properly, including quoting. For many people, Thunderbird is a fine choice of newsreader - it is easy to use, free, and works on all platforms.

formatting link
is a free newsserver that is very popular, as many ISP's no longer provide a Usenet server. A proper Usenet setup will make it easier for you to ask questions, and easier for others to answer you.

mvh.,

David

Reply to
David Brown

formatting link

Reply to
Tom Gardner

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.