2.3 Compiler MikroBasic PRO for PIC
Everything you have read so far about programming in Basic is just a theory. It is useful to know, of course, but don’t forget that this programming language is not much in connection with something concrete and tangible. You will experience many problems with accurate names of registers, their addresses, names of particular control bits and many others when you start writing your first program in Basic. The point is that you need more than theory to make the microcontroller do something useful.
Having in mind a saying ‘Prevention is better than cure’, we have to remind you of all the things you must have settled before you start writing a program for the microcontroller. First of all, you need a program installed on your PC which understands the programming language you are going to use and which provides appropriate working environment for it. There is no such a compiler sutable for only one type of microcontrollers nor for all microcontrollers. It’s about software used for programming similar microcontrollers of one manufacturer. We have previously introduced mikroBasic language which has been especially designed for programming PIC microcontrollers. Now, when you know enough about it, it’s time to present the software you are going to use for developing and editing your projects. This software is called the mikroBasic PRO for PIC compiler. It’s IDE (Integrated Development Environment) includes all the tools you need to develop your projects (editor, compiler debugger etc.)
As its name implies, the mikroBasic PRO for PIC compiler is intended for writing programs for PIC microcontrollers in the Basic language. It contains information about architecture of PIC microcontrollers (registers, their accurate addresses, memory modules, operation of its modules, instruction set, pinout etc.). Moreover, it includes specific tools for PIC microcontroller programming. So, the first thing you have to do when you start up the compiler is to select the chip and operating frequency from the list. This is not the end. This is the beginnig. You can finally start writing your program in Basic.
The process of creating and executing a project can be divided in several parts:
- Creation of the project (project name, project settings, file dependancies);
- Editing of the program;
- Compilation of the program and correction of errors;
- Debugging (execute your program step by step to make sure that it performs the operations you expect);
- Microcontroller programming (load the .hex file genertaed by the compiler into the microcontroller by using thePICflash programmer).
INSTALLING MIKROBASIC PRO FOR PIC
The installation of mikroBasic PRO for PIC is similar to the installation of any other Windows program and the whole procedure is carried out through Wizard:
You just have to follow the instructions and click on Next, OK, Next, Next... The same old story except for the last option 'Do you want to install PICFLASH v7.11 programmer?'. Does it make you question what to do? As you know, the compiler translates a program written in Basic into a hex code. The next step is to load that code into the microcontroller. That’s why you need the PICFLASH programmer. Install it!
After completing the PICFLASH installation, you will be asked for the installation of another similar program. It is a software for programming a special family of PIC microcontrollers which operate in low consumption mode (3.3 V). Skip it...
The last step - driver installation!
A driver is a program which enables the operating system to communicate with a peripheral device. In our case, this device is the hardware programmer of your development system. You definitely need it. Click Yes.
A type of driver to be installed depends on the operating system in use. In this case, the PC runs a 32-bit operating system Windows XP. Select the Win 2000, XP, 2003 32-bit folder containing the appropriate driver and start up its installation.
Now you are safe, just keep on clicking Next, OK, Next, Next...
IDE OF MIKROBASIC PRO FOR PIC
This is what appears on your screen when you start up mikroBasic PRO for PIC for the very first time:
Unfortunately, a detailed description of all the options available in this IDE would take up too much time, so we are not going into it. We are going to describe only the most important features of the mikroBasic PRO for PIC compiler, instead. Of course, you can always get more information by pressing the Help button [F1]. A detailed explanation on how to create a new project and write a program is given in several practical examples in Chapter 4 of this book.
PROJECT MANAGER
A program written in the mikroBasic PRO for PIC compiler is not a separate document, but a part of a project that also includes hex code, assembly code, and other files. Some of them are created during the compiler’s operation, while some are imported from other programs. However, the Project Manager window enables you to handle all of the project files. Just right click on any folder and select the option you need for your project.
LIBRARY MANAGER
Libraries contain a large number of ready-to-use functions and provide a lot of facilities when writing programs for PIC MCUs. The compiler must know all dependencies of your mikroBasic source file in order to compile it properly. You should open the Library Manager window and check libraries you want to use in your program. When a library is checked, it is automatically added to the project and linked during the compiling process. Thus, you don’t have to include them manually into your source code files using the #include directive. For example, if your program uses an LCD then there is no need to write new functions because by selecting the Lcd Library you will be able to use ready-to-use LCD functions in your program. If this library is not selected in the Library Manager, every time you try to use any of its functions, the compiler will report an error. A description of each library is available by right clicking on its name and selecting the Help option.
CODE EXPLORER
The Code Explorer window enables you to easily locate objects (functions, constants, procedures etc.) within long programs. For example, if you look for a function used in the program, just double click its name in this window, and the cursor will be automatically positioned at the appropriate line in the program.
PROJECT SETTINGS
When compiling a project, the compiler generates files to be loaded into the microcontroller. These files will be different depending on the type of the microcontroller and the purpose of the compilation. Thus, in order to enable the compiler to operate properly, it is necessary to set some project parameters in the Project Settings window:
Device: Selection of the microcontroller to be used enables the compiler to retrieve the associated definition file. The definition file of a microcontroller contain specific information on its SFR registers, their memory addresses and some variables specific to that microcontroller. It is all mandatory for creating a compatible .hex file.
Oscillator: This option is used to specify the operating speed of the microcontroller. Of course this value depends on the oscillator in use. It is retrieved by the compiler for compiling routines which require time information (function Delay_ms( ) for example). Later, this information will also be used by the programmer. The operating speed is set so as to enable the microcontroller’s internal oscillator to operate at selected frequency.
Build/Debugger Type: The whole compiling process is called building and includes parsing, compiling, linking and generation of .hex files. The build type enables you to set up the building mode. Depending on your choice of mode, the generated file to be loaded into the microcontroller will be different.
Build type - release: The program to be loaded into the microcontroller will not contain any complementary information to be debugged later. After completing the compiling process, the compiler has no more influence on the program execution.
Build type - ICD debug: Some information is added to the .hex file in order to allow you to perform hardware debugging. When the compiling process is completed and the microcontroller is programmed, the compiler remains connected to the microcontroller and still can affect its operation. A tool called mikroICD (In Circuit Debugger) enables the program to be executed step by step and provides an access to the current content of all registers of the microcontroller.
For the purpose of debugging, a software simulator can be used in both build type modes. It enables you to simulate your program by reproducing the microcontroller behaviour. The simulator doesn’t use real devices for its operation, so that some operations cannot be reproduced (interrupt, for example). However, it is faster to debug a program by using simulator and it doesn’t require any taget device.
Note that any of these settings can be modified at any time while editing the program. Don’t forget to re-build and re-program your device after modifying any of those settings.
EDITING AND COMPILING PROGRAMS
CODE EDITOR
Code Editor is a central part of the compiler window used for writing and editing programs. A large number of options used for setting its functions and layout can be found in the Tools/Options menu [F12]. When you write a program, don’t forget comments. Comments are very important for the program debugging and upgrading. Moreover, even if the compiler doesn’t have formatting restriction, you should always follow the same editing rules (like in the examples provided in this book). As there is no line length limitation, don’t hesitate to use blank spaces in order to make your code more readable.
When you write a program, compile it regularly in order to correct syntax errors as you come along. You can also compile your program every time the redaction of a new function is completed and test its behaviour by using debug mode. In this way, it is much easier to fix compilation errors. Otherwise, you will be compelled to modify the entire program.
COMPILING AND FIXING ERRORS
To compile your code, you just have to click on the Build option in the Project menu. In fact, the entire project is compiled and, in case the compilation is sucessful, the output files are generated (asm, .hex etc.). A compilation is successful if no error was found. The compilation generates many messages which are displayed in the Messages window. These messages consist of information, warnings and errors. Each error found is associated with its program line and description. As an error in your code can generate many other errors, you should always try to fix the first error on the list and then recompile your program. In other words, it is advisable to fix errors one by one.
In the previous example, the program reports a syntax error in the 80th line. The compilation enables you to correct your program by fixing all mikroBasic errors. When all the errors are fixed, your program is ready to be dumped into the microcontroller. However, your job is not finished yet because you still don’t know whether your program behaves as expected or not.
DEBUGGING YOUR PROGRAM
As we could see before, there are two debug modes: a software debugger which simulates actions of the microcontroller (default mode) and a hardware debugger (mikroICD) which directly reads the content of the microcontroller memory. Whatever mode you choose, the debugging procedure is the same. In case you choose ICD debug make sure you load your program into the microcontroller. Debugging is a very important step as it allows you to test your program after a sucessful compilation, or to fix bugs discovered in a running program.
To start the debugger, click on the Start debugger option from the Run menu. The code editor will automatically be slightly modified and a window called Watch Values will appear. The principle of debugging is based on running a program step by step and following the evolution of the content of registers and variable values. In this way, you will be able to verify the computation and to see if something unexpected has occured. By running the program step by step, you will be able to easily locate where problems occur. As debugging can lead to program modification, you should always re-compile the program after each correction and restart the debugger in order to verify what you have changed.
SOFTWARE SIMULATOR
If you wish to be a programmer and devote yourself to programming microcontrollers, then you have to get used to the fact that programs almost NEVER work on the first try or start using a simulator. The simulator is an integral part of the compiler used to simulate the operation of the microcontroller.
Prior to starting up the simulator, select its operating mode in the Project Settings window (Build type - release), compile the program and click the Run /Start Debugger option.
The compiler will be automatically set to simulation mode. As such, it monitors the state of all register bits and enables you to execute the program step by step while monitoring the operation of the microcontroller on the screen.
A few icons, used only for the operation of this simulator, will appear in the toolbar when the compiler is set to this mode.
These icons have the following meanings:
- Step Into: Execute the current program line, then halts. If the executed program line calls a subroutine, the debugger steps into the subroutine and halts after executing the first instruction within it.
- Step Over: Execute the current program line, then halts. If the executed program line calls a subroutine, the debugger will not step into it. The whole subroutine will be executed and the debugger halts at the first instruction following the call. It seems that one program line is skipped even though the whole subroutine is executed. As a result, the state of registers is changed. This command is commonly used when it is necessary to speed up the execution of long program loops.
- Run To Cursor: Execute all the program lines until the cursor position is reached.
- Step out: Execute all remaining program lines within the subroutine. The debugger halts immediately after exiting the subroutine.
Breakpoints make the process of debugging programs more efficient by enabling you to stop the program execution at some specific line. This is very useful as you will be able to check only critical parts of the program, not lose your time by testing the whole program line by line. To add or remove a breakpoint you just have to click on the left side of your code editor on the appropriate line or press [F5]. A small window called Breakpoints indicates you where the breakpoints are located. Note that the lines marked by breakpoints are highlighted in red.
The line being currently executed is highlighted in blue. You can read the content of registers and variables you have selected in Watch Values at any time. If you want to jump directly to breakpoints, use the Run/Pause Debugger command.
WATCH VALUES WINDOW
The software and harware debuggers have the same function to monitor the state of registers during program execution. The difference is that the software debugger simulates the execution of the program on a PC, while the ICD debugger uses the microcontroller. Any change of a pin logic state is reflected on the appropriate register (port). As the Watch Values window enables you to monitor the state of all registers, it is easy to check whether a pin is set or cleared. The latest modifications appears in red in this window. This enables you to easily locate modifications in the list file when debugging. Select View/Windows and click the Watch Values option to activate this window. You can make a list of registers/variables the state of which you want to monitor.
STOPWATCH
If you want to find out how long it takes for the microcontroller to execute a part of the program, select the Run/View Stopwatch option. A window, as shown in figure on the right, will appear. Do you know how the stopwatch works? Well, it’s as simple as that. The times (time when the program has been started, time of the last step execution etc.) are automatically measured for each action of the debugger (step into, step over, run/pause etc.) and displayed in the Stopwatch window.
IN-CIRCUIT DEBUGGER
Another way to check a program execution is by using mikroICD debugger (in-circuit debugger). The mikroICD is an integral part of the PICflash programmer. It is used for testing and debugging programs. The process of testing and debugging is performed by monitoring the state of all registers within the microcontroller which operates in real time. In order to enable debugging, it is necessary to select options Build Type-ICD Debug and Debugger-mikroICD before the program is loaded into the microcontroller. As soon as the mikroICD debugger starts up, a window, as shown in figure below, appears. The mikroICD debugger communicates with the PIC through its pins used for programming. Therefore, these pins cannot be used as I/O pins while the process of debugging is in progress. During the mikroICD debugger operation, programmer and PC have to be connected using a USB cable.
mikroICD debugger options:
- Start Debugger [F9]
- Run/Pause Debugger [F6]
- Stop Debugger [Ctrl+F2]
- Step Into [F7]
- Step Over [F8]
- Step Out [Ctrl+F8]
- Toggle Breakpoint [F5]
- Show/Hide Breakpoints [Shift+F4]
- Clear Breakpoints [Ctrl+Shift+F4]
Each of these commands is activated via keyboard shortcuts or by clicking appropriate icon within the Watch Values window.
The mikroICD debugger also offers options such as running a program step by step (single stepping), pausing the program execution to examine the state of currently active registers using breakpoints, tracking values of some variables etc. The following example illustrates a step-by-step program execution using the Step Over command.
Step 1:
In this example the 31st program line is highlighted in blue, which means that it will be executed next. The current state of all registers within the microontroller can be viewed in the mikroICD Watch Values window.
Step 2:
After the Step Over command [F8] is executed, the microcontroller will execute the 31st program line. The first next line (32nd) to be executed is highlighted in blue. The state of registers being changed during the execution of this instruction may be viewed now in the Watch Values window.
LOADING PROGRAM INTO THE MICROCONTROLLER
If you have fixed all errors in your code and you think the program is ready for use, then you should load it into the microcontroller. Use the PICflash programmer for this purpose. PICflash is a tool designed to program all types of PIC microcontrollers. It is composed of two parts:
- The hardware part buffers a hexadecimal file (the program to be loaded) and programs the microcontroller using specific voltage levels. During the process of programming, a new program is written into the microcontroller’s flash memory. The previous program will be automatically erased from the flash memory.
- The software part is in charge of sending program (.hex file) to the hardware part of the programmer over a USB cable. It is activated by clicking mE_Programmer from the Tools menu or by pressing [F11]. As a result, you can modify some programming settings and also control the operation of the programmer’s hardware (Load, Write, Verify...).
Note that you can re-program your microcontroller as many times as you wish.
COMPILER’S TOOLS
This compiler provides special tools which considerably simplify the process of program writing. All these tools are available from the Tools menu. Here is a brief description for all of them.
USART TERMINAL
The USART terminal is a replacement for the standard Windows Hyper Terminal. It can be used for checking the operation of the microcontroller which uses USART communication. Such microcontrollers are built into the target device and connected to the PC’s RS-232 connector over a serial cable. The USART terminal window contains options for setting serial communication and displaying sent/received data.
EEPROM EDITOR
By selecting the EEPROM Editor option from the Tools menu, a window, as shown in figure below, will appear. Here you can see how the EEPROM memory of the microcontroller looks like. If you want to change its contents after loading the program into the microcontroller this is the right way to do it. If a new content is a data of specific type (char, int or double), then you should select it, enter the value in the Edit Value field and click Edit. Then click the Save button to save it as a document with the .hex extension. If the Use EEPROM in Project option is active, the data will be automatically loaded into the microcontroller during the programming process.
ASCII CHART
If you need numerical representation of any ASCII character, just select the ASCII chart option from the Tools menu and the table, as shown in figure below, will appear.
You probably know that every key of your keyboard is associated with one code (ASCII). Numbers have strange equivalents. For this reason, the program instruction for displaying number 7 on an LCD will not display it, but the equivalent of the BEL instruction. If you send the same number as a character, you will get the expected result - number 7. Accordingly, if you want to display a number without previously converting it into appropriate character, then it is necessary to add number 48 to each digit the number to be displayed consists of.
SEVEN SEGMENT EDITOR
A seven segment editor enables you to easily find out which number is necessary to be set on an output port in order to display a desired symbol. Of course, it goes without saying that port pins must be properly connected to display segments. You just have to place the cursor on any display segment and click it. The number that you should enter into the program will be shown immediately. That’s all.
LCD CUSTOM CHARACTER
In addition to the standard characters, the LCD display can also display characters created by the programmer. By selecting the LCD custom character tool you will spare yourself from tedious work of writing functions for sending appropriate code to a display. Just create a symbol by clicking small squares in the LCD custom character window, select position and row and click the GENERATE button. The appropriate code appears in another window. No more clicks are needed. Copy to Clipboard - Paste...
GRAPHIC LCD BITMAP GENERATOR
This is another necessary tool in case the microcontroller you are writing program for uses graphic LCD display (GLCD). This tool enables you to display any bitmap easily. In order to take advantage of it, select Tools/Glcd Bitmap Editor and appropriate window appears. Select the type of display to be used and load a bitmap. The bitmap must be monochromatic and in specified resolution (128 x 64 pixels in this case). Further procedure is the same as in the example above. Copy to Clipboard...
Every code generated using tools for controlling LCD and GLCD displays contains functions of the Lcd library. If you use them in the program, don’t forget to check the box next to this library in the Library Manager window so as to enable the compiler to recognize these functions correctly.
LIBRARIES
If you need a function or a procedure to perform certain task while writing a program, you just have to find it in one of the libraries included into the compiler and use it. A library is actually a file called header. It contains a group of variables and functions written in mikroBasic. Each library has a specific purpose. For example, if you need a procedure to generate an audio signal on some pin, open the Sound library in the Library Manager window and double click the appropriate procedure Sound_Play. A detailed description of this procedure appears on the screen. Copy it to your program and set appropriate parameters. If this library is checked, its functions will be retrieved during the process of compiling and it will not be necessary to use the include directive.
The mikroBasic PRO for PIC includes miscellaneous and hardware specific libraries.
One of the most useful features of the mikroBasic PRO for PIC compiler is the Library Manager and surely deserves our attention.
MISCELLANEOUS LIBRARIES
Miscellaneous libraries contain some of the general-purpose functions:
LIBRARY |
DESCRIPTION |
Button Library |
Function eliminates the influence of contact button flickering |
Conversions Library |
Library routines for numerals to strings and BCD/decimal conversions |
C Type Library |
Library functions for testing and mapping characters |
String Library |
Library which automatizes string related tasks |
Time Library |
Time library routines usage for time calculations in UNIX time format |
Trigon Library |
Library functions for floating point math handling |
HARDWARE SPECIFIC LIBRARIES
Hardware specific libraries include functions used to control the operation of hardware modules:
LIBRARY |
DESCRIPTION |
ADC Library |
Used for A/D converter operation |
CAN Library |
Used for operation with CAN module |
CANSPI Library |
Used for operation with external CAN module (MCP2515 or MCP2510) |
Compact Flash Library |
Used for operation with Compact Flash memory cards |
EEPROM Library |
Used for operation with built-in EEPROM memory |
Ethernet PIC18FxxJ60 Library |
Used for operation with built-in Ethernet module |
Flash Memory Library |
Used for operation with built-in Flash memory |
Graphic Lcd Library |
Used for operation with graphic LCD module with 128x64 resolution |
I²C Library |
Used for operation with built-in serial communication module I2C |
Keypad Library |
Used for operation with keyboard (4x4 push buttons) |
Lcd Library |
Used for operation with LCD display (2x16 characters) |
Manchester Code Library |
Used for communication using Manchester code |
Multi Media Card library |
Used for operation with multimedia MMC flash cards |
OneWire Library |
Used for operation with circuits using One Wire serial communication |
Port Expander Library |
Used for operation with port expander MCP23S17 |
PS/2 Library |
Used for operation with standard keyboard PS/2 |
PWM Library |
Used for operation with built-in PWM module |
RS-485 Library |
Used for operation with modules using RS485 serial communication |
Software I²C Library |
Used for I2C software simulation |
Software SPI Library |
Used for SPI software simulation |
Software UART Library |
Used for UART software simulation |
Sound Library |
Used for audio signal generation |
SPI Library |
Used for operation with built-in SPI module |
SPI Ethernet Library |
Used for SPI communication with ETHERNET module (ENC28J60) |
SPI Graphic Lcd Library |
Used for 4-bit SPI communication with graphic LCD display |
SPI Lcd Library |
Used for 4-bit SPI communication with LCD display (2x16 characters) |
SPI Lcd8 Library |
Used for 8-bit SPI communication with LCD display |
SPI T6963C Graphic Lcd Library |
Used for SPI communication with graphic LCD display |
UART Library |
Used for operation with built-in UART module |
USB HID Library |
Used for operation with built-in USB module |