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

Delay

hi
i want delay in ms and us.am using LPC2366.CPU clock: 48MHz.peripheral clock: 12MHz.can any one have source code for delay function.please help me to know how to implement delay function in ms and us.Thanks i advance.

Parents Reply Children
  • No, you misunderstood me. I wasn't talking about that short times there, but was talking about constantly polling the timer.

    When you need really short delays, it normally works quite ok with a number of consecutive __nop();

    In cases where you really need a delay loop in assembler, then the code before/after the loop should most probably be in assembler too, to avoid the extra time spend entering/leaving the assembler function. Inline assembler is seldom a good idea.

  • Counting clock cycles in assembler is most definitely affected by the clock speed of the chip, or waitstates of flash, or if caching of flash is done or not.

    When the delays are long enough that the function call and timer setup times don't affect the total result too much, they most definitely represent a big advantage. When the delay is long enough that you can wait for an interrupt, instead of busy-polling the timer, you can even conserve power by putting the CPU to sleep.

    In short: measuring time with known assembler instructions is better than with an unkonwn C loop. But measuring time with a timer - when possible - solves a lot of problems.

    Making life black or white is a boring way to live. There is quite a lot of shades in between too...

  • Making life black or white is a boring way to live. There is quite a lot of shades in between too...
    I agree that there is more than one way to skin this cat, timer or assembler delay loop.
    I also agree that, when long enough, delay by timer is preferrable.
    HOWEVER I must insist that a delay loop in C is "delay some unknown time"
    The only C delay loop I would consider (although ridiculous) acceptable would be to figure out how many cycles the most efficient compiler would use and apply that where a delay is only specified as "not less than" and longer delay than minimum is no problem.

    a footnote on asembler delays
    that should ALWAYS have the following
    $IF (clock == xxx)
    $else
    change the code here
    $endif

    Erik