Chapter1: Introduction
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 * 1045 .. +3.4 * 1038).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 describedin Chapter 11: DSP Engine.
int *ptra;
1.2 Keywords
Key words are the concepts in a programming language having special meanings. The names of the variables must not use this set of words. In this chapter 3 key words whose understaning is very important will be treated. More details concerning key words could be found in the help-system accompanying the mikroC compiler for dsPIC.
1.2.1 Key word ASM
Key word asm denotes the beginning of an instruction block written in the assembler. The compiler supports the assembler instructions. If an exceptional speed of execution of a part of a code is required, and the user possesses the corresponding knowledge of the microcontroller architecture and assembler instructions, then the critical part of the programe could be written in the assembler (user-optimized parts of the code).
Note: The block of assembler instructions has to start by the key word asm { and finish by } !
Example – use of the key word asm:
asm {
MOVLW 10 // just a test
MOVLW _myvar
MOVLW 0 // just a test
MOVLW _myvar+1
}
When using the key word asm, care has to be taken of the available resources. For this reason the use of this key word is not recommended unless it is necessary. The use of the key word asm is necessary when using the DSP module which will be discussed in Chapter 11. In this case there is no alternative if the maximum use of the DSP module is targeted.
1.2.2 Key word ABSOLUTE
Key word absolute is used in cases when it is required that certain variable is saved at a memory cell which is determined in advance. In majority of cases the use of the key word absloute does not make sense. However, its use is sometimes necessary when using the DSP module decribed in Chapter 11: DSP Engine.Example – the use of the key word absolute:
int coef[10] absolute 0x0900; double series[16] absolute 0x1900;
In the above example array coef is located at address 0x0900 in the memory. It takes 10x2=20 bytes (10 elements, 2 bytes each), i.e. 20 addresses (10 locations), thus the range of addresses containing the elements of array coef is (0x0900 ... 0x0913). Array series is located at address 0x1900 in the memory. It takes 16x4=64 bytes (16 element, 4 bytes each), i.e. 64 addresses (32 locations) and the range of addresses containing the elements of array series is (0x1900 ... 0x193F).
1.2.3 Key word ORG
The key word org is used in situations when it is required that certain method (function) is saved at a preassigned address in the programe memory (the part of the memory where the programe is saved). The use of the key word org is necessary when processing the interrupts or traps. The methods (functions) of processing interrupts or traps have to be kept at a precisely specified address.
Example – the use of the key word org:
void ADC1_Int() org 0x2A{
int s;
IFS0.F11 = 0; //clear AD1IF
s = ADCBUF0; //fetch sample
}
The above example shows the method of instructing the compiler that the given function is saved in the part of the program memory starting from location 0x2A.
1.3 Architecture of the microcontroller
The microcontrollers of the family dsPIC30F are 16-bit modified Harvard architecture processors with an enhanced instruction set. Instructions are 24-bit wide. The working registers are 16-bit wide. There are 16 working registers; the 16th working register (W15) operates as a software stack pointer.
Fig. 1-1 Block diagram of the microcontrollers of the family dsPIC30F
1.4 Abreviations
| Abreviation | Full Description |
| AD | Analogue-to-Digital |
| DSP | Digital Signal Processing |
| UART | Universal Asynchronous Receiver Transmitter |
| I²C | Inter Integrated Circuit |
| PLL | Phase Locked Loop |
| PWM | Pulse Width Modulation |
| MCU | Microprocessor Unit |
| IVT | Interrupt Vector Table |
| AIVT | Alternate Interrupt Vector Table |
| SFR | Special Function Registers |
| SP | Stack Pointer |
| PC | Program Counter |
| IRQ | Interrupt Request |
| ACC | Accumulator |
| CAN | Control Area Network |
| DCI | Data Converter Interface |
| RTC | Real-Time Clock |
| GP | General Purpose |
| PSV | Program Space Visibility |
| RAM | Random Access Memory |
| ROM | Read Only Memory |
| AGU | Address Generator Unit |
| EA | Effective Address |
| ALU | Arithmetic Logic Unit |
| SPI | Serial Peripheral Interface |
| FIFO | First In – First Out |
| LIFO | Last In – First Out |
| PR | Period |
| SAR | Successive Approximation |
| SH | Sample and Hold |
| VREF | Voltage Reference |
| BUF | Buffer |
| MUX | Multiplexer |
| FFT | Fast Fourier Transform |
| INC | Increment |
| DEC | Decrement |
| MS | Master-Slave |
| SAT | Saturation |
| IPMI | Intelligent Management Interface |
| DTMF | Dual Tone Multiple Frequency |
| FIR | Finite Impulse Response |
| IIR | Infinite Impulse Response |
| FRC | Fast RC Oscillator (internal) |
| FSCM | Fail-Safe Clock Monitor |
| EXTR | External RESET |
| BOR | Brown-out Reset |
| LVD | Low-Voltage Detect |
Table 1-2 The abreviations used in the book
