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

📄 chapter 2 tokens -- valvano.htm

📁 介绍了在嵌入式系统中如何用c来设计嵌入式软件
💻 HTM
📖 第 1 页 / 共 3 页
字号:
    <TD vAlign=top width="10%"><CODE>US</CODE></TD>
    <TD vAlign=top width="11%"><CODE>/</CODE></TD>
    <TD vAlign=top width="10%"><CODE>?</CODE></TD>
    <TD vAlign=top width="11%"><CODE>O</CODE></TD>
    <TD vAlign=top width="10%"><CODE>_</CODE></TD>
    <TD vAlign=top width="11%"><CODE>o</CODE></TD>
    <TD vAlign=top width="14%"><CODE>DEL</CODE></TD></TR></TBODY></TABLE></P>
<ADDRESS>Table 2-1. ASCII Character codes.</ADDRESS>
<P>The first 32 (values 0 to 31 or $00 to $1F) and the last one (127=$7F) are 
classified as <I>control characters</I>. Codes 32 to 126 (or $20 to $7E) include 
the "normal" characters. Normal characters are divided into</P>
<DIR>
<P>the space character (32=$20), <CODE><BR></CODE>the numeric digits 0 to 9 (48 
to 57 or $30 to $39), <CODE><BR></CODE>the uppercase alphabet A to Z (65 to 90 
or $41 to $5A), <CODE><BR></CODE>the lowercase alphabet a to z (97 to122 or $61 
to $7A), and <CODE><BR></CODE>the special characters (all the rest). </P></DIR>
<P><B><I><FONT face=Helvetica,Arial><A 
name=CONSTANTS></A>Literals</FONT></I></B></P>
<P><I>Numeric literals</I><FONT face="Times New Roman,Times"> consist of an 
uninterrupted sequence of digits delimited by white spaces or special characters 
(operators or punctuation). Although ICC12 and Hiware do support floating point, 
this document will not cover it. The use of floating point requires a 
substantial about of program memory and execution time, therefore most 
applications should be implemented using integer math. Consequently the period 
will not appear in numbers as described in this document. For more information 
about numbers see the sections on <A 
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#DECIMAL">decimals</A>, 
<A 
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#OCTAL">octals</A>, 
or <A 
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#HEX">hexadecimals</A> 
in Chapter 3.</FONT></P>
<P><I>Character literals</I><FONT face="Times New Roman,Times"> are written by 
enclosing an ASCII character in apostrophes (single quotes). We would 
write</FONT><CODE> 'a'</CODE><FONT face="Times New Roman,Times"> for a character 
with the ASCII value of the lowercase a (97). The control characters can also be 
defined as constants. For example </FONT><CODE>'\t'</CODE><FONT 
face="Times New Roman,Times"> is the tab character. 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></P>
<P><I>String literals</I><FONT face="Times New Roman,Times"> are written as a 
sequence of ASCII characters bounded by quotation marks (double quotes). Thus, 
"ABC" describes a string of characters containing the first three letters of the 
alphabet in uppercase. 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.</FONT></P>
<P><B><I><FONT face=Helvetica,Arial><A 
name=KEYWORDS></A>Keywords</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">There are some predefined tokens, called 
</FONT><I>keywords</I><FONT face="Times New Roman,Times">, that have specific 
meaning in C programs. The reserved words we will cover in this document 
are:</FONT></P>
<P>
<TABLE cellSpacing=0 width=392 border=0>
  <TBODY>
  <TR>
    <TD vAlign=top width="13%">
      <ADDRESS><CODE>keyword</CODE></ADDRESS></TD>
    <TD vAlign=top width="87%">
      <ADDRESS><CODE>meaning</CODE></ADDRESS></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>asm</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Insert assembly code</CODE></TD></TR>
  <TR>
    <TD vAlign=center width="13%"><CODE>auto</CODE></TD>
    <TD vAlign=center width="87%"><CODE>Specifies a variable as automatic 
      (created on the stack)</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>break</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Causes the program control structure to 
      finish</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>case</CODE></TD>
    <TD vAlign=top width="87%"><CODE>One possibility within a switch 
      statement</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>char</CODE></TD>
    <TD vAlign=top width="87%"><CODE>8 bit integer</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>const</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Defines parameter as constant in 
      ROM</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>continue</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Causes the program to go to beginning of 
      loop </CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>default</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Used in switch statement for all other 
      cases</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>do</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Used for creating program 
  loops</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>double</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Specifies variable as double precision 
      floating point</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>else</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Alternative part of a 
    conditional</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>extern</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Defined in another module</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>float</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Specifies variable as single precision 
      floating point</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>for</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Used for creating program 
  loops</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>goto</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Causes program to jump to specified 
      location</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>if</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Conditional control 
structure</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>int</CODE></TD>
    <TD vAlign=top width="87%"><CODE>16 bit integer</CODE> (same as short on 
      the 6811 and 6812)</TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>long</CODE></TD>
    <TD vAlign=top width="87%"><CODE>32 bit integer</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>register</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Specifies how to implement a 
    local</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>return</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Leave function</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>short</CODE></TD>
    <TD vAlign=top width="87%"><CODE>16 bit integer</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>signed</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Specifies variable as signed 
      (default)</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>sizeof</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Built-in function returns the size of an 
      object</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>static</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Stored permanently in memory, accessed 
      locally</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>struct</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Used for creating data 
    structures</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>switch</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Complex conditional control 
      structure</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>typedef</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Used to create new data 
types</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>unsigned</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Always greater than or equal to 
      zero</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>void</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Used in parameter list to mean no 
      parameter</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>volatile</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Can change implicitly</CODE></TD></TR>
  <TR>
    <TD vAlign=top width="13%"><CODE>while</CODE></TD>
    <TD vAlign=top width="87%"><CODE>Used for creating program 
  loops</CODE></TD></TR></TBODY></TABLE></P>
