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

📄 chapter 2 tokens -- valvano.htm

📁 介绍了在嵌入式系统中如何用c来设计嵌入式软件
💻 HTM
📖 第 1 页 / 共 3 页
字号:
next<BR>Bit_PointerType Bit_GetPt; // Pointer of where to get next<BR>/* 
Bit_FIFO is empty if Bit_PutPt==Bit_GetPt */<BR>/* Bit_FIFO is full if 
Bit_PutPt+1==Bit_GetPt */<BR>short Bit_Same(Bit_PointerType p1, Bit_PointerType 
p2){<BR>&nbsp;&nbsp;&nbsp;if((p1.WPt==p2.WPt)&amp;&amp;(p1.Mask==p2.Mask))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(1); 
//yes<BR>&nbsp;&nbsp;&nbsp;return(0);} // no<BR>void Bit_Init(void) 
{<BR>&nbsp;&nbsp;&nbsp;Bit_PutPt.Mask=Bit_GetPt.Mask=0x8000;<BR>&nbsp;&nbsp;&nbsp;Bit_PutPt.WPt=Bit_GetPt.WPt=&amp;Bit_Fifo[0]; 
/* Empty */<BR>}<BR>// returns TRUE=1 if successful,<BR>// FALSE=0 if full and 
data not saved<BR>// input is boolean FALSE if data==0<BR><A 
name=BITPUT></A>short Bit_Put (short data) { Bit_PointerType 
myPutPt;<BR>&nbsp;&nbsp;&nbsp;myPutPt=Bit_PutPt;<BR>&nbsp;&nbsp;&nbsp;myPutPt.Mask=myPutPt.Mask&gt;&gt;1;<BR>&nbsp;&nbsp;&nbsp;if(myPutPt.Mask==0) 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myPutPt.Mask=0x8000;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((++myPutPt.WPt)==&amp;Bit_Fifo[Bit_FifoSize])<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myPutPt.WPt=&amp;Bit_Fifo[0]; 
// wrap<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;if 
(Bit_Same(myPutPt,Bit_GetPt))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(0); 
/* Failed, Bit_Fifo was full */<BR>&nbsp;&nbsp;&nbsp;else { 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(data)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(*Bit_PutPt.WPt) 
|= Bit_PutPt.Mask; // set 
bit<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(*Bit_PutPt.WPt) 
&amp;= ~Bit_PutPt.Mask; // clear 
bit<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bit_PutPt=myPutPt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(1);<BR>&nbsp;&nbsp;&nbsp;}<BR>}<BR>// 
returns TRUE=1 if successful,<BR>// FALSE=0 if empty and data not removed<BR>// 
output is boolean 0 means FALSE, nonzero is true<BR>short Bit_Get (unsigned 
short *datapt) {<BR>&nbsp;&nbsp;&nbsp;if 
(Bit_Same(Bit_PutPt,Bit_GetPt))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(0); 
/* Failed, Bit_Fifo was empty */<BR>&nbsp;&nbsp;&nbsp;else { 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*datapt=(*Bit_GetPt.WPt)&amp;Bit_GetPt.Mask;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bit_GetPt.Mask=Bit_GetPt.Mask&gt;&gt;1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(Bit_GetPt.Mask==0) 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bit_GetPt.Mask=0x8000;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((++Bit_GetPt.WPt)==&amp;Bit_Fifo[Bit_FifoSize])<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bit_GetPt.WPt=&amp;Bit_Fifo[0]; 
// 
wrap<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(1); 
<BR>&nbsp;&nbsp;&nbsp;}<BR>}</CODE></P></DIR>
<ADDRESS>Listing 2-2: This naming convention can create modularity similar to 
classes in C++.</ADDRESS>
<P>&nbsp;</P>
<P><B><I><FONT face=Helvetica,Arial><A 
name=PUNCTUATION></A>Punctuation</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">Punctuation marks (</FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap2/chap2.htm#SEMICOLONS">semicolons</A><FONT 
face="Times New Roman,Times">, </FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap2/chap2.htm#COLONS">colons</A><FONT 
face="Times New Roman,Times">, </FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap2/chap2.htm#COMMAS">commas</A><FONT 
face="Times New Roman,Times">, </FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap2/chap2.htm#APOSTROPHES">apostrophes</A><FONT 
face="Times New Roman,Times">, </FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap2/chap2.htm#QUOTATIONS">quotation 
marks</A><FONT face="Times New Roman,Times">, </FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap2/chap2.htm#BRACES">braces</A><FONT 
face="Times New Roman,Times">, </FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap2/chap2.htm#BRACKETS">brackets</A><FONT 
face="Times New Roman,Times">, and </FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap2/chap2.htm#PARENTHESES">parentheses</A><FONT 
face="Times New Roman,Times">) are very important in C. It is one of the most 
frequent sources of errors for both the beginning and experienced programmers. 
</FONT></P>
<P><FONT face=Helvetica,Arial><A name=SEMICOLONS></A>Semicolons</FONT></P>
<P><FONT face="Times New Roman,Times">Semicolons are used as statement 
terminators. Strange and confusing syntax errors may be generated when you 
forget a semicolon, so this is one of the first things to check when trying to 
remove syntax errors. Notice that one semicolon is placed at the end of every 
simple statement in the following example</FONT></P>
<DIR>
<P><CODE>#define PORTB *(unsigned char volatile *)(0x1004)<BR>void 
Step(void){<BR>&nbsp;&nbsp;&nbsp;PORTB = 10;<BR>&nbsp;&nbsp;&nbsp;PORTB = 
9;<BR>&nbsp;&nbsp;&nbsp;PORTB = 5;<BR>&nbsp;&nbsp;&nbsp;PORTB = 
6;}</CODE></P></DIR>
<ADDRESS><FONT face="Times New Roman,Times">Listing 2-3: Semicolons are used to 
separate one statement from the next.</FONT></ADDRESS>
<P><FONT face="Times New Roman,Times">Preprocessor directives do not end with a 
semicolon since they are not actually part of the C language proper. 
Preprocessor directives begin in the first column with the 
</FONT><CODE>#</CODE><FONT face="Times New Roman,Times">and conclude at the end 
of the line. The following example will fill the array 
</FONT><CODE>DataBuffer</CODE><FONT face="Times New Roman,Times"> with data read 
from the input port (PORTC). We assume in this example that Port C has been 
initialized as an input. Semicolons are also used in the </FONT><CODE>for 
loop</CODE><FONT face="Times New Roman,Times"> statement (see also <A 
href="http://www.ece.utexas.edu/~valvano/embed/chap6/chap6.htm">Chapter 6</A>), 
as illustrated by</FONT></P>
<DIR>
<P><CODE>void Fill(void){ short 
j;<BR>&nbsp;&nbsp;&nbsp;for(j=0;j&lt;100;j++){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DataBuffer[j]=PORTC;}<BR>}</CODE></P></DIR>
<ADDRESS><FONT face="Times New Roman,Times">Listing 2-4: Semicolons are used to 
separate three fields of the for statement.</FONT></ADDRESS>
<P>&nbsp;</P>
<P><FONT face=Helvetica,Arial><A name=COLONS></A>Colons</FONT></P>
<P><FONT face="Times New Roman,Times">We can define a label using the colon. 
Although C has a </FONT><CODE>goto</CODE><FONT face="Times New Roman,Times"> 
statement, I discourage its use. I believe the software is easier to understand 
using the block-structured control statements (</FONT><CODE>if</CODE><FONT 
face="Times New Roman,Times">, </FONT><CODE>if else</CODE><FONT 
face="Times New Roman,Times">, </FONT><CODE>for</CODE><FONT 
face="Times New Roman,Times">, </FONT><CODE>while</CODE><FONT 
face="Times New Roman,Times">, </FONT><CODE>do while</CODE><FONT 
face="Times New Roman,Times">, and </FONT><CODE>switch case</CODE><FONT 
face="Times New Roman,Times">.) The following example will return after the Port 
C input reads the same value 100 times in a row. Again we assume Port C has been 
initialized as an input. Notice that every time the current value on Port C is 
different from the previous value the counter is reinitialized.</FONT></P>
<DIR>
<P><CODE>char Debounce(void){ short Cnt; unsigned char 
LastData;<BR>Start:&nbsp;&nbsp;&nbsp;&nbsp;Cnt=0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* 
number of times Port C is the same 
*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LastData=PORTC;&nbsp;<BR>Loop:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(++Cnt==100) 
goto Done;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* same thing 100 times 
*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(LastData!=PORTC) 
goto Start;/* changed 
*/&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;goto 
Loop;&nbsp;<BR>Done:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(LastData);}</CODE></P></DIR>
<ADDRESS><FONT face="Times New Roman,Times">Listing 2-4: Colons are used to 
define labels (places we can jump to)</FONT></ADDRESS>
<P><FONT face="Times New Roman,Times">Colons also terminate 
</FONT><CODE>case</CODE><FONT face="Times New Roman,Times">, and 
</FONT><CODE>default</CODE><FONT face="Times New Roman,Times"> prefixes that 
appear in switch statements. For more information see the section on <A 
href="http://www.ece.utexas.edu/~valvano/embed/chap6/chap6.htm#SWITCH">switch</A> 
in Chapter 6. In the following example, the next stepper motor output is found 
(the proper sequence is 10,9,5,6). The default case is used to restart the 
pattern.</FONT></P>
<DIR>
<P><CODE>unsigned char NextStep(unsigned char step){ unsigned char 
theNext;<BR>&nbsp;&nbsp;&nbsp;switch(step){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 
10: theNext=9; break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 9: theNext=5; 
break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 5: theNext=6; 
break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 6: theNext=10; 
break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default: theNext=10; 
<BR>&nbsp;&nbsp;&nbsp;} <BR>return(theNext);}</CODE></P></DIR>
<ADDRESS><FONT face="Times New Roman,Times">Listing 2-5: Colons are also used to 
with the switch statement</FONT></ADDRESS>
<P><FONT face="Times New Roman,Times">For both applications of the colon 
(</FONT><CODE>goto</CODE><FONT face="Times New Roman,Times"> and 
</FONT><CODE>switch</CODE><FONT face="Times New Roman,Times">), we see that a 
label is created that is a potential target for a transfer of 
control.</FONT></P>
<P><FONT face=Helvetica,Arial><A name=COMMAS></A>Commas</FONT></P>
<P><FONT face="Times New Roman,Times">Commas separate items that appear in 
lists. We can create multiple variables of the same type. E.g.,</FONT></P>
<DIR>
<P><CODE>unsigned short beginTime,endTime,elapsedTime;</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">Lists are also used with functions having 
multiple parameters (both when the function is defined and called):</FONT></P>
<DIR>
<P><CODE>short add(short x, short y){ short z;<BR>&nbsp;&nbsp;&nbsp;&nbsp;z=x+y; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if((x&gt;0)&amp;&amp;(y&gt;0)&amp;&amp;(z&lt;0))z=32767; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if((x&lt;0)&amp;&amp;(y&lt;0)&amp;&amp;(z&gt;0))z=-32768; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;return(z);} <BR>void main(void){ short 
a,b;<BR>&nbsp;&nbsp;&nbsp;&nbsp;a=add(2000,2000)<BR>&nbsp;&nbsp;&nbsp;&nbsp;b=0<BR>&nbsp;&nbsp;&nbsp;&nbsp;while(1){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b=add(b,1);<BR>&nbsp;&nbsp;}</CODE></P></DIR>
<P><I><FONT face="Times New Roman,Times">Listing 2-6: Commas separate the 
parameters of a function</FONT></I></P>
<P><FONT face="Times New Roman,Times">Lists can also be used in general 
expressions. Sometimes it adds clarity to a program if related variables are 
modified at the same place. The value of a list of expressions is always the 
value of the last expression in the list. In the following example, first 
</FONT><CODE>thetime</CODE><FONT face="Times New Roman,Times"> is incremented, 
thedate is decremented, then x is set to k+2.</FONT></P>
<DIR>
<P><CODE>x=(thetime++,--thedate,k+2);</CODE></P></DIR>
<P><FONT face=Helvetica,Arial><A name=APOSTROPHES></A>Apostrophes</FONT></P>
<P>Apostrophes are used to specify character literals. <FONT 
face="Times New Roman,Times">For more information about character literals see 
the section on <A 
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#CHARACTER">characters</A> 
in Chapter 3. </FONT>Assuming the function <CODE>OutChar</CODE> will print a 
single ASCII character, the following example will print the lower case 
alphabet:</P>
<DIR>
<P><CODE>void Alphabet(void){ unsigned char 
mych;<BR>&nbsp;&nbsp;&nbsp;for(mych='a';mych&lt;='z';mych++){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OutChar(mych);}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* 
Print next letter */<BR>}</CODE></P></DIR>
<ADDRESS><FONT face="Times New Roman,Times">Listing 2-7: Apostrophes are used to 
specify characters</FONT></ADDRESS>
<P><FONT face=Helvetica,Arial><A name=QUOTATIONS></A>Quotation marks</FONT></P>
<P><FONT face="Times New Roman,Times">Quotation marks are used to specify string 
literals. For more information about string literals see the section on <A 
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#STRING">strings</A> 
in Chapter 3. Example</FONT></P>
<DIR>
<P><CODE>unsigned char Name[12]; /* Place for 11 characters and 
termination*/<BR>void InitName(void){ <BR>&nbsp;&nbsp;&nbsp;Name="Hello 
World";<BR>}</CODE></P></DIR>
<ADDRESS><FONT face="Times New Roman,Times">Listing 2-8: Quotation marks are 
used to specify strings</FONT></ADDRESS>
<P><FONT face="Times New Roman,Times">The command <CODE>Letter='A';</CODE> 
places the ASCII code (65) into the variable <CODE>Letter</CODE>. The command 
<CODE>pt="A";</CODE> creates an ASCII string and places a pointer to it into the 
variable <CODE>pt</CODE>. </FONT></P>
<P><FONT face=Helvetica,Arial><A name=BRACES></A>Braces</FONT></P>
<P><FONT face="Times New Roman,Times">Braces {} are used throughout C programs. 
The most common application is for creating a compound statement. Each open 
brace { must be matched with a closing brace }. One approach that helps to match 
up braces is to use indenting. Each time an open brace is used, the source code 
is tabbed over. In this way, it is easy to see at a glance the brace pairs. 
Examples of this approach to tabbing are the </FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap2/chap2.htm#BITPUT">Bit_Put</A><FONT 
face="Times New Roman,Times"> function within Listing 2-2 and the median 
function in </FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap1/chap1.htm#COMPOUND">Listing 
1-4</A><FONT face="Times New Roman,Times">. </FONT></P>
<P><FONT face=Helvetica,Arial><A name=BRACKETS></A>Brackets</FONT></P>
<P><FONT face="Times New Roman,Times">Square brackets enclose array 
<I>dimensions</I> (in declarations) and <I>subscripts</I> (in expressions). 
Thus,</FONT></P>
<DIR>
<P><CODE>short Fifo[100];</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">declares an integer array named 
</FONT><CODE>Fifo</CODE><FONT face="Times New Roman,Times"> consisting of 80 
words numbered from 0 through 99, and</FONT></P>
<DIR>
<P><CODE>PutPt = &amp;Fifo[0];</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">assigns the variable 
</FONT><CODE>PutPt</CODE><FONT face="Times New Roman,Times"> to the address of 
the first entry of the array.</FONT></P>
<P><FONT face=Helvetica,Arial><A name=PARENTHESES></A>Parentheses</FONT></P>
<P><FONT face="Times New Roman,Times">Parentheses enclose argument lists that 
are associated with function declarations and calls. They are required even if 
there are no arguments.</FONT></P>
<P><FONT face="Times New Roman,Times">As with all programming languages, C uses 
parentheses to control the order in which expressions are evaluated. Thus, 
(11+3)/2 yields 7, whereas 11+3/2 yields 12. Parentheses are very important when 
writing expressions. </FONT></P>
<P>&nbsp;</P>
<P><B><I><FONT face=Helvetica,Arial><A 
name=OPERATORS></A>Operators</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">The special characters used as 
<I>expression operators</I> are covered in the </FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap5/chap5.htm">operator 
section</A><FONT face="Times New Roman,Times"> in chapter 5. There are many 
operators, some of which are single characters </FONT></P>
<DIR>
<DIR>
<P><CODE>~&nbsp;&nbsp;!&nbsp;&nbsp;@&nbsp;&nbsp;%&nbsp;&nbsp;^&nbsp;&nbsp;&amp;&nbsp;&nbsp;*&nbsp;&nbsp;-&nbsp;&nbsp;+&nbsp;&nbsp;=&nbsp;&nbsp;|&nbsp;&nbsp;/&nbsp;&nbsp;:&nbsp;&nbsp;?&nbsp;&nbsp;&lt;&nbsp;&nbsp;&gt; 
,</CODE></P></DIR></DIR>
<P><FONT face="Times New Roman,Times">while others require two 
characters</FONT></P>
<DIR>
<DIR>
<P><CODE>++&nbsp;&nbsp;--&nbsp;&nbsp;&lt;&lt;&nbsp;&nbsp;&gt;&gt;&nbsp;&nbsp;&lt;=&nbsp;&nbsp;+=&nbsp;&nbsp;-=&nbsp;&nbsp;*=&nbsp;&nbsp;/=&nbsp;&nbsp;==&nbsp;&nbsp;|=&nbsp;&nbsp;%=&nbsp;&nbsp;&amp;=&nbsp;&nbsp;^=&nbsp;&nbsp;||&nbsp;&nbsp;&amp;&amp;&nbsp;&nbsp;!=</CODE></P></DIR></DIR>
<P><FONT face="Times New Roman,Times">and some even require three 
characters</FONT></P>
<DIR>
<DIR>
<P><CODE>&lt;&lt;=&nbsp;&nbsp;&gt;&gt;=</CODE></P></DIR></DIR>
<P><FONT face="Times New Roman,Times">The multiple-character operators can not 
have white spaces or comments between the characters. </FONT></P>
<P><FONT face="Times New Roman,Times">The C syntax can be confusing to the 
beginning programmer. For example</FONT></P>
<DIR>
<P><CODE>z=x+y;&nbsp;&nbsp;&nbsp;/* sets z equal to the sum of x and y 
*/<BR>z=x_y;&nbsp;&nbsp;&nbsp;/* sets z equal to the value of x_y */</CODE></P>
<P>&nbsp;</P></DIR>
<P><FONT face="Times New Roman,Times">Go to <A 
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm">Chapter 3 on 
</A><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm">Literals</A> 
Return to <A href="http://www.ece.utexas.edu/~valvano/embed/toc1.htm">Table of 
Contents</A> </FONT></P></BODY></HTML>

⌨️ 快捷键说明

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