Unable to get the timer working correctly in LPC2148

Discussion in 'Embedded Systems and Microcontrollers' started by Shankari Amma, Apr 6, 2015.

  1. Shankari Amma

    Thread Starter New Member

    Apr 6, 2015
    1
    0
    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.

    Code (Text):
    1.  
    2. // Timer resolution is 10 micro Second with PCLK = 15 MHz.
    3. void initTimer0(void)
    4. {
    5. /*PLL0 has been setup with CCLK = 60Mhz and PCLK = 15Mhz.*/
    6. // 150 count @15 MHz means 10 usec. So TC gets incremented every 10 usec
    7. T0CTCR = 0x0; // Select the timer mode.
    8. T0PR = 150-1; //(Value in Decimal!) - Increment T0TC at every 150 clock cycles
    9. //Count begins from zero hence subtracting 1
    10.  
    11.  
    12. T0TCR = 0x02; //Reset Timer
    13. }
    14.  
    15.  
    16.  
    17. // Function below is used to give a delay in micro seconds.
    18. // Delay in 10 usec multiples is possible
    19. void delayus(unsigned int microSecond) //Using Timer0
    20. {
    21. // In case the value is not multiple of 10, make it multile of 10 by adding the difference
    22. if((microSecond % 10)!=0)
    23. {
    24. microSecond = microSecond + (10-microSecond%10);
    25. }
    26.  
    27. T0TCR = 0x02; //Reset Timer
    28.  
    29. T0TCR = 0x01; //Enable timer
    30. // Reset the Timer counter
    31.  
    32. // T0TC=0; // Need not reset since we are disabling the timer itself and then enabling
    33. // Probably we need not disable timer, instead we could reset this TOTC.
    34.  
    35. while(T0TC < (microSecond/10)); //PR is loaded such that TC updates every 10 microsecond.
    36. // So the T0TC count value required = microsecond/10
    37.  
    38.  
    39. T0TCR = 0x00; //Disable timer
    40. }
    41.  
    42. // The following function gives delay in milliseconds.
    43. void delayMS(unsigned int milliseconds) //Using Timer0
    44. {
    45. delayus( milliseconds*1000);
    46. }
    47.  
    Moderators note : Please use code tags for pieces of code
     
    Last edited by a moderator: Apr 6, 2015
Loading...