The compiler might have optimized out your statements based on the non-volatile value. Once you define the volatile keyword, the portion of the code might not have been optimized out. Every global variable that is changed in an interrupt handler must be declared as volatile.
1 of 1 people found this helpful
For understanding what's happening, you can take a look at the assembly code diff below. .L9+4 holds the address of the systick variable. You can see a missing part in while(systick<100u) in non-volatile case. systick value is not continuously read. That is why, it gets stuck there.
Thanks for the link.
Again many thanks for the help here. My bad! I assumed variables declared outside of functions can't be optimized into registers.
I've been corrected.
You'd think that if the compiler found a variable declared outside a function and used in two different functions (especially if one of the functions was called in an interrupt service routine), it would default to volatile.
You're welcome Len.