1.1 The basic concepts of the programming language C
All examples presented in this book rely on the programming language C and its application in the mikroC compiler for dsPIC. It is thus necessary to introduce some of the concepts, modes of their applications, and their meanings in order to facilitate understaning of the abundant examples contained by the book. The simplest structures of C are used, so the examples can be understood with no need for an analysis of the programming language.
NOTE: In this chapter no complete C language used by the compiler has been presented, but only some of the methods of declaring types and some of the key words often used in the examples. The reader is referred to the help-system accompanying the mikroC compiler for a detailed study of the possibilities of the C language and its application in programming the microcontrollers of the family dsPIC30F.
The memory of a microcontroller of the family dsPIC30F keeps 16-bit (2-bytes) basic data. All other types of data are derived from these. The compiler also supports 1-byte and 4-bytes data. Both integer and floating point variables are supported. This chapter will present the most frequently used types of data.
Table 1-1 presents a review of integer variables supported by the dsPIC mikroC compiler.
TYPE |
SIZE IN BYTES |
RANGE |
(unsigned) char |
1 |
0 .. 255 |
signed char |
1 |
- 128 .. 127 |
(signed) short (int) |
1 |
- 128 .. 127 |
unsigned short (int) |
1 |
0 .. 255 |
(signed) int |
2 |
-32768 .. 32767 |
unsigned (int) |
2 |
0 .. 65535 |
(signed) long (int) |
4 |
-2147483648 .. 2147483647 |
unsigned long (int) |
4 |
0 .. 4294967295 |
Example – declaring integer variable:
int i;
long l;
Example – declaration of a floating point variable:
The floating point variables are supported by the type of data
float,
double and
long double, 32 bits wide in the memory and of the range (-1.5 * 10
45 .. +3.4 * 10
38).
float fnumber;
Example – declaration of arrays:
A set of variables of the same type, if indexed, is represented by an array.
int vector_one[10]; /* declares an array of 10 integers */
int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
As can be noticed from the above examples, the indexes in an array can start from any integer value. If within the square brackets only one value is declared, the array has so many members (10 in the above example). In that case the index of the first member iz zero!
Example – declaration of type string:
A series of characters is represented by a type of data called
string. It, in essense, represents an array of the type
char, but sometimes it is useful to be considered as text.
char txt[6] = "mikro";
Example – declaration of pointer:
More difficult to understand, but often used type of data is pointer. It serves for keeping the address in the memory where some essential information is kept. The application of this type of data is of exceptional significance for the module described
in
Chapter 11: DSP Engine.
int *ptra;