Ask It!
Home > Ask It! > Industry > Computers

C-What is the meaning of the volatile

void main (void) {volatile int i; int j; i = 1; / / 1 is not optimized i = 1 i = 2; / / 2 is not optimized i = 1 i = 3; / / 3 i are not optimized = 1 j = 1; / / 4 is optimized for j = 2; / / 5 is optimized for j = 3; / / 6 j = 3} What does it mean that in the end ah

Posted on 2014-07-28 02:34:42

All Answers


volatile volatile are intended to be examples to explain the general two different ways - 1. "transient"; 2. "variable." Both claims are feasible. But what exactly is the meaning of volatile, is an example (in Keil-c and a51 Case examples from Keil FQA), you should understand after reading the example of the meaning of a volatile, if not understand, they had to go over the. Cases 1.void main (void) {volatile int i; int j; i = 1; / / 1 is not optimized i = 1i = 2; / / 2 is not optimized i = 1i = 3; / / 3 are not optimized i = 1j = 1; / / 4 is optimized for j = 2; / / 5 is optimized for j = 3; / / 6 j = 3 }------------------- -------------------------------------------------- Example 2. Function: void func (void) {unsigned char xdata xdata_junk; unsigned char xdata * p = & xdata_junk; unsigned char t1, t2; t1 = * p; t2 = * p;} compile the assembly is: 0000 7E00 R MOV R6, # HIGH xdata_junk0002 7F00 R MOV R7, # LOW xdata_junk ;---- Variable 'p' assigned to Register 'R6/R7' ---- 0004 8F82 MOV DPL, R70006 8E83 MOV DPH, R6 ;!!!! !!!!!!!!!!!!!!!!!!!!!!!!! Note 0008 E0 MOVX A, @ DPTR0009 F500 R MOV t1, A000B F500 R MOV t2, A ;!!!! !!!!!!!!!!!!!!!!!!!!!!!!! 000D 22 RET will function becomes: void func (void) {volatile unsigned char xdata xdata_junk; volatile unsigned char xdata * p = & xdata_junk; unsigned char t1, t2; t1 = * p; t2 = * p;} compiled assembly is: 0000 7E00 R MOV R6, # HIGH xdata_junk0002 7F00 R MOV R7, # LOW xdata_junk ;---- Variable ' p 'assigned to Register' R6/R7 '---- 0004 8F82 MOV DPL, R70006 8E83 MOV DPH, R6 ;!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!! 0008 E0 MOVX A, @ DPTR0009 F500 R MOV t1, A a Department 000B E0 MOVX A, @ DPTR000C F500 R MOV t2, A ;!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!! 000E 22 RET comparison can be seen, not with the volatile keyword, only referred to from the * p address read at once as in a change in the contents of * p, then t2 get is not really * p content. -------------------------------------------------- ------------------- cases 3volatile unsigned char bdata var; / / use volatile keyword heresbit var_0 = var ^ 0; sbit var_1 = var ^ 1; unsigned char xdata values [ 10]; void main (void) {unsigned char i; for (i = 0; i sizeof (values); i + +) {var = values [i]; if (var_0) {var_1 = 1; / / a at the values [ i] = var; / / without the volatile keyword, the compiler / / assumes that 'var' is unmodified and does not / / reload the variable content.}}} In this case, as in a place before running the next one , var will not be subject to change, such as var = 0xff; in values [i] = var; received or values [i] = 1 ;----------------- -------------------------------------------------- - Application examples: Example 1. # define DBYTE ((unsigned char volatile data *) 0) Description: here without volatile keyword, the content may not be true. -------------------------------------------------- ------------------- Example 2. # define TEST_VOLATILE_C //*********************** ****************************************// verwendete Include Dateien //*** ************************************************** **********# if __C51__ 600 # error:!! Keil wrong version # endif //*********************** ****************************************// function void v_IntOccured (void) / / ************************************************** ************* extern void v_IntOccured (void );//**************************** ***********************************// variable definition //********* ************************************************** **** char xdata cvalue1; / / global xdatachar volatile xdata cvalue2; / / global xdata //**************************** ***********************************// function: v_ExtInt0 () / / version: / / Parameters: / / Purpose: cvalue1 + +, cvalue2 ++//***************************************** ********************** void v_ExtInt0 (void) interrupt 0 {cvalue1 + +; cvalue2 + +;} //************* ************************************************** / / Function: main () / / version: / / Parameters: / / Purpose: Test volatile //*************************** ************************************ void main () {char cErg; / / 1. so cErg = cvalue1; cErg = cvalue1; / / 2. In the simulation here to manually interrupt INT0, so cvalue1 + +; cvalue2 + + if (cvalue1! = cErg) v_IntOccured ();// 3. so cErg = cvalue2; cErg = cvalue2 ; / / 4. In the simulation here to manually interrupt INT0, so cvalue1 + +; cvalue2 + + if (cvalue2! = cErg) v_IntOccured (); / / 5. to complete while (1 );}//***** ************************************************** ********// function: v_IntOccured () / / version: / / Parameters: / / Usage: loop //******************* ******************************************** void v_IntOccured () {while (1);} simulation can be seen without using volatile, that is two, the program can not enter v_IntOccured (); but in the four to enter v_IntOccured ();

2014-07-28 02:34:41
<<Previous
1
Next>>