⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter 4 variables and constants -- valvano.htm

📁 介绍了在嵌入式系统中如何用c来设计嵌入式软件
💻 HTM
📖 第 1 页 / 共 5 页
字号:
replaced by a single colon,<B> :</B>, in the 6812 code. In both cases, this 
static global can not be referenced outside the scope of this file.</FONT></P>
<DIR>
<P><CODE>static short TheGlobal;&nbsp;&nbsp;&nbsp;/* a static global 
variable*/<BR>void main(void){ 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;TheGlobal=1000;&nbsp;&nbsp;&nbsp;&nbsp;<BR>}</CODE></P></DIR>
<ADDRESS>Listing 4-3: Example showing a static global variable</ADDRESS>
<P><FONT face="Times New Roman,Times">The 6811 code generated by the ICC11 
(Version 4) compiler is as follows</FONT></P>
<DIR>
<P><CODE>&nbsp;&nbsp;&nbsp;&nbsp;.area text <BR>&nbsp;&nbsp;&nbsp;&nbsp;.global 
_main<BR>_main:<BR>&nbsp;&nbsp;&nbsp;&nbsp;ldd #1000 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;std _TheGlobal <BR>&nbsp;&nbsp;&nbsp;&nbsp;rts 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;.area bss <BR>_TheGlobal: .blkb 2 </CODE></P></DIR>
<P><FONT face="Times New Roman,Times">The 6812 code generated by the ICC12 
(Version 5.1) compiler is as follows</FONT></P>
<DIR>
<P><CODE>&nbsp;&nbsp;&nbsp;&nbsp;.area text <BR>_main:: 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;movw #1000,_TheGlobal 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;rts <BR>&nbsp;&nbsp;&nbsp;&nbsp;.area bss 
<BR>_TheGlobal: .blkb 2 </CODE></P></DIR>
<P><FONT face="Times New Roman,Times">The 6812 code generated by the Hiware 
compiler is the same as a regular global. Hiware does properly limit the access 
only to the static global to functions defined in this file.</FONT></P>
<DIR>
<P><CODE>main: <BR>&nbsp;&nbsp;&nbsp;&nbsp;LDD #1000 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;STD TheGlobal <BR>&nbsp;&nbsp;&nbsp;&nbsp;RTS 
<BR></CODE></P></DIR>
<P><FONT face="Times New Roman,Times"><A name=STATICLOCAL></A>A <B>static 
local</B> is similar to the static global. Just as with the other statics, the 
variable is defined in RAM permanently. The assembly language code generated by 
the compiler that accesses the variable is identical. The only difference is the 
scope. The static local can only be accessed within the function where it is 
defined. The following example sets a static local, called <B>TheLocal</B>, to 
the value 1000. The compiler limits the access to the static local, so that this 
variable can not be accessed by other functions in this file or in other files. 
Notice that the assembly language name of the static local is a unique 
compiler-generated name (L2 in this example.) This naming method allows other 
functions to also define a static local or automatic local with the same name. 
</FONT></P>
<DIR>
<P><CODE>void main(void){ <BR>&nbsp;&nbsp;&nbsp;&nbsp;static stort 
TheLocal;&nbsp;&nbsp;&nbsp;/* a static local 
variable*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;TheLocal=1000;&nbsp;&nbsp;&nbsp;&nbsp;<BR>}</CODE></P></DIR>
<ADDRESS>Listing 4-4: Example showing a static local variable</ADDRESS>
<P><FONT face="Times New Roman,Times">The 6811 code generated by the ICC11 
(Version 4) compiler is as follows</FONT></P>
<DIR>
<P><CODE>&nbsp;&nbsp;&nbsp;&nbsp;.area text <BR>&nbsp;&nbsp;&nbsp;&nbsp;.global 
_main<BR>_main:<BR>&nbsp;&nbsp;&nbsp;&nbsp;ldd #1000 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;std L2 <BR>&nbsp;&nbsp;&nbsp;&nbsp;rts 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;.area bss <BR>L2: .blkb 2 </CODE></P></DIR>
<P><FONT face="Times New Roman,Times">The 6812 code generated by the ICC12 
(Version 5.1) compiler is as follows</FONT></P>
<DIR>
<P><CODE>&nbsp;&nbsp;&nbsp;&nbsp;.area text <BR>_main:: 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;movw #1000,L2 <BR>&nbsp;&nbsp;&nbsp;&nbsp;rts 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;.area bss <BR>L2: .blkb 2 </CODE></P></DIR>
<P><FONT face="Times New Roman,Times">Again the 6812 code generated by the 
Hiware compiler is the same as a regular global. Hiware does properly limit the 
access only to the static local to the function in which it is 
defined.</FONT></P>
<DIR>
<P><CODE>main: <BR>&nbsp;&nbsp;&nbsp;&nbsp;LDD #1000 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;STD TheLocal <BR>&nbsp;&nbsp;&nbsp;&nbsp;RTS 
<BR></CODE></P></DIR>
<P><FONT face="Times New Roman,Times">A <B>static local </B>can be used to save 
information from one instance of the function call to the next. Assume each 
function wished to know how many times it has been called. Remember upon reset, 
the ICC11/ICC12/Hiware compilers will initialize all statics to zero (including 
static locals). The following functions maintain such a count, and these counts 
can not be accessed by other functions. Even though the names are the same, the 
two static locals are in fact distinct.</FONT></P>
<DIR>
<P><CODE>void function1(void){ <BR>&nbsp;&nbsp;&nbsp;&nbsp;static short 
TheCount;<BR>&nbsp;&nbsp;&nbsp;&nbsp;TheCount=TheCount+1;<BR>}<BR>void 
function2(void){ <BR>&nbsp;&nbsp;&nbsp;&nbsp;static short 
TheCount;<BR>&nbsp;&nbsp;&nbsp;&nbsp;TheCount=TheCount+1;<BR>}</CODE></P></DIR>
<ADDRESS><FONT face="Times New Roman,Times">Listing 4-5: Example showing two 
static local variables with the same name</FONT></ADDRESS>
<P><FONT face="Times New Roman,Times">The 6811 code generated by the ICC11 
(Version 4) compiler is as follows</FONT></P>
<DIR>
<P><CODE>&nbsp;&nbsp;&nbsp;&nbsp;.area text <BR>&nbsp;&nbsp;&nbsp;&nbsp;.global 
_function1<BR>_function1:<BR>&nbsp;&nbsp;&nbsp;&nbsp;ldd L2 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;addd #1 <BR>&nbsp;&nbsp;&nbsp;&nbsp;std L2 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;rts <BR>&nbsp;&nbsp;&nbsp;&nbsp;.global 
_function2<BR>_function2:<BR>&nbsp;&nbsp;&nbsp;&nbsp;ldd L3 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;addd #1 <BR>&nbsp;&nbsp;&nbsp;&nbsp;std L3 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;rts <BR>&nbsp;&nbsp;&nbsp;&nbsp;.area bss <BR>L2: 
.blkb 2<BR>L3: .blkb 2 </CODE></P></DIR>
<P><FONT face="Times New Roman,Times">The 6812 code generated by the ICC12 
(Version 5.1) compiler is as follows</FONT></P>
<DIR>
<P><CODE>&nbsp;&nbsp;&nbsp;&nbsp;.area text 
<BR>_function1::<BR>&nbsp;&nbsp;&nbsp;&nbsp;ldd L2 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;addd #1 <BR>&nbsp;&nbsp;&nbsp;&nbsp;std L2 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;rts<BR>_function2::<BR>&nbsp;&nbsp;&nbsp;&nbsp;ldd 
L3 <BR>&nbsp;&nbsp;&nbsp;&nbsp;addd #1 <BR>&nbsp;&nbsp;&nbsp;&nbsp;std L3 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;rts <BR>&nbsp;&nbsp;&nbsp;&nbsp;.area bss <BR>L2: 
.blkb 2<BR>L3: .blkb 2 </CODE></P></DIR>
<P><B><I><FONT face=Helvetica,Arial><A 
name=VOLATILE></A>Volatile</FONT></I></B><FONT face=Helvetica,Arial> </FONT></P>
<P><FONT face="Times New Roman,Times">We add the <B>volatile</B> modifier to a 
variable that can change value outside the scope of the function. Usually the 
value of a global variable changes only as a result of explicit statements in 
the C function that is currently executing. The paradigm results when a single 
program executes from start to finish, and everything that happens is an 
explicit result of actions taken by the program. There are two situations that 
break this simple paradigm in which the value of a memory location might change 
outside the scope of a particular function currently executing: </FONT></P>
<DIR>
<DIR>
<P><FONT face="Times New Roman,Times">1) interrupts and<BR>2) input/output 
ports. </FONT></P></DIR></DIR>
<P><FONT face="Times New Roman,Times">An interrupt is a hardware-requested 
software action. Consider the following multithreaded interrupt example. There 
is a foreground thread called <B>main()</B>, which we setup as the usual main 
program that all C programs have. Then, there is a background thread called 
<B>TOFhandler()</B>, which we setup to be executed on a periodic basis (e.g., 
every 16 ms). Both threads access the global variable, <B>Time</B>. The 
interrupt thread increments the global variable, and the foreground thread waits 
for time to reach 100. Notice that <B>Time</B> changes value outside the 
influence of the <B>main() </B>program.</FONT></P>
<DIR>
<P><CODE>volatile char Time;<BR>#pragma interrupt_handler TOFhandler<BR>void 
TOFhandler(void){ &nbsp;&nbsp;&nbsp;/* every 16ms 
*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;TFLG2=0x80;&nbsp;&nbsp;&nbsp;&nbsp;/* TOF 
interrupt acknowledge */<BR>&nbsp;&nbsp;&nbsp;&nbsp;Time=Time+1;<BR>}<BR>void 
main(void){ <BR>&nbsp;&nbsp;&nbsp;&nbsp;TSCR |=0x80; /* TEN(enable) 
*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;TMSK2=0xA2; &nbsp;/* TOI arm, TPU(pullup) timer/4 
(500ns) 
*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;CLKCTL=0x00;<BR>&nbsp;&nbsp;&nbsp;&nbsp;Time=0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;while(Time&lt;100){};&nbsp;/* 
wait for 100 counts of the 16 ms timer*/<BR>}</CODE></P></DIR>
<ADDRESS><FONT face="Times New Roman,Times">Listing 4-6: ICC12 example showing 
shared access to a common global variable</FONT></ADDRESS>
<P><FONT face="Times New Roman,Times">Without the <B>volatile</B> modifier the 
compiler might look at the two statements:</FONT></P>
<DIR>
<P><CODE>Time=0;<BR>while(Time&lt;100){};&nbsp;</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">and conclude that since the while loop 
does not modify <B>Time</B>, it could never reach 100. Some compilers (not yet 
in the current versions of ICC11 and ICC12) might attempt to move the read 
<B>Time </B>operation, performing it once before the while loop is executed. The 
<B>volatile</B> modifier disables the optimization, forcing the program to fetch 
a new value from the variable each time the variable is accessed. </FONT></P>
<P><FONT face="Times New Roman,Times">In the next 6812 example, assume PORTA is 
an input port containing the current status of some important external signals. 
The program wishes to collect status versus time data of these external signals. 
</FONT></P>
<DIR>
<P><CODE>unsigned char data[100];<BR>#define PORTA *(unsigned char volatile 
*)(0x0000)<BR>#define DDRA *(unsigned char volatile *)(0x0004)<BR>void 
main(void){ short i;<BR>&nbsp;&nbsp;&nbsp;&nbsp;DDRA=0x00; /* make Port A an 
input */<BR>&nbsp;&nbsp;&nbsp;&nbsp;for(i=0;i&lt;100;i++){ /* collect 100 
measurements 
*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[i]=PORTA; &nbsp;/* 
collect ith measurement */<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>}</CODE></P></DIR>
<ADDRESS>Listing 4-7: Example showed shared access to a common global 
variable</ADDRESS>
<P><FONT face="Times New Roman,Times">Without the <B>volatile</B> modifier in 
the PORTA definition, the compiler might optimize the for loop, reading PORTA 
once, then storing 100 identical copies into the data array. </FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap5/chap5.htm#IO">I/O 
ports</A><FONT face="Times New Roman,Times"> will be handled in more detail in 
<A href="http://www.ece.utexas.edu/~valvano/embed/chap7/chap7.htm">Chapter 7</A> 
on pointers. </FONT></P>
<P><B><I><FONT face=Helvetica,Arial><A 
name=AUTOMATIC></A>Automatics</FONT></I></B><FONT face=Helvetica,Arial> 
</FONT></P>
<P><FONT face="Times New Roman,Times">Automatic variables, on the other hand, do 
not have fixed memory locations. They are dynamically allocated when the block 
in which they are defined is entered, and they are discarded upon leaving that 
block. Specifically, they are allocated on the 6811/6812 stack by subtracting a 
value (one for characters, two for integers and four for long integers) from the 
stack pointer register (SP). Since automatic objects exist only within blocks, 
they can only be declared locally. Automatic variables can only be referenced 
(read or write) by the function that created it. In this way, the information is 
protected or local to the function.</FONT></P>
<P><FONT face="Times New Roman,Times">When a local variable is created it has no 
dependable initial value. It must be set to an initial value by means of an 
assignment operation. C provides for automatic variables to be initialized in 
their declarations, like globals. It does this by generating "hidden" code that 
assigns values automatically after variables are allocated space. </FONT></P>
<P><FONT face="Times New Roman,Times">It is tempting to forget that automatic 
variables go away when the block in which they are defined exits. This sometimes 
leads new C programmers to fall into the "dangling reference" trap in which a 
function returns a pointer to a local variable, as illustrated by </FONT></P>
<DIR>
<P><CODE>int *BadFunction(void) {<BR>&nbsp;&nbsp;&nbsp;int 
z;<BR>&nbsp;&nbsp;&nbsp;z=1000;<BR>&nbsp;&nbsp;&nbsp;return 
(&amp;z);<BR>}</CODE></P></DIR>
<ADDRESS>Listing 4-8: Example showing an illegal reference to a local 
variable</ADDRESS>
<P><FONT face="Times New Roman,Times">When callers use the returned address of 
<B>z </B>they will find themselves messing around with the stack space that <B>z 
</B>used to occupy. This type of error is NOT flagged as a syntax error, but 
rather will cause unexpected behavior during execution.</FONT></P>
<P>&nbsp;</P>
<P><B><I><FONT face=Helvetica,Arial><A name=IMPLEMENTATION></A>Implementation of 
automatic variables</FONT></I></B><FONT face=Helvetica,Arial> </FONT></P>
<P><FONT face="Times New Roman,Times">If locals are dynamically allocated at 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -