56/317
3 - Programming a microcontroller
take a long string of instructions, since the designer of the microcontroller has worked hard to
provide efficient instructions to do the job.
C is a structured language, meaning that code can easily be divided into blocks that can
readily become functions, or the body of looping or conditional statements, keeping the source
code legible if the writer has taken some precautions in the layout of the source text and pro-
vided useful comments.
Saying that C is a powerful language means that a short statement, taking only a single line of
source text, may perform complex operations, involving hundreds or thousands of basic in-
structions. Not only does this conciseness help keep the source text easy to read, but also it
guarantees the correct execution of the code it hides, since this code has already been exten-
sively tested by the manufacturer of the compiler.
The availability of C on many computers makes it possible for you write and test some parts of
the program directly on your PC. Most of the time, if the language constraints have been ad-
hered to, a piece of code that works on a PC will work straight off on the target microcontroller.
This is a recommended method of development, since it allows you to start writing and testing
the program before the hardware of the application has actually been built. The less debug-
ging is done directly on the hardware, the quicker the development takes.
The portability of C is also a guarantee for the future. The microcomputer world evolves so
fast, that it is not unusal to have to redesign an existing application for another microcontroller,
or to reuse parts of the code of a previous application for a future one. In such an event, the C
language allows you to reuse code with virtually no change from project to project. This fea-
ture alone, pays back the moderate investment the C compiler represents.
To remove any doubts you might have about the sincerity of these arguments and since
nothing is perfect, the drawbacks of C are be listed here.
The first drawback is that C was originally designed for mainframe computers. In these ma-
chines, there is only one memory space, that contains both code and data. In the ST7, there
is also a single memory space that contains code, data and input-outputs, but the code re-
sides in ROM and data in RAM. Standard C has no provision for specifying the address
ranges of these parts of memory.
The interrupt service routines may, and if possible, should be written in C, like any other part
of the code. However, standard C does not know anything about interrupts.
Inputs and outputs are handled in a mainframe through the operating system. Thus standard
C has no special provision for handling them.
These drawbacks would totally prevent you from using C for microcontroller appilactions if the
various implementations of C available did not provide solutions for these cases. These solu-
tions vary from implementation to implementation, and constitute the main cause of non-port-