Society of Robots - Robot Forum

Software => Software => Topic started by: Klinger on March 11, 2009, 06:10:37 AM

Title: While(1)
Post by: Klinger on March 11, 2009, 06:10:37 AM

I my program I have a few While(1) statement but they are not staying in a infinite loop. I don't see any way they are break out of the statement

could you some reason why they are not staying in a infinite loop. Or other ways I can make a infinite loop I have tried for(;;) but that still did not work.

Thanks
Title: Re: While(1)
Post by: paulstreats on March 11, 2009, 06:39:22 AM
what is your setup? which microcontroller and which C compiler.

can you post an example of your code
Title: Re: While(1)
Post by: tristantech on March 11, 2009, 07:50:13 PM
Your compiler might not use while(1)... if you use gcc/winavr it should. Try while(1==1). That has the same purpose and should work no mater what.
Title: Re: While(1)
Post by: chelmi on March 12, 2009, 09:05:03 AM
Your compiler might not use while(1)... if you use gcc/winavr it should. Try while(1==1). That has the same purpose and should work no mater what.

 ??? what do you mean "not use while(1)" ? assuming we are talking about C, if your compiler does not loop for ever with this syntax then it's a bug in the compiler.
Title: Re: While(1)
Post by: Trumpkin on March 12, 2009, 01:07:08 PM
Your code should look like this:
Code: [Select]
while(1)
{
code here
}

you could also try
Code: [Select]
for(;;)
{
code here
}
Title: Re: While(1)
Post by: sprince09 on March 13, 2009, 12:50:41 PM
Also make sure you don't have any interrupts running that somehow branch to a different part of your program.

Such as:

while(1){
      //main loop
}

void example_function(void){
    while(1){//do stuff
}

SIGNAL(interupt_vector){
    example_function();
}
Title: Re: While(1)
Post by: paulstreats on March 13, 2009, 03:56:06 PM
If you are using PICs make sure the watchdog timer is switched off
Title: Re: While(1)
Post by: Admin on March 19, 2009, 03:41:04 AM
Quote
I my program I have a few While(1) statement but they are not staying in a infinite loop. I don't see any way they are break out of the statement
If you are using AVR, make sure you turn off optimization in the makefile. An empty while loop would otherwise be deleted when compiled (as it is useless).
Title: Re: While(1)
Post by: chelmi on March 19, 2009, 08:42:42 AM
Quote
I my program I have a few While(1) statement but they are not staying in a infinite loop. I don't see any way they are break out of the statement
If you are using AVR, make sure you turn off optimization in the makefile. An empty while loop would otherwise be deleted when compiled (as it is useless).

Are you sure? Which compiler are you referring to? I just checked with gcc 3.2.3 on x86, with -O3 and -Os and as I would expect the while(1); loop was not removed.
I am very doubtful that any decent compiler would remove this statement, since this would change the execution flow of the program. A while(1); statement is NOT useless.

On the contraey, while(0); will probably be optimized out, since this is clearly useless.

Chelmi.
Title: Re: While(1)
Post by: dellagd on March 19, 2009, 06:21:25 PM
this is a bit more complex but it should work to as I see it:

Code: [Select]
code: //yes, I really want you to type "code"
//put your code here
goto code;

this should also be an infinite loop and should cancel out the chance of the compiler deleting the part of an infinite loop that makes it a infinite loop
Title: Re: While(1)
Post by: chelmi on March 20, 2009, 08:38:03 AM
this is a bit more complex but it should work to as I see it:

Code: [Select]
code: //yes, I really want you to type "code"
//put your code here
goto code;

this should also be an infinite loop and should cancel out the chance of the compiler deleting the part of an infinite loop that makes it a infinite loop

I know I sound repetitious, but no compiler will "delete the part of an infinite loop that makes it a infinite loop". I'm pretty sure his problem is not with the while(1) loop. Probably a bad interrupt handler or a watchdog timer, but without more info it's hard to tell.
Title: Re: While(1)
Post by: Webbot on March 21, 2009, 08:27:38 AM
I agree: the compiler wont get rid of a while(1){} but it would get rid of an empty for loop: eg  for(int i=0; i<100;i++){}  would get removed by optimisation.

Are you sure its jumping out of the loop - or is the microcontroller reseting and starting from the beginning again?
Title: Re: While(1)
Post by: frank26080115 on March 24, 2009, 09:23:10 AM
If it's an AVR chip, what are you doing inside this loop? make sure nothing touches your stack memory or else RET or RETI instructions will get confused (no out of bound array access and use free() for every malloc() and calloc()). Check the .lss file generated to see if the RJMP is correct and there's no possible path that can lead to the RJMP being skipped. If you can, run the code in the simulator. Also make sure your circuit is not noisy, a bad clock cycle might cause you to skip instructions.
Title: Re: While(1)
Post by: fiflak666 on March 24, 2009, 12:46:49 PM
Quote
I my program I have a few While(1) statement but they are not staying in a infinite loop. I don't see any way they are break out of the statement
If you are using AVR, make sure you turn off optimization in the makefile. An empty while loop would otherwise be deleted when compiled (as it is useless).

No way, while(1) is never useless and compiler that doesn't know this is a weak compiler ;) there is no other way to stop the C written program to step over a part of memory outside its border
Title: Re: While(1)
Post by: Admin on April 08, 2009, 05:59:46 AM
my bad ;D