Page 9 - Programming Microcontrollers in C
P. 9
x Introduction to First Edition
it is controlling. Therefore, no aspect of the machine could be hidden from
the programmer. Features like bit manipulation, bit field manipulation,
direct memory addressing, and the ability to manipulate function ad
dresses as pointers have been included in C. All of these features are used
in programming microcontrollers. In fact, C is probably the only popular
high level language that can be conveniently used for a microcontroller.
Every effort has been made to present the C aspects of programming
these machines clearly. Example programs and listings along with their
compiled results are presented whenever needed. If there are problems
hidden in the C code, these problems are explored and alternate methods
of writing the code are shown. General rules that will result in more
compact code or quicker execution of the code are developed. Example
programs that demonstrate the basis for these rules will be shown.
C is a rich and powerful language. Beyond the normal high level
language capability, C makes extensive use of pointers and address indi
rection that is usually available only with assembly language. C also
provides you with a complete set of bit operations, including bit manipula
tions and bit fields in addition to logical bit operations. In C, the program
mer knows much about the memory map which is often under program
mer control. A C programmer can readily write a byte to a control register
of a peripheral component to the computer. These assembly language-like
features of the C language serve to make C the high level language of
choice for the microcontroller programmer.
As a language, C has suffered many well-intended upgrades and
changes. It was written early in the 1970s by Dennis Ritchie of Bell
Laboratories. As originally written, C was a “free wheeling” language
with few constraints on the programmer. It was assumed that any pro
grammer using the language would be competent, so there was little need
for the controls and hand-holding done by popular compilers of the day.
Therefore, C was a typed language but it was not strongly typed. All
function returns were assumed to be integer unless otherwise specified.
Function arguments were typed, but these types were never checked for
validity when the functions were called. The programmer could specify an
integer argument and then pass a floating point number as the argument.
These kinds of errors are made easily by the best programmer, and they
are usually very difficult to find when debugging the program.
Another set of problems with the language was the library functions
that always accompanied a compiler. No standard library was specified. C
does not have built-in input/output capability. Therefore, the basic C
standard contained the specifications for a set of functions needed to
provide sensible input/output to the language. A few other features such as
a math library, a string handling library, and so forth started out with the