[Technology] Why the microcontroller C programming language, sometimes free to modify a variable

Recently a project which, in KEIL using C language in which the definition of a single-chip state machine global variable that will change, for switching interface touch screen,

But the program is running there is a problem, the state machine number is always a problem is baffling change, resulting in a normal touch screen can not jump to the default interface,

After inspection and analysis, there may be two reasons:

•) global variables and local variables of the same name duplicate definitions. There may be a local variable you define global variables with the same name, and modify the values ​​of variables in the subroutine inside.

•) the lack of volatile modifier. C language volatile modifier is not used, its role is to clear the variable is changed to prevent the compiler to optimize the variable, so that every time the processor reads data from memory at runtime, rather than register backup data. This command is recommended only for global modification plays an important role in key variables.

Usage: When you define: volatile unsigned char a; // defines a character variable a

The inspection is the second reason, add the volatile modifier after the problem is solved when you define a variable in the program.

My concern, shared technology development, efficient thinking, financial and other bits and pieces. Micro-channel public number: yonkotech

Guess you like

Origin www.cnblogs.com/yonkotech/p/12457776.html