- •1 Introduction to C
- •1.1 Some Simple Programs
- •1.2 Names
- •1.3 Types and Type Declarations
- •1.4 Storage Classes, Linkage, and Scope
- •1.5 Character Constants
- •1.6 Arrays
- •1.7 Other types
- •1.8 Operators and Expressions
- •1.9 Increment and Decrement Operators
- •1.10 Precedence and Associativity
- •1.11 Program Flow and Control
- •1.12 Functions
- •1.13 Recursion
- •1.14 Summary
- •2 Advanced C Topics
- •2.1 Pointers
- •2.2 Multidimensional Arrays
- •2.3 Structures
- •2.4 More Structures
- •2.5 Input and Output
- •2.6 Memory Management
- •2.7 Miscellaneous Functions
- •2.8 Summary
- •3 What Are Microcontrollers?
- •3.1 Microcontroller Memory
- •3.3 Programming Microcontrollers
- •3.4 Coding Tips for Microcontrollers
- •4.1 Microcontroller Memory
- •4.2 Timers
- •4.4 Pulse Width Modulator System
- •4.5 Other Program Items
- •4.6 Summary
- •5.1 Header File
- •5.2 Sorting Programs
- •5.3 Data Compression
- •5.4 Timer Operations
- •5.5 Summary
- •6 Large Microcontrollers
- •6.3 A Pulse Width Modulation Program
- •6.4 Cosmic MC68HC16 Compiler
- •6.6 Digital Signal Processor Operations
- •6.7 Other MC68HC16 Considerations
- •7.1 Numeric Encoding
- •7.2 Numeric Decoding
- •7.3 Coding the alpha data
- •7.4 The Monitor Program
- •7.5 The SAVEIT() Routine
- •7.6 The printout() and the printafter() Functions
- •7.7 Reset
- •7.9 Putting It All Together
- •7.10 Summary
- •8 MCORE, a RISC Machine
- •8.1 Delay Routine
- •8.2 Delays Revisited
- •8.4 Handling Interrupts
- •8.5 A Clock Program
- •8.6 Keyboard
- •8.7 Integrating Keyboard and Clock
- •8.8 Adding a Display
- •8.9 Summary
8 Chapter 1 Introduction to C
of course, is less than 11, so the statement following the while will be executed again and new values will be printed to the screen. This sequence will be repeated until the incremented value for i equals 11, at which time i<11 will be FALSE. At that point in the pro gram, the statement following the while will be skipped, and the program will have reached its end. The result of executing the above program is shown in the following table:
i |
i |
i |
|
squared |
cubed |
1 |
1 |
1 |
2 |
4 |
8 |
3 |
9 |
27 |
4 |
16 |
64 |
5 |
25 |
125 |
6 |
36 |
216 |
7 |
49 |
343 |
8 |
64 |
512 |
9 |
81 |
729 |
10 |
100 |
1000 |
EXERCISES
1.Write, compile, and execute each of the example programs shown in this section.
2.Write a program to calculate the Fahrenheit temperature for the Cel sius values between 0° degrees and 100° in steps of 10° each. The conversion formula is F=9*C/5+32. Use integer variables, and ex amine the result when you use F=C*(9/ 5) + 32. What went wrong?
Names
Variables, constants and functions in C are named, and the pro gram controls operations on these named variables and constants. Variables and constants are called operands. Names can be as many as 31 characters long. The characters that make up the name can be the upper and the lower case letters, the digits 0 through 9, and the underscore character ‘_’. There are several defined constants and functions that are used by the compiler. All of these names begin
Names 9
with an underscore. Because of this convention, you should avoid the use of an underscore as the first character for either function or variable names in your code. This approach will completely avoid name conflict with these hidden or unexpected names. Compilers usually allow the names to be unique in the first 31 characters. Un fortunately, some linkers used to link various program modules require that the names be unique in the first six or eight characters, depend ing on the linker.
C has a collection of keywords that cannot be used for names. These keywords are listed below:
KEYWORDS
auto |
double |
int |
struct |
break |
else |
long |
switch |
case |
enum |
register |
typedef |
char |
extern |
return |
union |
const |
float |
short |
unsigned |
continue |
for |
signed |
void |
default |
goto |
sizeof |
volatile |
do |
if |
static |
while |
Types and Type Declarations
C has only a few built-in types. Here they are:
char—is usually eight bits. The character is the smallest stor age unit.
int—an integer is usually the size of the basic unit of storage for the machine. An int must be at least 16 bits wide.
float—a single precision floating-point number.
double—a double precision floating-point number.
Additional qualifiers are used to modify the basic types. These qualifiers include:
short—modifies an int, and is a variable whose width is no greater than that of the int. For example, with a compiler with a 32 bit int a short int could be 16 bits. You will find examples where short and int are the same size.
10 Chapter 1 Introduction to C
long—modifies an int, and is a variable size whose width is no less than that of an int. For example, on a 16-bit machine, an int might be 16 bits, and a long int could be 32 bits. long can also modify a double to specify an extended precision floating-point number. You will find examples where a long and an int are the same size.
signed—modifies all integral numbers and produces a range of numbers that contains both positive and negative numbers. For example, if the type char is 8 bits, a signed char can contain the range of numbers –128 to +127. Default for char and int is signed when they are declared.
unsigned—modifies all integral numbers and produces a range of numbers that are positive only. For example, if the type char is 8 bits, an unsigned char can contain the range of numbers 0 to +255. It is not necessary to include the type int with the qualifiers short or long. Thus, the following statements are the same:
long int a,c; short int d;
and
long a,c; short d;
When a variable is defined, space is allocated in memory for its storage. The basic variable size is implementation dependent, and especially for microcontrollers, you will find that this variability will show up when you change from one microcomputer to another.
Each variable must be defined prior to being used. A variable may be defined at the beginning of any code block, and the variable’s scope is the block in which it is defined. When the block in which the variable is defined is exited, the variable goes out of existence. There is no problem with defining variables with the same name in differ ent blocks. The compiler will make certain that these variables do not get mixed up in the execution of the code.
An additional qualifier is const. When const is used as a quali fier on the declaration of any variable, an initialization value must be declared. This value cannot be changed by the program. Therefore the declaration
Types and Type Declarations |
11 |
|
|
const double PI = 3.14159265;
will create the value for the mathematical constant pi and store it in the location provided for PI. Any attempt to change the value of PI by the program will cause compiler error.
Conventions for writing constants are straightforward. A simple number with no decimal point is an int. To make a number long, you must suffix it with an l or an L. For example, 6047 is an int and 6047L is a long. The u or U suffix on a number will cause creation of a proper unsigned number.
A floating-point number must contain a decimal point or an ex ponent or both. The numbers 1.114 and 17.3e-5 are examples of floating point numbers. All floating point numbers are of the type double unless a suffix is appended to the number. Any number suffixed with an f or an F is a single precision floating-point num ber, and a suffix of l or L on a floating-point number will generate a type long double. Octal (base 8) and hexadecimal (base 16) numbers can be created. Any number that is prefixed with a 0—a leading zero—is taken to be an octal number. Hexadecimal numbers are prefixed with a 0x or a 0X. The rules above for L and U also apply to octal and hexadecimal numbers.
The final type qualifier is volatile. The qualifier volatile instructs the compiler to NOT optimize any code involving the vari able. In execution of an expression, a side effect refers to the fact that the expression alters something. The side effect of the following state ment
a=b+c;
is that the stored value of a is changed. A sequence point is a point in the code where all side effects of previous evaluations are completed and no side effects from subsequent evaluations will have taken place. An important consideration of the optimization is that if an expression has no side effects, it can be eliminated by the compiler. Therefore, if a statement involves no sequence point, or alters no memory, it is sub ject to being discarded by the compiler. This operation is not particularly bad when writing normal code, but when working with microcontrollers where events can occur as a result of hardware operations, not the program, this optimization can utterly destroy a program. For example, whenever the hardware can alter a stored value, the compiler should