📄 impdet23.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="GENERATOR" content="Quadralay WebWorks Publisher Professional Edition 6.0.5">
<meta name="TEMPLATEBASE" content="book_html">
<meta name="LASTUPDATED" content="12/04/03 16:06:26">
<title>13.9 volatile qualifier</title>
<STYLE TYPE="text/css">
<!--
span.Signal { text-transform: uppercase; font-family: Verdana }
-->
</STYLE>
</head>
<body link="#3366CC" vlink="#9999CC" text="#000000" alink="#0000CC" bgcolor="#FFFFFF"
background="images/backgrnd.gif">
<p><img src="images/stlogo.gif" width="106" height="83" align="left"
alt="logo here!"> </p>
<table width="331" border="0" align="right" cellpadding="0" cellspacing="0">
<tr>
<td><a href="a_refman.htm"><img src="images/navtoc.gif" width="84" height="23"
border="0" alt="TOC"> </a></td>
<td><a href="impdet22.htm"><img src="images/navprev.gif" width="80" height="23"
border="0" alt="PREV"> </a></td>
<td><a href="impdet24.htm"><img src="images/navnext.gif" width="83" height="23"
border="0" alt="NEXT"> </a></td>
<td><a href="a_refmaa.htm"><img src="images/navidx.gif" width="84" height="23"
border="0" alt="INDEX"> </a></td>
</tr>
</table>
<p><br clear="all">
</p>
<hr align="left">
<blockquote>
<h2>
<a name="1205194"> </a><font color="#003366" face="Verdana, Arial, Helvetica, sans-serif">13.9 volatile qualifier</font>
</h2><hr>
<p>
<a name="1205195"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">An access to an object that has volatile-qualified type is a "read" from the memory location containing the object (if the object's value is required) or a "write" to the memory location containing the object (if the object is assigned to).</font>
</p>
<p>
<a name="1205196"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">If the volatile object is an array, then the access is only to the appropriate element of the array.</font>
</p>
<p>
<a name="1205197"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">If the volatile object is a structure and only a field of the structure is required, then the access is only to the appropriate field.</font>
</p>
<p>
<a name="1205198"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">If the access is an indirect access through a pointer, then the access is equivalent to an access of an array element of that type.</font>
</p>
<p>
<a name="1209572"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">If the object is not an array element or structure field and the access is not indirect, then the object occupies a whole number of words, and all the words are accessed. Otherwise, if the array element or structure field is shorter than a word, then only the appropriate bytes are accessed.</font>
</p>
<p>
<a name="1205200"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">If the object is a bit-field, then in the case of read access, the entire word containing the bit-field is read; and in the case of write access, the entire word containing the bit-field is first read, and then written.</font>
</p>
<p>
<a name="1205201"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">When the volatile qualifier is used, the compiler reduces the scope of its optimization. A typical use of volatile is where a variable is accessed from two separate threads of control, and compiler optimizations may otherwise prevent the correct functioning of the code.</font>
</p>
<p>
<a name="1205202"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">The following example shows the use of an interrupt handler which updates a shared synchronization variable, and the main program busy waits for the variable to be updated. Without the use of <font size=2 face=Courier><strong>volatile</strong></font> in this example, the compiler would be free to optimize away reading the <font size=2 face=Courier><strong>sync</strong></font> variable, because it knows that nothing in the <font size=2 face=Courier><strong>wait_for_isr()</strong></font> function can write to it:</font>
</p>
<a name="1215510"> </a><font size=2 face=Courier><strong>void isr(unsigned int Areg, unsigned int Breg, unsigned int Creg);<br>#pragma ST_interrupt_handler(isr)<br><br></strong></font>
<a name="1215512"> </a><font size=2 face=Courier><strong>volatile int sync=0;<br><br></strong></font>
<a name="1205208"> </a><font size=2 face=Courier><strong>void isr(unsigned int Areg, unsign int Breg, unsigned int Creg)<br>{<br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -