PROGRAMMING PIC MICROCONTROLLERS IN C: STATIC VARIABLES AND VOLATILE VARIABLES

STATIC VARIABLES

Static variables are usually used in functions. A static variable can only be accessed from the function in which it was declared. The value of a static variable is not destroyed on exit from the function, instead its value is preserved and becomes available again when the function is next called. Static variables can be initialized like other normal variables, but the initialization is done once only when the program starts. Static variables are declared by preceding them with the keyword static. For example,

static int sum;

VOLATILE VARIABLES

A variable should be declared volatile whenever its value can be changed by something beyond the control of the program in which it appears, such as an interrupt service routine. The volatile qualifier tells the compiler that the value of the variable may change at any time without any action being taken by the code the compiler finds nearby. All I/O based variables and variables shared by the main code and interrupt service routines should be declared volatile. In the following example, the variable count is declared volatile:

volatile int count;

Leave a comment

Your email address will not be published. Required fields are marked *