In embedded system, we usually use the keyword "volatile", what does the keyword mean?
Analysis
The main purpose of the volatile modifier is to prompt the compiler that the value of the object may be changed without the compiler being monitored, so the compiler cannot arbitrarily optimize the code for that object.
A simple compiler optimizer
1) Read the value of variable a into register X, as long as the X value is not changed, and later need to use the value of a to read from X
The role of volatile
1) Each time you need to use the value of a, you cannot read from X, you must read from a
The following description of volatile is available in the AVR Help documentation:
Declaring OBJECTS VOLATILE
There is three main reasons for declaring an object volatile:
Shared access; The object is gkfx between several tasks in a multitasking environment
Trigger access; As for a memory-mapped SFR where the fact of an access occurs have an effect
Modified access; Where the contents of the object can change in ways not known to the compiler.
Two examples of volatile variables
1) Variables shared by several tasks in multi-threaded applications
2) non-automatic variables that are accessed in an interrupt service subroutine
A question of volatile