Page 155 - Programming Microcontrollers in C
P. 155
140 Chapter 3 What Are Microcontrollers?
should have a comment header that explains what the function
does in the program. The header should contain a list of the func
tion arguments and a record of modifications to the function. If
there is some nonobvious code in the program, this code should
be documented to explain what is happening.
9. Kernighan and Ritchie, the developers of C, gave us a language
that is rich with shorthand notation. While often useful, this is a
tool that can be badly misused. Don’t write code in a convoluted
manner using tricky shorthand notations so that nobody can un
derstand what you’re trying to do. You might be the one who has
to maintain the resulting code a decade later!
10.Always create mnemonics for addresses and bit variables used in
the program.
11.Use assembly language only when C cannot accomplish neces
sary operations.
12.Microcontrollers usually work with unsigned variables. Often the
microcontroller can create more efficient code when doing compares
with unsigned variables. Therefore, use unsigned variables every
where unless it is necessary to use signed variables. A few typedef
operations will help keep the use of unsigned variables in mind:
typedef WORD unsigned int;
typedef BYTE unsigned char;
In writing code for a microcontroller, you must be able to deal
with ports at some specific address location, the bits within these
ports, control registers, certain data registers, and other important
control functions. The program can be easily written if the names of
all registers, ports, bits, and so forth are taken from the component
specification and not made subject to the whim of the programmer.
Therefore, if all of the ports and bits have the name given in the
microcontroller reference manual, then any variable used will be
defined in one place and the programmer need only find the appro
priate register along with the definition of its bits in the reference
manual to understand what is being done in the program. The refer
ence manual becomes part of your documentation.
We saw back in Chapter 2 how to create mnemonics for address
locations. It is necessary for programmers to write a series of define