2

I have a flag (as a global variable) which is waiting for a certain time to pass before being set. I have a while loop which waits for the flag to set before proceeding. The global variable does get set to '1' but the while loop does not exit, any ideas what I'm doing wrong:

while (TC3Flag == 0); //Global Flag Variabe TC3Flag, 0 = Not Set, 1 = Set
TurnOnFive();
TurnOnTwelve();
TC3Flag = 0;

Edit i also have tried with the same results, I do want the processor to do nothing while the flag is 0 and only call TurnOnFive and TurnOnTwleve after the flag is set

    while (TC3Flag == 0) {}
    TurnOnFive();
    TurnOnTwelve();
    TC3Flag = 0;

EDIT EDIT: Adding volitile to the TC3FLag declaration fixed it.

David Gash
  • 41
  • 1
  • 4

1 Answers1

11

Unless TC3Flag is marked as volatile, the compiler can aggressively optimize the loop away (or just assume it's an infinite loop). You should mark TC3Flag as volatile in order to force the compiler to read the value at its memory address on every iteration.

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416