delay function not evaluating properly inside loop condition
Mauvai
Posts: 45
Using the following code:
I expect the code '//do stuff' to execute (assuming it was actual code), and then delay until the loop has run for a full 2 seconds (i.e. CLKFREQ*2) - but it doesnt - as far as i can tell it doesn't even enter the loop (tested by printing text from inside it - never happened).
What's interesting is that if i uncomment the print line, and print CNT-t and dt, not only can i see that CNT-t is much smaller than dt (and thus the loop should be entered), but the loop now enters, and the code behaves as expected.
I suspect that CNT is not being rechecked on each iteration of the loop, though really I have no idea. Ideas?
int dt, t; dt = CLKFREQ*2; while(1) { t = CNT; //do stuff //print("\n%d, %d", CNT-t, dt); while(CNT-t < dt) { pause(1); } }
I expect the code '//do stuff' to execute (assuming it was actual code), and then delay until the loop has run for a full 2 seconds (i.e. CLKFREQ*2) - but it doesnt - as far as i can tell it doesn't even enter the loop (tested by printing text from inside it - never happened).
What's interesting is that if i uncomment the print line, and print CNT-t and dt, not only can i see that CNT-t is much smaller than dt (and thus the loop should be entered), but the loop now enters, and the code behaves as expected.
I suspect that CNT is not being rechecked on each iteration of the loop, though really I have no idea. Ideas?
Comments
It was my understanding that CNT is internally stored as an unsigned long. Is this wrong?
//print("\n%d, %d", CNT-t, dt);
makes the code behave exactly as expected. I dont think the issue is quite this simple.
There is quite a bit of code... the dofSetup function works, so I'm assuming it can be ignored...
/the above works for me too. However i suspect that if you comment out the line that prints CNT and t (include a different print, for instance), i suspect it wont work -it doesn't for me
For instance, the lines:
Will prevent it from working correctly, but if I comment out any one of them - it doesn't matter which - it works perfectly. I have no earthly idea why this would happen.
I'll look at the code a bit more to see if I can find the problem.