\contentsline {chapter}{\numberline {1}Introduction}{1} \contentsline {section}{\numberline {1.1}How should you learn the C language?}{1} \contentsline {subsection}{\numberline {1.1.1}Practice!}{1} \contentsline {subsection}{\numberline {1.1.2}The \textsf {Quincy}\xspace interpreter}{2} \contentsline {subsection}{\numberline {1.1.3}Use your laboratory!}{2} \contentsline {subsection}{\numberline {1.1.4}Books about C programming}{2} \contentsline {section}{\numberline {1.2}The convention used here for C programs}{3} \contentsline {section}{\numberline {1.3}Embedded systems}{3} \contentsline {section}{\numberline {1.4}How should I use this book?}{4} \contentsline {chapter}{\numberline {2}The C programming language}{6} \contentsline {section}{\numberline {2.1}Constant values in C}{6} \contentsline {subsection}{\numberline {2.1.1}Constant integer values}{6} \contentsline {subsection}{\numberline {2.1.2}Constant character values}{7} \contentsline {subsection}{\numberline {2.1.3}Constant strings}{7} \contentsline {subsection}{\numberline {2.1.4}Named constants in C}{7} \contentsline {section}{\numberline {2.2}Integral types}{8} \contentsline {subsection}{\numberline {2.2.1}Definitions and declarations of integer-type variables}{9} \contentsline {section}{\numberline {2.3}The meaning of \textsf {\textbf {\textup {\#include}}}}{10} \contentsline {section}{\numberline {2.4}Comments in the C language}{10} \contentsline {section}{\numberline {2.5}C is case sensitive}{11} \contentsline {section}{\numberline {2.6}The use of \textmd {\textsf {\textsl {printf}()}}}{11} \contentsline {subsection}{\numberline {2.6.1}Displaying variables}{13} \contentsline {subsubsection}{Displaying an integer}{13} \contentsline {subsubsection}{Displaying a \textsf {\textbf {\textup {short}}} integer}{13} \contentsline {subsubsection}{Displaying an \textsf {\textbf {\textup {unsigned char}}}}{14} \contentsline {subsubsection}{Displaying a \textsf {\textbf {\textup {long int}}}}{15} \contentsline {subsection}{\numberline {2.6.2}Displaying a string}{15} \contentsline {subsection}{\numberline {2.6.3}A warning about the format characters and \textmd {\textsf {\textsl {printf}()}}}{15} \contentsline {subsubsection}{A summary of the format characters}{15} \contentsline {subsection}{\numberline {2.6.4}Example programs showing the use of \textmd {\textsf {\textsl {printf}()}}}{16} \contentsline {section}{\numberline {2.7}Expressions and operators}{17} \contentsline {subsection}{\numberline {2.7.1}Expressions and operators---C compared with Pascal}{17} \contentsline {subsection}{\numberline {2.7.2}The assignment operator \textmd {\textsf {=}}}{19} \contentsline {subsubsection}{The assignment operator and \IO }{20} \contentsline {subsection}{\numberline {2.7.3}The compound assignment operators}{21} \contentsline {subsection}{\numberline {2.7.4}The relational operators}{21} \contentsline {subsection}{\numberline {2.7.5}The increment and decrement operators, \textmd {\textsf {++}} and \textmd {\textsf {$--$}}}{21} \contentsline {subsection}{\numberline {2.7.6}The logical operators}{21} \contentsline {subsubsection}{The value of \ensuremath {\@mathvf {true}} and \ensuremath {\@mathvf {false}}}{23} \contentsline {subsection}{\numberline {2.7.7}The bitwise operators and bit patterns}{24} \contentsline {subsubsection}{The C bitwise AND operator, \textmd {\textsf {\&}}}{24} \contentsline {subsubsection}{Using bitwise AND to examine the ROW bits in the keypad}{25} \contentsline {subsubsection}{The C bitwise OR operator, \textmd {\textsf {\textbar }}}{26} \contentsline {subsubsection}{The C bitwise NOT operator, $\sim $}{27} \contentsline {subsubsection}{The C bitwise exclusive-OR operator, {\fontencoding {OMX}\fontfamily {cmex}\fontseries {m}\fontshape {n}\selectfont \ignorespaces \char '142}}{27} \contentsline {subsubsection}{The shift operators, \Lsh \ and \Rsh }{29} \contentsline {section}{\numberline {2.8}Statements}{30} \contentsline {subsection}{\numberline {2.8.1}The null statement}{30} \contentsline {subsection}{\numberline {2.8.2}Compound statements}{31} \contentsline {subsection}{\numberline {2.8.3}\textsf {\textbf {\textup {if}}} statement}{31} \contentsline {subsection}{\numberline {2.8.4}The \textsf {\textbf {\textup {while}}} statement}{33} \contentsline {subsection}{\numberline {2.8.5}The \textsf {\textbf {\textup {for}}} statement}{33} \contentsline {subsection}{\numberline {2.8.6}The \textsf {\textbf {\textup {do}}} statement}{37} \contentsline {subsection}{\numberline {2.8.7}Nested loops}{37} \contentsline {subsection}{\numberline {2.8.8}The \textsf {\textbf {\textup {break}}} statement}{38} \contentsline {subsection}{\numberline {2.8.9}The \textsf {\textbf {\textup {continue}}} statement}{38} \contentsline {section}{\numberline {2.9}Arrays}{40} \contentsline {subsection}{\numberline {2.9.1}Array indexing errors}{43} \contentsline {section}{\numberline {2.10}Functions}{44} \contentsline {subsection}{\numberline {2.10.1}Function return values}{49} \contentsline {subsubsection}{The return type of \textmd {\textsf {\textit {main}()}}}{50} \contentsline {subsection}{\numberline {2.10.2}The function parameter list}{50} \contentsline {section}{\numberline {2.11}Indentation of your programs}{53} \contentsline {chapter}{\numberline {3}Input and output}{54} \contentsline {section}{\numberline {3.1}I/O ports and the 68230 parallel interface/\penalty \z@ {}timer chip}{55} \contentsline {subsection}{\numberline {3.1.1}The interface card}{56} \contentsline {section}{\numberline {3.2}The \acro {FLIGHT-68K}\xspace microprocessor\xspace board}{56} \contentsline {subsection}{\numberline {3.2.1}Octal tristate buffers on the interface board}{58} \contentsline {subsection}{\numberline {3.2.2}Address decoder for the 68230 \acro {PI\Slash {}{}T}\xspace }{58} \contentsline {subsubsection}{Partial address decoding}{58} \contentsline {subsection}{\numberline {3.2.3}The other connections between the 68000 and the \acro {PI\Slash {}{}T}\xspace }{59} \contentsline {subsection}{\numberline {3.2.4}The 68230 parallel interface/\penalty \z@ {}timer chip}{59} \contentsline {subsubsection}{The seven segment display}{60} \contentsline {subsection}{\numberline {3.2.5}The dip switches}{60} \contentsline {subsection}{\numberline {3.2.6}Register addresses in the 68230 \acro {PI\Slash {}{}T}\xspace on the \acro {FLIGHT-68K}\xspace }{61} \contentsline {paragraph}{The \acro {PI\Slash {}{}T}\xspace has many modes:}{61} \contentsline {paragraph}{Submode 1X}{61} \contentsline {subsection}{\numberline {3.2.7}Initialising the \acro {PI\Slash {}{}T}\xspace for bit \IO }{63} \contentsline {paragraph}{What does it all mean?}{63} \contentsline {subsubsection}{Choosing inputs and outputs: the data direction registers}{64} \contentsline {subsubsection}{Initialising the \acro {PI\Slash {}{}T}\xspace for bit \IO : a summary}{65} \contentsline {subsection}{\numberline {3.2.8}How the interface card is connected to the parallel chip}{65} \contentsline {section}{\numberline {3.3}The keypad}{67} \contentsline {subsection}{\numberline {3.3.1}Scanning the keypad}{68} \contentsline {subsubsection}{The method}{68} \contentsline {subsection}{\numberline {3.3.2}Debouncing the keys}{70} \contentsline {subsection}{\numberline {3.3.3}How to detect if any key has been pressed}{70} \contentsline {chapter}{\numberline {A}The operators in C and their precedence}{72} \contentsline {section}{\numberline {A.1}Operator precedence}{72} \contentsline {chapter}{\numberline {B}The \acro {ASCII}\xspace code for representing characters}{76} \contentsline {chapter}{\numberline {C}The memory map of the 68000 system in the laboratory}{78} \contentsline {chapter}{\numberline {D}The compile, assemble and link process}{80} \contentsline {section}{\numberline {D.1}The preprocessor}{80} \contentsline {section}{\numberline {D.2}The translator (or the main part of the C compiler)}{82} \contentsline {section}{\numberline {D.3}The listing file and the map file}{82} \contentsline {section}{\numberline {D.4}The assembler}{82} \contentsline {section}{\numberline {D.5}Relocatable object file}{83} \contentsline {section}{\numberline {D.6}The linker}{83} \contentsline {subsection}{\numberline {D.6.1}The startup code}{83} \contentsline {section}{\numberline {D.7}The library}{84} \contentsline {subsection}{\numberline {D.7.1}The file \texttt {flight.h}}{84} \contentsline {subsection}{\numberline {D.7.2}The library function \ensuremath {\@mathvf {DelayMs}}\textsf {\textmd {\textup {()}}}}{84} \contentsline {subsection}{\numberline {D.7.3}The library function \ensuremath {\@mathvf {kbhit}}\textsf {\textmd {\textup {()}}}}{85} \contentsline {subsection}{\numberline {D.7.4}The library function \ensuremath {\@mathvf {getch}}\textsf {\textmd {\textup {()}}}}{86} \contentsline {subsection}{\numberline {D.7.5}The library function \ensuremath {\@mathvf {WriteSevenSegment}}\textsf {\textmd {\textup {()}}}}{87} \contentsline {subsection}{\numberline {D.7.6}The library function \ensuremath {\@mathvf {ReadKey}}\textsf {\textmd {\textup {()}}}}{87} \contentsline {section}{\numberline {D.8}The \textsf {make} program}{88} \contentsline {chapter}{\numberline {E}Why C and not assembly language?}{90} \contentsline {chapter}{\numberline {F}The \textsf {Quincy}\xspace interpreter---get it and use it!}{92} \contentsline {section}{\numberline {F.1}What is \textsf {Quincy}\xspace ?}{92} \contentsline {subsection}{\numberline {F.1.1}What is an interpreter?}{92} \contentsline {subsection}{\numberline {F.1.2}What can I do with \textsf {Quincy}\xspace ?}{93} \contentsline {section}{\numberline {F.2}How to get a copy of \textsf {Quincy}\xspace }{93} \contentsline {chapter}{\numberline {G}Compiler errors: they are there to help you}{95} \contentsline {section}{\numberline {G.1}What do I do if there are lots of error messages?}{95} \contentsline {section}{\numberline {G.2}Error messages from different parts of the compiler}{96} \contentsline {subsection}{\numberline {G.2.1}Error messages from the preprocessor}{96} \contentsline {subsection}{\numberline {G.2.2}Error messages from the main part of the compiler}{97} \contentsline {subsection}{\numberline {G.2.3}Error messages from the linker}{98} \contentsline {subsection}{\numberline {G.2.4}The ``my computer looks sick!'' error message}{99} \contentsline {chapter}{\numberline {H}Editing, compiling, downloading and running a program}{101} \contentsline {section}{\numberline {H.1}The program design}{101} \contentsline {section}{\numberline {H.2}The general procedure for editing, compiling, downloading and running a program}{101} \contentsline {section}{\numberline {H.3}Editing the program}{102} \contentsline {section}{\numberline {H.4}Compiling your program}{104} \contentsline {section}{\numberline {H.5}Downloading the program}{104} \contentsline {section}{\numberline {H.6}Running the program}{105} \contentsline {chapter}{\numberline {I}Solutions to exercises}{106} \contentsline {chapter}{References}{119} \contentsline {chapter}{Index}{121}