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

Timer0 with delay function

Hi,

I am using MCB2300 board with LPC2387.
I want to toggle my LEDs with delay from 20 to 30 microsecond.
I found function for delay in millisecond and microsecond from its sample code.
below is the function for microsecond.

 void delayus(BYTE timer_num, DWORD delayInus)
{
  if ( timer_num == 0 )
  {
        /*
        * setup timer #0 for delay
        */
        T0TCR = 0x02;           /* reset timer */
        T0PR  = 0x00;           /* set prescaler to zero */
        T0MR0 = delayInus * (Fpclk - 1);
        T0IR  = 0xff;           /* reset all interrrupts */
        T0MCR = 0x04;           /* stop timer on match */
        T0TCR = 0x01;           /* start timer */

        /* wait until delay time has elapsed */
        while (T0TCR & 0x01);
  }

Below is the function of millisecond.

void delayMs(BYTE timer_num, DWORD delayInMs)
{
  if ( timer_num == 0 )
  {
        /*
        * setup timer #0 for delay
        */
        T0TCR = 0x02;           /* reset timer */
        T0PR  = 0x00;           /* set prescaler to zero */
        T0MR0 = delayInMs * (Fpclk / 1000-1);
        T0IR  = 0xff;           /* reset all interrrupts */
        T0MCR = 0x04;           /* stop timer on match */
        T0TCR = 0x01;           /* start timer */

        /* wait until delay time has elapsed */
        while (T0TCR & 0x01);
  }

the problem is when I use delay function of Microsecond in main function for delay, I write delayus(0,20) for delay of 20 Microsecond
But led blinks at every 20 seconds instead of every 20 Microsecond.

So I think this function is work for delay of second not for Microsecond.

while the function of Millisecond is working properly.

So let me know am i doing something wrong?

Thanks

Parents
  • Fpclk represents frequency fed to the timer. Frequency is measured in Hz, which is the same as periods per second.

    So after that number of ticks, one second have passed.

    Your code for ms delays looks like:

    delayInMs * (Fpclk / 1000-1);
    


    Divides Fpclk with thousand. That is quite reasonable since one ms is one thousands of a second.

    Your code for us delays looks like:

    T0MR0 = delayInus * (Fpclk - 1);
    

    Are you surprised that your microsecond delays results in second delays?

    By the way - you don't need to setup everything in the timer. You can prepare the timer beforehand and just reset/activate it. Or you can keep it free-running and just sample the value when you enter the function and then in a loop sample again and return when current sample - initial sample is >= the required number of ticks for your delay.

    And since a timer overflows a 32-bit counter, a timer running at 1MHz can measure in 1us granularity while still being able to handle delays of 4^32/1000000 or about 4000 seconds. So you can perform both us, ms and seconds delays using the same timer while sharing the same initialization.

    Are you aware that there are ways for the timers to automagically toggle a pin every x us - or actually even faster than that depending on used crystal frequency and configured PLL? All with zero involvement of your program. The compare/match registers are quite capable.

Reply
  • Fpclk represents frequency fed to the timer. Frequency is measured in Hz, which is the same as periods per second.

    So after that number of ticks, one second have passed.

    Your code for ms delays looks like:

    delayInMs * (Fpclk / 1000-1);
    


    Divides Fpclk with thousand. That is quite reasonable since one ms is one thousands of a second.

    Your code for us delays looks like:

    T0MR0 = delayInus * (Fpclk - 1);
    

    Are you surprised that your microsecond delays results in second delays?

    By the way - you don't need to setup everything in the timer. You can prepare the timer beforehand and just reset/activate it. Or you can keep it free-running and just sample the value when you enter the function and then in a loop sample again and return when current sample - initial sample is >= the required number of ticks for your delay.

    And since a timer overflows a 32-bit counter, a timer running at 1MHz can measure in 1us granularity while still being able to handle delays of 4^32/1000000 or about 4000 seconds. So you can perform both us, ms and seconds delays using the same timer while sharing the same initialization.

    Are you aware that there are ways for the timers to automagically toggle a pin every x us - or actually even faster than that depending on used crystal frequency and configured PLL? All with zero involvement of your program. The compare/match registers are quite capable.

Children