This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

how to use volatile ,and why to use volatile ,and where we shoud use volatile !

thank you !
my friends!

Parents Reply Children
  • If a variable can change for any reason other than being manipulated by a local bit of code, it should be declared volatile.

    Common reasons for this sort of behavior include hardware registers that update whether or not the code touches them, hardware where the actual read or write cycle causes some operation to take place, global variables shared with ISRs (local code has no idea whether an ISR occurs and changes the variable, just as it wouldn't know if hardware updated itself), and memory shared between different processes.

    The volatile keyword essentially forces the compiler to re-read the value of a variable, rather than using an older value might still have in registers.