Hello, I have a problem with this code:
void test(void) { unsigned long time_start, time_diff; unsigned int x=2; time_start = timer_value; while (x<5) { time_diff = timer_value; if (time_diff-time_start >= 2ul) x=5; // printf("%X\n", time_diff-time_start); } }
You probably need to declare timer_value as volatile. - mike
Thanks a lot. Now it works... I'v tried the volatile-thingie before my post, but on timer_diff, not on timer_value *g* Torsten
timer_value needs to be declared volatile because it is the item that can be changed by something outside the scope of this routine -- that is, the timer hardware itself. Declaring timer_diff volatile does not help because this variable, unlike timer_value, is affected only by code in this procedure itself. timer_diff is likely optimized away completely in this routine.