\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}{4} \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}{8} \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}{11} \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}{14} \contentsline {subsubsection}{Displaying an \textsf {\textbf {\textup {unsigned char}}}}{15} \contentsline {subsubsection}{Displaying a \textsf {\textbf {\textup {long int}}}}{15} \contentsline {subsection}{\numberline {2.6.2}Displaying a string}{16} \contentsline {subsection}{\numberline {2.6.3}A warning about the format characters and \textmd {\textsf {\textsl {printf}()}}}{16} \contentsline {subsubsection}{A summary of the format characters}{16} \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}{18} \contentsline {subsection}{\numberline {2.7.2}The assignment operator \textmd {\textsf {=}}}{20} \contentsline {subsubsection}{The assignment operator and \IO }{21} \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 {$--$}}}{23} \contentsline {subsection}{\numberline {2.7.6}The logical operators}{23} \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 {\&}}}{25} \contentsline {subsubsection}{Using bitwise AND to examine the ROW bits in the keypad}{26} \contentsline {subsubsection}{The C bitwise OR operator, \textmd {\textsf {\textbar }}}{27} \contentsline {subsubsection}{The C bitwise NOT operator, $\sim $}{28} \contentsline {subsubsection}{The C bitwise exclusive-OR operator, {\fontencoding {OMX}\fontfamily {cmex}\fontseries {m}\fontshape {n}\selectfont \ignorespaces \char '142}}{29} \contentsline {subsubsection}{The shift operators, \Lsh \ and \Rsh }{30} \contentsline {section}{\numberline {2.8}Statements}{31} \contentsline {subsection}{\numberline {2.8.1}The null statement}{31} \contentsline {subsection}{\numberline {2.8.2}Compound statements}{31} \contentsline {subsection}{\numberline {2.8.3}\textsf {\textbf {\textup {if}}} statement}{32} \contentsline {subsection}{\numberline {2.8.4}The \textsf {\textbf {\textup {while}}} statement}{34} \contentsline {subsection}{\numberline {2.8.5}The \textsf {\textbf {\textup {for}}} statement}{34} \contentsline {subsection}{\numberline {2.8.6}The \textsf {\textbf {\textup {do}}} statement}{37} \contentsline {subsection}{\numberline {2.8.7}Nested loops}{38} \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}{39} \contentsline {section}{\numberline {2.9}Arrays}{41} \contentsline {subsection}{\numberline {2.9.1}Array indexing errors}{43} \contentsline {section}{\numberline {2.10}Functions}{45} \contentsline {subsection}{\numberline {2.10.1}Function return values}{50} \contentsline {subsubsection}{The return type of \textmd {\textsf {\textit {main}()}}}{51} \contentsline {subsection}{\numberline {2.10.2}The function parameter list}{52} \contentsline {section}{\numberline {2.11}Indentation of your programs}{54} \contentsline {chapter}{\numberline {3}Input and output}{55} \contentsline {section}{\numberline {3.1}I/O ports and the 68230 parallel interface/\penalty \z@ {}timer chip}{56} \contentsline {subsection}{\numberline {3.1.1}The interface card}{57} \contentsline {section}{\numberline {3.2}The \acro {FLIGHT-68K}\xspace microprocessor\xspace board}{58} \contentsline {subsection}{\numberline {3.2.1}Octal tristate buffers on the interface board}{59} \contentsline {subsection}{\numberline {3.2.2}Address decoder for the 68230 \acro {PI\Slash {}{}T}\xspace }{59} \contentsline {subsubsection}{Partial address decoding}{59} \contentsline {subsection}{\numberline {3.2.3}The other connections between the 68000 and the \acro {PI\Slash {}{}T}\xspace }{60} \contentsline {subsection}{\numberline {3.2.4}The 68230 parallel interface/\penalty \z@ {}timer chip}{60} \contentsline {subsubsection}{The seven segment display}{61} \contentsline {subsection}{\numberline {3.2.5}The dip switches}{62} \contentsline {subsection}{\numberline {3.2.6}Register addresses in the 68230 \acro {PI\Slash {}{}T}\xspace on the \acro {FLIGHT-68K}\xspace }{62} \contentsline {paragraph}{The \acro {PI\Slash {}{}T}\xspace has many modes:}{62} \contentsline {paragraph}{Submode 1X}{64} \contentsline {subsection}{\numberline {3.2.7}Initialising the \acro {PI\Slash {}{}T}\xspace for bit \IO }{64} \contentsline {paragraph}{What does it all mean?}{64} \contentsline {subsubsection}{Choosing inputs and outputs: the data direction registers}{66} \contentsline {subsubsection}{Initialising the \acro {PI\Slash {}{}T}\xspace for bit \IO : a summary}{66} \contentsline {subsection}{\numberline {3.2.8}How the interface card is connected to the parallel chip}{67} \contentsline {section}{\numberline {3.3}The keypad}{68} \contentsline {subsection}{\numberline {3.3.1}Scanning the keypad}{68} \contentsline {subsubsection}{The method}{69} \contentsline {subsection}{\numberline {3.3.2}Debouncing the keys}{71} \contentsline {subsection}{\numberline {3.3.3}How to detect if any key has been pressed}{72} \contentsline {chapter}{\numberline {A}The operators in C and their precedence}{74} \contentsline {section}{\numberline {A.1}Operator precedence}{74} \contentsline {chapter}{\numberline {B}The \acro {ASCII}\xspace code for representing characters}{77} \contentsline {chapter}{\numberline {C}The memory map of the 68000 system in the laboratory}{79} \contentsline {chapter}{\numberline {D}The compile, assemble and link process}{81} \contentsline {section}{\numberline {D.1}The preprocessor}{81} \contentsline {section}{\numberline {D.2}The translator (or the main part of the C compiler)}{83} \contentsline {section}{\numberline {D.3}The listing file and the map file}{83} \contentsline {section}{\numberline {D.4}The assembler}{83} \contentsline {section}{\numberline {D.5}Relocatable object file}{84} \contentsline {section}{\numberline {D.6}The linker}{84} \contentsline {subsection}{\numberline {D.6.1}The startup code}{84} \contentsline {section}{\numberline {D.7}The library}{85} \contentsline {subsection}{\numberline {D.7.1}The file \texttt {flight.h}}{85} \contentsline {subsection}{\numberline {D.7.2}The library function \ensuremath {\@mathvf {DelayMs}}\textsf {\textmd {\textup {()}}}}{86} \contentsline {subsection}{\numberline {D.7.3}The library function \ensuremath {\@mathvf {kbhit}}\textsf {\textmd {\textup {()}}}}{87} \contentsline {subsection}{\numberline {D.7.4}The library function \ensuremath {\@mathvf {getch}}\textsf {\textmd {\textup {()}}}}{87} \contentsline {subsection}{\numberline {D.7.5}The library function \ensuremath {\@mathvf {WriteSevenSegment}}\textsf {\textmd {\textup {()}}}}{88} \contentsline {subsection}{\numberline {D.7.6}The library function \ensuremath {\@mathvf {ReadKey}}\textsf {\textmd {\textup {()}}}}{89} \contentsline {section}{\numberline {D.8}The \textsf {make} program}{90} \contentsline {chapter}{\numberline {E}Why C and not assembly language?}{92} \contentsline {chapter}{\numberline {F}The \textsf {Quincy}\xspace interpreter---get it and use it!}{94} \contentsline {section}{\numberline {F.1}What is \textsf {Quincy}\xspace ?}{94} \contentsline {subsection}{\numberline {F.1.1}What is an interpreter?}{94} \contentsline {subsection}{\numberline {F.1.2}What can I do with \textsf {Quincy}\xspace ?}{95} \contentsline {section}{\numberline {F.2}How to get a copy of \textsf {Quincy}\xspace }{95} \contentsline {chapter}{\numberline {G}Compiler errors: they are there to help you}{97} \contentsline {section}{\numberline {G.1}What do I do if there are lots of error messages?}{98} \contentsline {section}{\numberline {G.2}Error messages from different parts of the compiler}{98} \contentsline {subsection}{\numberline {G.2.1}Error messages from the preprocessor}{98} \contentsline {subsection}{\numberline {G.2.2}Error messages from the main part of the compiler}{99} \contentsline {subsection}{\numberline {G.2.3}Error messages from the linker}{100} \contentsline {chapter}{\numberline {H}Editing, compiling, downloading and running a program}{102} \contentsline {section}{\numberline {H.1}The program design}{102} \contentsline {section}{\numberline {H.2}The general procedure for editing, assembling, downloading and running an assembly language program}{102} \contentsline {section}{\numberline {H.3}Editing the program}{103} \contentsline {section}{\numberline {H.4}Compiling your program}{105} \contentsline {section}{\numberline {H.5}Downloading the program}{105} \contentsline {section}{\numberline {H.6}Running the program}{106} \contentsline {chapter}{\numberline {I}Solutions to exercises}{107} \contentsline {chapter}{References}{120} \contentsline {chapter}{Index}{122}