This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Unable to get 1 msec timer working

I tried to configure timer for sec. But unable to get it working for 1 sec. Instead is somewhere between 2 and 3 sec. Below is the code. Do anyone see something wrong with the code?
Note: Crystal - 12 MHz. CCLK = 60 MHz (I verified this by reading PLL0STAT and it is 0x00000724). Also VPBDIV=0x00000000 after reading it, meaning PCLK = 15 MHz.

// Function to give 1 msec delay
void delayMS(unsigned int milliseconds) //Using Timer0
{
    T0TCR = 0x02; //Reset Timer

    T0TCR = 0x01; //Enable timer

    while(T0TC < milliseconds); //wait until timer counter reaches the desired delay

    T0TCR = 0x00; //Disable timer
}


// Initialize the Timer resolution for 1 msec with PCLK = 15 MHz.

void initTimer0For1msec(void)
{

        /*PLL0 has been setup with CCLK = 60Mhz and PCLK  = 15Mhz.*/

    T0CTCR = 0x0;  // Select the timer mode. By default timer mode is selected. Not required to do again.
                                        // But still to be on safer side.

        T0PR = 60000;  // Increment T0TC at every 60000 clock cycles
                   //60000 clock cycles @15Mhz = 1 msec
        T0TCR = 0x02; //Reset Timer. Both T0PC and T0TC are reset to zero until bit 2 is made 0 again.
}

Parents Reply Children