gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Weird while loop problem

#20258 - kayan - Wed May 05, 2004 7:41 pm

I took this function from dovotos site and the while loops forever if it is empty. As soon if I put something like sprintf it works.

The code how I would like it to be. but doesnt' work
Code:

void sleep(int seconds)
{
    //Start the timer
    REG_TM3CNT = TIMER_FREQUENCY_1024 | TIMER_ENABLE;
    //zero the timer
    REG_TM3D = 0;

    while(seconds--)
    {
        while(REG_TM3D <= 16386){}   //wait
        REG_TM3D = 0; //reset the timmer
    }
    REG_TM3CNT = 0;
}


if I add a sprintf like this it works:
Code:

while(REG_TM3D <= 16386)
{
     sprintf(str,"ab");
}


Is anybody had this weird problem? Is it a compilier bug or something?

Thank you

#20259 - niltsair - Wed May 05, 2004 7:46 pm

Make sure REG_TM3D is declared as Volatile. All register should be declared as being Volatile.
_________________
-Inside every large program is a small program struggling to get out. (Hoare's Law of Large Programs)
-The man who can smile when things go wrong has thought of someone he can blame it on. (Nixon's Theorem)


Last edited by niltsair on Wed May 05, 2004 7:47 pm; edited 1 time in total

#20260 - alek - Wed May 05, 2004 7:46 pm

kayan wrote:
I took this function from dovotos site and the while loops forever if it is empty. As soon if I put something like sprintf it works.

The code how I would like it to be. but doesnt' work
Code:

void sleep(int seconds)
{
    //Start the timer
    REG_TM3CNT = TIMER_FREQUENCY_1024 | TIMER_ENABLE;
    //zero the timer
    REG_TM3D = 0;

    while(seconds--)
    {
        while(REG_TM3D <= 16386){}   //wait
        REG_TM3D = 0; //reset the timmer
    }
    REG_TM3CNT = 0;
}




Don't know if this will make any difference but have you tried replacing the empty {} with ;
Code:

while(REG_TM3D <= 16386);

#20261 - kayan - Wed May 05, 2004 8:01 pm

alek: no, I tried but it doesn't work

niltsair: you were right it wasn't declared as volatile. Thanks.


Now it's working.

#20266 - niltsair - Wed May 05, 2004 8:34 pm

Just so you know, Volatile means that the variable can be modified outside of your current code (namely by hardware or code inside interrupt). Failing to specify Volatile for register means that the compiler will 'Optimize' in unpleasant ways.
_________________
-Inside every large program is a small program struggling to get out. (Hoare's Law of Large Programs)
-The man who can smile when things go wrong has thought of someone he can blame it on. (Nixon's Theorem)

#20269 - kayan - Wed May 05, 2004 9:31 pm

Now it make sense why the while loop was doing something weird.

Thank you for the explanation.

#20309 - sgeos - Thu May 06, 2004 9:23 pm

kayan wrote:
Now it make sense why the while loop was doing something weird.


The while loop was not really doing anything weird. The compiler just did something unexpected.

-Brendan