🎁📬❄️📦 Holiday Shipping Deadlines are approaching! International customers and domestic ground - get your orders in 12/9/2020 📦❄️📬🎁
0

_delay_ms() crashing?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

_delay_ms() crashing?

by mojo on Thu Mar 27, 2008 9:12 pm

I am having a problem with the following code. Whenever I insert a call to _delay_ms() or _delay_loop_2() the AVR freezes:

Code: Select all | TOGGLE FULL SIZE
while(1)
{
      t = 1 << z;
      z++;
      if (z>7) {z=0;}
      PORTC = t && 0b00111111;
      j = 0b00010001;
      if (t && 0b01000000) {j &= 0b00000001;}
      if (t && 0b10000000) {j &= 0b00010000;}
      PORTD = j;
      for (j=0; j<200; j++)
      {
         _delay_ms(1);
      }
      PORTD = 0b00010001;
}


Is it a bug in avr-libc? My code is using the AVR-USB stuff, maybe it interferes with it.

mojo
 
Posts: 136
Joined: Mon Jan 21, 2008 5:04 pm

by adafruit on Thu Mar 27, 2008 9:43 pm

i know you need to have the frequency defined somehow
maybe thats the problem

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

by mojo on Fri Mar 28, 2008 9:30 am

I did check and the frequency is defined... However, I am now getting some very strange results with normal code too. Here is my loop:

Code: Select all | TOGGLE FULL SIZE
uint8_t   i, j, k;

for(;;)
{
   PORTC = displaycounter;
   displaycounter++;
   j = 100;
   while(--j)
   {
      i = 100;
      while(--i)
      {
         k = 100;
         while(k--);
      }
   }
}



It freezes and does not loop... I am totally stumped. Interrupts are not enabled.

mojo
 
Posts: 136
Joined: Mon Jan 21, 2008 5:04 pm

by mojo on Fri Mar 28, 2008 9:40 am

I found the problem!

I looked at the assembler code for my loop, and it seems that the optimiser had optimised it to nothing. Inserting a NOP fixed it. It looks like the same thing happens with the _delay_ms() function - it's optimised into oblivion.

Worryingly, the optimisation seems to happen even if I optimise for size rather than performance :(

mojo
 
Posts: 136
Joined: Mon Jan 21, 2008 5:04 pm

by adafruit on Fri Mar 28, 2008 3:01 pm

_delay_ms() shouldnt be optimized out so im surprised by that. the code should have been declared 'volatile' but then again i havent actually verified that

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Please be positive and constructive with your questions and comments.