📄 chapter 2 tokens -- valvano.htm
字号:
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> if((p1.WPt==p2.WPt)&&(p1.Mask==p2.Mask))<BR> return(1);
//yes<BR> return(0);} // no<BR>void Bit_Init(void)
{<BR> Bit_PutPt.Mask=Bit_GetPt.Mask=0x8000;<BR> Bit_PutPt.WPt=Bit_GetPt.WPt=&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> myPutPt=Bit_PutPt;<BR> myPutPt.Mask=myPutPt.Mask>>1;<BR> if(myPutPt.Mask==0)
{<BR> myPutPt.Mask=0x8000;<BR> if((++myPutPt.WPt)==&Bit_Fifo[Bit_FifoSize])<BR> myPutPt.WPt=&Bit_Fifo[0];
// wrap<BR> }<BR> if
(Bit_Same(myPutPt,Bit_GetPt))<BR> return(0);
/* Failed, Bit_Fifo was full */<BR> else {
<BR> if(data)<BR> (*Bit_PutPt.WPt)
|= Bit_PutPt.Mask; // set
bit<BR> else<BR> (*Bit_PutPt.WPt)
&= ~Bit_PutPt.Mask; // clear
bit<BR> Bit_PutPt=myPutPt;<BR> return(1);<BR> }<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> if
(Bit_Same(Bit_PutPt,Bit_GetPt))<BR> return(0);
/* Failed, Bit_Fifo was empty */<BR> else {
<BR> *datapt=(*Bit_GetPt.WPt)&Bit_GetPt.Mask;<BR> Bit_GetPt.Mask=Bit_GetPt.Mask>>1;<BR> if(Bit_GetPt.Mask==0)
{<BR> Bit_GetPt.Mask=0x8000;<BR> if((++Bit_GetPt.WPt)==&Bit_Fifo[Bit_FifoSize])<BR> Bit_GetPt.WPt=&Bit_Fifo[0];
//
wrap<BR> }<BR> return(1);
<BR> }<BR>}</CODE></P></DIR>
<ADDRESS>Listing 2-2: This naming convention can create modularity similar to
classes in C++.</ADDRESS>
<P> </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> PORTB = 10;<BR> PORTB =
9;<BR> PORTB = 5;<BR> 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> for(j=0;j<100;j++){<BR> 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> </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: Cnt=0; /*
number of times Port C is the same
*/<BR> LastData=PORTC; <BR>Loop: if(++Cnt==100)
goto Done; /* same thing 100 times
*/<BR> if(LastData!=PORTC)
goto Start;/* changed
*/ <BR> goto
Loop; <BR>Done: 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> switch(step){<BR> case
10: theNext=9; break;<BR> case 9: theNext=5;
break;<BR> case 5: theNext=6;
break;<BR> case 6: theNext=10;
break;<BR> default: theNext=10;
<BR> } <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> z=x+y;
<BR> if((x>0)&&(y>0)&&(z<0))z=32767;
<BR> if((x<0)&&(y<0)&&(z>0))z=-32768;
<BR> return(z);} <BR>void main(void){ short
a,b;<BR> a=add(2000,2000)<BR> b=0<BR> while(1){<BR> b=add(b,1);<BR> }</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> for(mych='a';mych<='z';mych++){<BR> OutChar(mych);} /*
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> 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 = &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> </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>~ ! @ % ^ & * - + = | / : ? < >
,</CODE></P></DIR></DIR>
<P><FONT face="Times New Roman,Times">while others require two
characters</FONT></P>
<DIR>
<DIR>
<P><CODE>++ -- << >> <= += -= *= /= == |= %= &= ^= || && !=</CODE></P></DIR></DIR>
<P><FONT face="Times New Roman,Times">and some even require three
characters</FONT></P>
<DIR>
<DIR>
<P><CODE><<= >>=</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; /* sets z equal to the sum of x and y
*/<BR>z=x_y; /* sets z equal to the value of x_y */</CODE></P>
<P> </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 + -