Volatile instance description

Source: Internet
Author: User
Volatile is intended to have two types of statements: 1. "transient" and 2. "changeable ".
Both of them are feasible. But what does volatile mean? An example is given (taking Keil-C and A51 as examples ).
The example is from Keil fqa). After reading the example, you should understand the meaning of volatile. If you still don't understand it, you have
Read it again.

Example 1.

Void main (void)
{
Volatile int I;
Int J;

I = 1; // 1 not optimized I = 1
I = 2; // 2 is not optimized I = 1
I = 3; // 3 not optimized I = 1

J = 1; // 4 optimized
J = 2; // 5 optimized
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;
}

The compiled assembly is:

0000 7e00 R mov R6, # High xdata_junk
0002 7f00 R mov R7, # Low xdata_junk
; ---- Variable 'P' assigned to register 'r6/r7 '----

0004 8f82 mov DPL, r7
0006 8e83 mov DPH, R6

;!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Note:
0008 E0 movx A, @ dptr
0009 F500 R mov T1,

000b F500 R mov T2,
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
000d 22 RET

Change the function:
Void func (void)
{
Volatile unsigned char xdata xdata_junk;
Volatile unsigned char xdata * P = & xdata_junk;
Unsigned char T1, T2;

T1 = * P;
T2 = * P;
}

The compiled assembly is:
0000 7e00 R mov R6, # High xdata_junk
0002 7f00 R mov R7, # Low xdata_junk
; ---- Variable 'P' assigned to register 'r6/r7 '----

0004 8f82 mov DPL, r7
0006 8e83 mov DPH, R6

;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
0008 E0 movx A, @ dptr
0009 F500 R mov T1, a

000b E0 movx A, @ dptr
000c F500 R mov T2,
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

000e 22 RET

The comparison result shows that when the volatile keyword is not used, only the read from the address indicated by * P is performed once.
For example, if the content of * P changes at a, what T2 obtains is not the actual * P content.

---------------------------------------------------------------------
Example 3

Volatile unsigned char bdata var; // use volatile keyword here
Sbit 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; //

Values [I] = var; // without the volatile keyword, the compiler
// Assumes that 'var' is unmodified and does not
// Reload the variable content.
}
}
}

In this example, for example, if var does not change before running in a to the next sentence, for example, Var = 0xff; then in
Values [I] = var; returns values [I] = 1;

---------------------------------------------------------------------
Example:

Example 1.
# Define dbyte (unsigned char volatile data *) 0)

Note: The volatile keyword is not used here, and real content may not be obtained.
---------------------------------------------------------------------

Example 2.

# Define test_volatile_c

//************************************** *************************
// Verwendete include dateien
//************************************** *************************
# If _ c5__ <600
# Error :!! The Keil version is incorrect.
# Endif

//************************************** *************************
// Function void v_1_ccured (void)
//************************************** *************************
Extern void v_1_ccured (void );

//************************************** *************************
// Variable definition
//************************************** *************************
Char xdata cvalue1; // global xdata
Char 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. Make CERG = cvalue1;
CERG = cvalue1;

// 2. During simulation, int0 is manually interrupted, so that cvalue1 ++ and cvalue2 ++
If (cvalue1! = CERG)
V_1_ccured ();

// 3. Make CERG = cvalue2;
CERG = cvalue2;

// 4. During simulation, int0 is manually interrupted, so that cvalue1 ++ and cvalue2 ++
If (cvalue2! = CERG)
V_1_ccured ();

// 5. Complete
While (1 );
}

//************************************** *************************
// Function: v_1_ccured ()
// Version:
// Parameters:
// Purpose: endless loop
//************************************** *************************
Void v_1_ccured (){
While (1 );
}

Simulation shows that when volatile is not used, that is, 2 places, the program cannot enter v_1_ccured ();
However, you can enter v_intoccured () at four points ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.