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: Klaus Rudolph
Subject: Re: [avr-gcc-list] Problem with delay loop
Date: Fri, 28 Sep 2007 11:27:14 +0200
User-agent: Thunderbird 2.0.0.6 (Windows/20070728)

Please use optimizer! Something like -O2 -O3 -Os ... as you need!

Simplify your delay loop:
void delay(volatile word cnt) { ...

Have fun!


Royce Pereira schrieb:
Hi,

On Fri, 28 Sep 2007 14:47:26 +0530, David Brown <address@hidden> wrote:

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--);
}

I tried that and -Yikes !! Here's what I get :( :
Compare it to the short & sweet loop of the earlier avr-gcc version (below)...
//-------------------------------------------
void delay(unsigned int del_cnt)
    {
  2aa:  cf 93           push    r28
  2ac:  df 93           push    r29
  2ae:  cd b7           in      r28, 0x3d       ; 61
  2b0:  de b7           in      r29, 0x3e       ; 62
  2b2:  22 97           sbiw    r28, 0x02       ; 2
  2b4:  0f b6           in      r0, 0x3f        ; 63
  2b6:  f8 94           cli
  2b8:  de bf           out     0x3e, r29       ; 62
  2ba:  0f be           out     0x3f, r0        ; 63
  2bc:  cd bf           out     0x3d, r28       ; 61
       volatile unsigned int n = del_cnt;
  2be:  9a 83           std     Y+2, r25        ; 0x02
  2c0:  89 83           std     Y+1, r24        ; 0x01
       while(n--);
  2c2:  89 81           ldd     r24, Y+1        ; 0x01
  2c4:  9a 81           ldd     r25, Y+2        ; 0x02
  2c6:  01 97           sbiw    r24, 0x01       ; 1
  2c8:  9a 83           std     Y+2, r25        ; 0x02
  2ca:  89 83           std     Y+1, r24        ; 0x01
  2cc:  89 81           ldd     r24, Y+1        ; 0x01
  2ce:  9a 81           ldd     r25, Y+2        ; 0x02
  2d0:  8f 5f           subi    r24, 0xFF       ; 255
  2d2:  9f 4f           sbci    r25, 0xFF       ; 255
  2d4:  b1 f7           brne    .-20            ; 0x2c2 <delay+0x18>
  2d6:  22 96           adiw    r28, 0x02       ; 2
  2d8:  0f b6           in      r0, 0x3f        ; 63
  2da:  f8 94           cli
  2dc:  de bf           out     0x3e, r29       ; 62
  2de:  0f be           out     0x3f, r0        ; 63
  2e0:  cd bf           out     0x3d, r28       ; 61
  2e2:  df 91           pop     r29
  2e4:  cf 91           pop     r28
  2e6:  08 95           ret

000002e8 <wr_lcd_hi>:

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

Output of the older -WinAVR-20060421-version (and without the 'volatile' thingy 
too!)
//==========================================
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;
    }
//=======================================


Thanks,

--Royce.





reply via email to

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