[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] How to calculate delay time

From: Peter N Lewis
Subject: Re: [avr-gcc-list] How to calculate delay time
Date: Tue, 2 Apr 2002 10:57:21 +0800

At 19:12 +0100 30/3/02, Karsten Becker wrote:

If i want to perform a certain delay, how do i calculate the number of iterations i need to do.
I mean, if i do
for (i=0;i<255;i++)
   for (j=0;j<255;i++)

What times do i need to add to 255*255Cycles of Nop()?
BTW where can i find a human-readable version of the resulting asm-code?

Firstly, the best way to do a delay is to use a timer.

In any event, you'll need to know your clock speed of your processor, since the timer normally runs off that, and any other delay will be proportional to the clock period.

If you are use a CPU with an internal oscillator, then the oscillator will probably not be very stable and you will have a hard time getting a reliable speed and hence a reliable delay.

Also, you need to be *very* careful trying to a do a delay in C, because the optimizer can easily see through your efforts to take a long time doing nothing and decide it would be ever so helpful to take a short time doing nothing.

Here is a routine I have lieing around:

// 8MHz
static void delay50us() {
  asm volatile (
    "L_%=: dec %0" "\n\t"
    "brne L_%=" "\n\t"
    : : "a" (133) );

That should work and should give you roughly a 50uS delay at 8MHz. If your clock speed is different, can you will need to adjust the 133 number (keep it inside a byte range!). You cna then build routines to get longer delays based on calling this one a number of times, eg:

static void delayms( uint16_t n ) {
  uint16_t i, j;

  for( i=0; i<n; i++ ) {
    for( j=0; j<20; j++ ) {
} // end delay

For best results, you'll need to calibrate it, set it up to delay for a long period (eg 60 seconds) and toggle a LED and repeat, and then adjust the 133 number based on a stopwatch time.


<http://www.interarchy.com/>  <ftp://ftp.interarchy.com/interarchy.hqx>
avr-gcc-list at http://avr1.org

reply via email to

[Prev in Thread] Current Thread [Next in Thread]