+ Post New Thread
Results 1 to 2 of 2
  1. #1
    Newbie level 1
    Points: 14, Level: 1

    Join Date
    Apr 2015
    Posts
    0
    Helped
    0 / 0
    Points
    14
    Level
    1

    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 (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);
    }

    •   AltAdvertisement

        
       

  2. #2
    Newbie level 3
    Points: 567, Level: 5

    Join Date
    Apr 2014
    Posts
    4
    Helped
    0 / 0
    Points
    567
    Level
    5

    Re: Unable to get the timer working correctly in LPC2148

    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.



--[[ ]]--