<ADDRESS>&nbsp;Table 2-2. Keywords have predefined meanings.</ADDRESS>
<P><FONT face="Times New Roman,Times">Did you notice that all of the keywords in 
C are lowercase? Notice also that as a matter of style, I used a mixture of 
upper and lowercase for the names I created, and all uppercase for the I/O 
ports. It is a good programming practice not to use these keywords for your 
variable or function names.</FONT></P>
<P><B><I><FONT face=Helvetica,Arial><A name=NAMES></A>Names</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">We use <I>names</I> to identify our 
variables, functions, and macros. ICC11/ICC12 names may be up to 31 characters 
long. Hiware names may be up to xxx characters long. Names must begin with a 
letter or underscore and the remaining characters must be either letters or 
digits. We can use a mixture of upper and lower case or the underscore character 
to create self-explaining symbols. E.g.,</FONT></P>
<P><CODE>time_of_day&nbsp;&nbsp;&nbsp;&nbsp;go_left_then_stop</CODE></P>
<P><CODE>TimeOfDay&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GoLeftThenStop</CODE></P>
<P><FONT face="Times New Roman,Times">The careful selection of names goes a long 
way to making our programs more readable. Names may be written with both upper 
and lowercase letters. The names are case sensitive. Therefore the following 
names are different:</FONT></P>
<DIR>
<P><CODE>thetemperature<BR>THETEMPERATURE<BR>TheTemperature</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">The practice of naming macros in uppercase 
calls attention to the fact that they are not variable names but defined 
symbols. Remember the</FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap1/chap1.htm#LISTING"> I/O 
port names</A><FONT face="Times New Roman,Times"> are implemented as macros in 
the header files HC11.h and HC12.h.</FONT></P>
<P><FONT face="Times New Roman,Times">Every global name defined with the 
ICC11/ICC12 compiler generates an assembly language label of the same name, but 
preceded by an underscore. The purpose of the underscore is to avoid clashes 
with the assembler's reserved words. So, as a matter of practice, we should not 
ordinarily name globals with leading underscores. Hiware labels will not include 
the underscore. For examples of this naming convention, observe the assembly 
generated by the compiler (either the assembly itself in the *.s file or the 
listing file *.lst file.) These assembly names are important during the 
debugging stages. We can use the map file to get the absolute addresses for 
these labels, then use the debugger to observe and modify their 
contents.</FONT></P>
<P><FONT face="Times New Roman,Times">Since the Imagecraft compiler adds its own 
underscore, names written with a leading underscore appear in the assembly file 
with two leading underscores.</FONT></P>
<P><FONT face="Times New Roman,Times">Developing a naming convention will avoid 
confusion. Possible ideas to consider include:</FONT></P>
<P><FONT face="Times New Roman,Times">1. Start every variable name with its 
type. E.g., </FONT></P>
<DIR>
<P><FONT face="Times New Roman,Times">b means boolean 
true/false</FONT><CODE><BR></CODE><FONT face="Times New Roman,Times">n means 8 
bit signed integer</FONT><CODE><BR></CODE><FONT face="Times New Roman,Times">u 
means 8 bit unsigned integer</FONT><CODE><BR></CODE><FONT 
face="Times New Roman,Times">m means 16 bit signed 
integer</FONT><CODE><BR></CODE><FONT face="Times New Roman,Times">v means 16 bit 
unsigned integer</FONT><CODE><BR></CODE><FONT face="Times New Roman,Times">c 
means 8 bit ASCII character</FONT><CODE><BR></CODE><FONT 
face="Times New Roman,Times">s means null terminated ASCII 
string</FONT></P></DIR>
<P><FONT face="Times New Roman,Times">2. Start every local variable with "the" 
or "my"</FONT></P>
<P><FONT face="Times New Roman,Times">3. Start every global variable and 
function with associated file or module name. In the following example the names 
all begin with </FONT><CODE>Bit_</CODE><FONT face="Times New Roman,Times">. 
Notice how similar this naming convention recreates the look and feel of the 
modularity achieved by classes in C++. E.g.,</FONT> </P>
<DIR>
<P><CODE><A name=BITFIFO></A>/* 
**********file=Bit.c*************<BR>&nbsp;&nbsp;&nbsp;Pointer implementation of 
the a Bit_Fifo<BR>&nbsp;&nbsp;&nbsp;These routines can be used to save (Bit_Put) 
and<BR>&nbsp;&nbsp;&nbsp;recall (Bit_Get) binary data 1 bit at a time (bit 
streams)<BR>&nbsp;&nbsp;&nbsp;Information is saved/recalled in a first in first 
out manner<BR>&nbsp;&nbsp;&nbsp;Bit_FifoSize is the number of 16 bit words in 
the Bit_Fifo<BR>&nbsp;&nbsp;&nbsp;The Bit_Fifo is full when it has 
16*Bit_FifoSize-1 bits */<BR>#define Bit_FifoSize4<BR>// 16*4-1=31 bits of 
storage<BR>unsigned short Bit_Fifo[Bit_FifoSize]; // storage for Bit 
Stream<BR>struct Bit_Pointer{<BR>&nbsp;&nbsp;&nbsp;unsigned short Mask; // 
0x8000, 0x4000,...,2,1<BR>&nbsp;&nbsp;&nbsp;unsigned short *WPt;}; // Pointer to 
word containing bit<BR>typedef struct Bit_Pointer 
Bit_PointerType;<BR>Bit_PointerType Bit_PutPt; // Pointer of where to put 

⌨️ 快捷键说明

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