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

Volatile declaration

Should I be using the "volatile" declaration with the Keil C compiler? It hasn't made a difference so far - as far as I can tell (which may or may not mean anyting). The manuals make no reference to it one way or the other.

Parents
  • "Any variable which can be changed by something outside of a very narrow "local" view of the immediate code should be declared volatile. This includes variables shared with other tasks and variables shared with interrupt handlers, as well as addresses that overlay hardware addresses and might be updated by the hardware."

    There are additional considerations when preemptible task switching and interrupts are used. In these situations volatile will not help (but won't necessarily hurt). When manipulating variables of more than one byte in size, you (the coder) have to guaranty mutually exclusive access, since the MCU can't modify multi-byte data objects atomically. Even with 1-byte data objects, you've got to know whether the manipulation is atomic by examining the compiler output.

Reply
  • "Any variable which can be changed by something outside of a very narrow "local" view of the immediate code should be declared volatile. This includes variables shared with other tasks and variables shared with interrupt handlers, as well as addresses that overlay hardware addresses and might be updated by the hardware."

    There are additional considerations when preemptible task switching and interrupts are used. In these situations volatile will not help (but won't necessarily hurt). When manipulating variables of more than one byte in size, you (the coder) have to guaranty mutually exclusive access, since the MCU can't modify multi-byte data objects atomically. Even with 1-byte data objects, you've got to know whether the manipulation is atomic by examining the compiler output.

Children