📄 chapter 1 program structure -- valvano.htm
字号:
operators. We begin with the assignment operator. Notice that in the line
</FONT><CODE>x=1;</CODE><FONT face="Times New Roman,Times"> x is on the left
hand side of the = . This specifies the address of x is the destination of
assignment. On the other hand, in the line </FONT><CODE>z=x;</CODE><FONT
face="Times New Roman,Times"> x is on the right hand side of the = . This
specifies the value of x will be assigned into the variable z. Also remember
that the line </FONT><CODE>z=x;</CODE><FONT face="Times New Roman,Times">
creates two copies of the data. The original value remains in x, while z also
contains this value.</FONT></P>
<P><CODE>int x,y,z; /* Three variables */<BR>void Example(void){
<BR> x=1; /* set the value of x to 1 */
<BR> y=2; /* set the value of y
to 2 */ <BR> z=x; /* set the
value of z to the value of x (both are 1) */
<BR> x=y=z=0; /* all all three to
zero */ <BR>}</CODE></P>
<ADDRESS><FONT face="Times New Roman,Times">Listing 1-2: Simple program
illustrating C arithmetic operators</FONT></ADDRESS>
<P><FONT face="Times New Roman,Times">Next we will introduce the arithmetic
operations addition, subtraction, multiplication and division. The standard
arithmetic precedence apply. </FONT>For a detailed description of these
operations, see <A
href="http://www.ece.utexas.edu/~valvano/embed/chap5/chap5.htm">Chapter
5</A>.</P>
<P><CODE>int x,y,z; /* Three variables */<BR>void Example(void){
<BR> x=1; y=2; /* set the values of x and y */
<BR> z=x+4*y; /* arithmetic operation */
<BR> x++; /* same as
x=x+1; */ <BR> y--;
/* same as y=y-1; */
<BR> x=y<<2; /* left shift same as
x=4*y; */ <BR> z=y>>2;
/* right shift same as x=y/4; */
<BR> y+=2; /* same as
y=y+2; */ <BR>}</CODE></P>
<ADDRESS><FONT face="Times New Roman,Times">Listing 1-3: Simple program
illustrating C arithmetic operators</FONT></ADDRESS>
<P>Next we will introduce a simple conditional control structure. PORTB is an
output port, and PORTE is an input port on the 6811. For more information on
input/output ports see chapter 3 of <U>Embedded Microcomputer Systems: Real Time
Interfacing</U> by Jonathan W. Valvano, Brooks/Cole Publishing Co., 1999. The
expression <CODE>PORTE&0x04</CODE> will return 0 if PORTE bit 2 is 0 and
will return a 4 if PORTE bit 2 is 1. The expression
<CODE>(PORTE&0x04)==0</CODE> will return TRUE if PORTE bit 2 is 0 and will
return a FALSE if PORTE bit 2 is 1. The statement immediately following the
<CODE>if</CODE> will be executed if the condition is TRUE. The <CODE>else</CODE>
statement is optional. </P>
<P><CODE>#define PORTB *(unsigned char volatile *)(0x1004)<BR>#define PORTE
*(unsigned char volatile *)(0x100A)<BR>void Example(void){
<BR> if((PORTE&0x04)==0){ /* test bit 2 of PORTE */
<BR> PORTB=0;} /* if PORTE bit 2 is 0,
then make PORTB=0 */ <BR> else{
<BR> PORTB=100;} /* if PORTE bit 0 is
not 0, then make PORTB=100 */ <BR>}</CODE></P>
<ADDRESS><FONT face="Times New Roman,Times">Listing 1.4: Simple program
illustrating the C if else control structure</FONT></ADDRESS>
<P>PORTA bit 3 is another output pin on the 6811. Like the <CODE>if</CODE>
statement, the <CODE>while</CODE> statement has a conditional test (i.e.,
returns a TRUE/FALSE). The statement immediately following the
<CODE>while</CODE> will be executed over and over until the conditional test
becomes FALSE. </P>
<P><CODE>#define PORTA *(unsigned char volatile *)(0x1000)<BR>#define PORTB
*(unsigned char volatile *)(0x1004)<BR>void Example(void){ /* loop until PORTB
equals 200 */
<BR> PORTB=0;<BR> while(PORTB!=200){<BR> PORTA
= PORTA^0x08;} /* toggle PORTA bit 3 output
*/<BR> PORTB++;} /* increment PORTB
output */ <BR>}</CODE></P>
<ADDRESS><FONT face="Times New Roman,Times">Listing 1.5: Simple program
illustrating the C while control structure</FONT></ADDRESS>
<P>The <CODE>for</CODE> control structure has three parts and a body.
<CODE>for(part1;part2;part3){body;} </CODE>The first part <CODE>PORTB=0</CODE>
is executed once at the beginning. Then the body <CODE>PORTA =
PORTA^0x08;</CODE> is executed, followed by the third part <CODE>PORTB++</CODE>.
The second part <CODE>PORTB!=200</CODE> is a conditional. The body and third
part are repeated until the conditional is FALSE. For a more detailed
description of the control structures, see <A
href="http://www.ece.utexas.edu/~valvano/embed/chap6/chap6.htm">Chapter
6</A>.</P>
<P><CODE>#define PORTB *(unsigned char volatile *)(0x1004)<BR>void
Example(void){ /* loop until PORTB equals 200 */
<BR> for(PORTB=0;PORTB!=200;PORTB++){<BR> PORTA
= PORTA^0x08;} /* toggle PORTA bit 3 output */<BR> }
<BR>}</CODE></P>
<ADDRESS><FONT face="Times New Roman,Times">Listing 1.6: Simple program
illustrating the C for loop control structure</FONT></ADDRESS>
<ADDRESS> </ADDRESS>
<P><B><I><FONT face=Helvetica,Arial><A
name=PRECEDENCE></A>Precedence</FONT></I></B></P>
<P>As with all programming languages the order of the tokens is important. There
are two issues to consider when evaluating complex statements. The
<B>precedence</B> of the operator determines which operations are performed
first. In the following example, the 2*x is performed first because * has higher
precedence than + and =. The addition is performed second because + has higher
precedence than =. The assignment = is performed last. Sometimes we use
parentheses to clarify the meaning of the expression, even when they are not
needed. Therefore, the line <B>z=y+2*x;</B> could also have been written
<B>z=2*x+y;</B> or <B>z=y+(2*x);</B> or <B>z=(2*x)+y;</B>.</P>
<DIR>
<P><CODE>int example(int x, int y){ int z;
<BR> z=y+2*x;<BR>
return(z);<BR>}</CODE></P></DIR>
<P>The second issue is the <B>associativity</B>. Associativity determines the
left to right or right to left order of evaluation when multiple operations of
the precedence are combined. For example + and - have the same precedence, so
how do we evaluate the following?</P>
<DIR>
<P><CODE>z=y-2+x;</CODE></P></DIR>
<P>We know that + and - associate the left to right, this function is the same
as <B>z=(y-2)+x;</B>. Meaning the subtraction is performed first because it is
more to the left than the addition. Most operations associate left to right, but
the following table illustrates that some operators associate right to left.</P>
<P>
<TABLE cellSpacing=0 border=0>
<TBODY>
<TR>
<TD vAlign=top width="17%">Precedence </TD>
<TD vAlign=top width="55%">Operators</TD>
<TD vAlign=top width="28%">Associativity</TD></TR>
<TR>
<TD vAlign=top width="17%">highest</TD>
<TD vAlign=top width="55%">() <CODE> </CODE>[]<CODE> </CODE>.
<CODE> </CODE>-> <CODE> </CODE>++(postfix) <CODE>
</CODE>--(postfix)</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">++(prefix) <CODE> </CODE>--(prefix)
<CODE> </CODE>!~ <B>sizeof</B>(type)<CODE>
</CODE>+(unary)<CODE> </CODE>-(unary) &(address)<CODE>
</CODE>*(dereference)</TD>
<TD vAlign=top width="28%">right to left</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">*<CODE> </CODE> /
<CODE> </CODE>%</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">+ <CODE> </CODE>-</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%"><< <CODE> </CODE>>></TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">< <CODE> </CODE> <=
<CODE> </CODE>> <CODE> </CODE>>=</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">==<CODE> </CODE>!=</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">&</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">^</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">|</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">&&</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">||</TD>
<TD vAlign=top width="28%">left to right</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">? :</TD>
<TD vAlign=top width="28%">right to left</TD></TR>
<TR>
<TD vAlign=top width="17%"> </TD>
<TD vAlign=top width="55%">= <CODE> </CODE>+=
<CODE> </CODE>-=<CODE> </CODE>*=<CODE>
</CODE>/=<CODE> </CODE>%= <CODE> </CODE><<=<CODE>
</CODE>>>= <CODE> </CODE>|= <CODE> </CODE>&=
<CODE> </CODE>^= </TD>
<TD vAlign=top width="28%">right to left</TD></TR>
<TR>
<TD vAlign=top width="17%">lowest</TD>
<TD vAlign=center width="55%">,</TD>
<TD vAlign=center width="28%">left to right</TD></TR></TBODY></TABLE></P>
<P><I>Table 1-4: Precedence and associativity determine the order of
operation</I></P>
<P><I><FONT face="Times New Roman,Times">"When confused about precedence (and
aren't we all) add parentheses to clarify the expression."</FONT></I></P>
<P><I><B><FONT face=Helvetica,Arial><A
name=COMMENTS></A>Comments</FONT></B></I></P>
<P>There are two types of comments. The first type explains how to use the
software. These comments are usually placed at the top of the file, within the
header file, or at the start of a function. The reader of these comments will be
writing software that uses or calls these routines. Lines 1 and 12 in the above
<A
href="http://www.ece.utexas.edu/~valvano/embed/chap1/chap1.htm#LISTING">listing</A>
are examples of this type of comment. The second type of comments assists a
future programmer (ourselves included) in changing, debugging or extending these
routines. We usually place these comments within the body of the functions. The
comments on the right of each line in the above <A
href="http://www.ece.utexas.edu/~valvano/embed/chap1/chap1.htm#LISTING">listing</A>
are examples of the second type. For more information on writing good comments
see chapter 2 of <U>Embedded Microcomputer Systems: Real Time Interfacing</U> by
Jonathan W. Valvano, Brooks/Cole Publishing Co., 1999.</P>
<P>Comments begin with the<CODE> /*</CODE> sequence and end with the
<CODE>*/</CODE> sequence. They may extend over multiple lines as well as exist
in the middle of statements. The following is the same as
<CODE>BAUD=0x30;</CODE></P>
<DIR>
<P><CODE>BAUD /*specifies transmission rate*/=0x30/*9600
bits/sec*/;</CODE></P></DIR>
<P>ICC11 and ICC12 do allow for the use of C++ style comments (see <A
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -