avr-gcc-list
[Top][All Lists]
Advanced

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

Re: [avr-gcc-list] Problem with delay loop


From: David Brown
Subject: Re: [avr-gcc-list] Problem with delay loop
Date: Fri, 28 Sep 2007 11:17:26 +0200
User-agent: Thunderbird 2.0.0.6 (Windows/20070728)


This is probably in the FAQ somewhere - if not, it should be!

The compiler is smart enough to figure out that your delay function does no useful work - thus the optimiser does not generate any code. This is correct compilation - it's your code that is wrong. The difference is that the newer version of the compiler is smarter than the older version (or newer makefiles have higher optimisation enabled by default).

The correct way to write such a loop is:

void delay(unsigned int del_cnt) {
        volatile unsigned int n = del_cnt;
        while (n--);
}

(Incidentally, *never* use "unsigned" as a shorthand - write the proper type name "unsigned int". The same applies to "long", "short", "signed", etc.)

If you don't know how to use "volatile", there should be plenty of references on the web (including the archives for this mailing list), which will give you a better explanation.

mvh.,

David


Royce Pereira wrote:
Hi all,

In the latest WinAVR (avr-gcc (GCC) 4.1.2 (WinAVR 20070525) I found this.

Check this out:
//======================
void delay(unsigned del_cnt)
    {
       while(del_cnt--);

       return;
    }
//=======================

Compiles as (from the .lss file):

//=======================
void delay(word cnt)
    {
  2aa:  08 95           ret

       while(cnt--);

       return;
    }
//=======================

The loop is not executed !!

Where as in an older version (WinAVR-20060421)

It compiles correctly as:
//==========================================
void delay(word cnt)
    {
       while(cnt--);
  286:  01 97           sbiw    r24, 0x01       ; 1
  288:  2f ef           ldi     r18, 0xFF       ; 255
  28a:  8f 3f           cpi     r24, 0xFF       ; 255
  28c:  92 07           cpc     r25, r18
  28e:  d9 f7           brne    .-10            ; 0x286 <delay>
  290:  08 95           ret


       return;
    }
//=======================================

As a result none of my delays used in my older programs work when recompiled...

Thanks,

--Royce






reply via email to

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