Login | Cart (0)

Mini Cart Cart Loading

Programming dsPIC MCU in BASIC

Chapter1: Introduction

1.1 The basic concepts of the programming language Basic

All examples presented in this book rely on the programming language Basic and its application in the mikroBasic 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 Basic are used, so the examples can be understood with no need for an analysis of the programming language.

NOTE: In this chapter no complete Basic 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 mikroBasic compiler for a detailed study of the possibilities of the Basic 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 mikroBasic compiler.

Type Size Range of values
byte, char 8–bit 0 .. 255
short 8–bit -127 .. 128
word 16–bit 0 .. 65535
integer 16–bit -32768 .. 32767
longword, dword 32–bit 0 .. 4294967295
longint 32–bit -2147483648 .. 2147483647

Example – declaring integer variable:

dim i, j, k as byte
dim counter, temp as word 

Example – declaration of a floating point variable:

The floating point variables are supported by the type of data real, 32 bits wide in the memory and of the range (±1.17549435082 * 10-38 .. ±6.80564774407 * 1038).

dim real_number as float

Example – declaration of arrays:

A set of variables of the same type, if indexed, is represented by an array.

dim weekdays as byte[7]
dim samples as word[50]

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 (5 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.

dim text as string[20]

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.

dim p as ^word

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 mikroBasic 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 end!

Example – use of the key word asm:

  MOV  W1,  W0
  ADD  W0,  W1,  W0
end asm

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 decriberd in Chapter 11: DSP Engine.

Example – the use of the key word absolute:

dim x as word absolute 0x32
' Variable x will occupy 1 word (16 bits) at address 0x32

dim y as longint absolute 0x34
' Variable y will occupy 2 words at addresses 0x34 and 0x36

In the above example array coef is located at address $0900 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 ($0900 ... $0913). Array series is located at address $1900 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 ($1900 ... $193F).

1.2.3 Key word ORG

The key word org is used in situations when it is required that certain method (function or procedure) 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 or procedures) of processing interrupts or traps have to be kept at a precisely specified address.

Example – the use of the key word org:

sub procedure proc(dim par as word) org 0x200
' Procedure will start at the address 0x200;
end sub

The above example shows the method of instructing the compiler that the given procedure is saved in the part of the program memory starting from location $2A.

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.

Block diagram of the microcontrollers of the family dsPIC30F

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
BOR Brown-out Reset
LVD Low-Voltage Detect

Table 1-2 The abreviations used in the book

table of contents | next chapter

Easter 2014
LiveZilla Live Chat Software