Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Unable to get the timer working correctly in LPC2148

Status
Not open for further replies.

shankariamma

Newbie level 1
Joined
Apr 6, 2015
Messages
0
Helped
0
Reputation
0
Reaction score
0
Trophy points
0
Activity points
9
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 (Did 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 whenever needed? Which one is efficient? I felt clearing T0TC might be efficient since it takes only one instruction cycle.

Code:
// 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);
}
 

Hi Shankariamma,

In the timer initialization function i.e void initTimer0(void) the PRESCALE value should be 15 and not 150 if you want delay in microseconds.

change the abovementioned line to T0PR = 15-1;

Check these calculations for more clarity reference ocrfreaks.com:


Prescale (TxPR) Related Calculations:

The delay or time required for 1 clock cycle at ‘X’ MHz is given by :
(1 / X * (10^6)) Seconds

Hence in our case when PR=0 i.e TC increments at every PCLK the delay required for TC to increment by 1 is:
((0+1) / 60 * (10^6)) Seconds

Similarly when we set PR = 59999 the delay in this case will be:
((59999+1) / 60 * (10^6)) = (60000 / 60 * (10^6) = 1 / (10^3) Seconds

… which boils down to 1/1000 = 0.001 Seconds which is nothing but 1 Milli-Second i.e mS. Hence the delay required for TC to increment by 1 will be 1mS.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top