MicroEngineering Labs, Inc. provides two BASIC compilers that make writing the code for the PIC family of microcontrollers offered by Microchip Technology tremendously easy. In this book, we will discuss the more powerful of the two: the PICBASIC PRO Compiler. A listing of the commands provided by each compiler is provided in the following to let you compare the two compilers and select the one best suited to both your budget and your needs.
The following is the smaller compiler of the two:
All math operations are unsigned and performed with 16-bit precision:
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
** |
MSB of multiplication |
/ |
Division |
// |
Remainder |
MIN |
Minimum |
MAX |
Maximum |
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise XOR |
&/ |
Bitwise AND NOT |
|/ |
Bitwise OR NOT |
^/ |
Bitwise XOR NOT |
This is the larger compiler of the two:
MATH FUNCTIONS/OPERATORS
The math operations are unsigned and performed with 16-bit precision:
As can be seen from the preceding comparison, the PICBASIC PRO Compiler provides a much more comprehensive instruction set and therefore is the compiler of choice for serious development work.
It is, of course, also possible to program microcontrollers in assembly language and “C,” but this book does not cover these languages. A number of good books are available on the subject, and some that I looked over are listed in a file on the support Web site with my comments. Some educators feel that a Junior College level class on the subject is the best way to learn how to do this and there is some merit to this but for our purposes the PICBASIC PRO Compiler will do everything we need and is much easier to use.
In addition to the compiler, you need an editor to allow you to write and edit programs with ease. A very adequate editor is provided as a part of the compiler package. Its called the MicroCode Studio editor. This comprehensive and powerful editor is also available at no charge (on the Internet) from MicroCode Studios. This is a complete editor with no limit on the number of lines of code you can write. It is fully integrated with the software and hardware provided by microEngineering Labs and is the editor of choice for most users. (The free version is limited to compiling programs for just a few microcontrollers, but these include both the 16F877A and the 16F84A.)
Three of the editors available are:
MicroCode Studio Mecanique’s MicroCode Studio is a powerful, visual, integrated development environment (IDE) with an In-Circuit Debugging (ICD) capability designed specifically for microEngineering Labs’ PICBASIC PRO Compiler. This software can be downloaded from the Internet at no charge. The only limitation on the software is that it allows you to run only one IDE at one time. This is not a real handicap at our level of interest, however, since it is the editor that best suits our needs, and because all programs in this book were written with this editor.
Proton+ Lite BASIC editor (provided by Crownhill) This is a test version of their editor and is limited to 50 lines of code and three processors (including the PIC 16F877A). If you like this editor, you can use this as your main editor and then cut and paste to the MicroCode Studio to compile and run your programs, and thus go around the 50-line limit. The native language of this editor is not the same as PICBASIC, so there are other handicaps to contend with. (These incompatibilities are best avoided.)
Microchip MPLAB This is the software that the maker of the microcontrollers, Microchip Technology Inc., provides for editing programs written for their PIC series of microcontrollers. It is an assembly level programmer. We will not be doing any assembly language programming, but the editor can be useful.
The PICBASIC PRO Compiler—hereafter referred to as the PBP—provides all the functions needed to program almost the entire family of PIC microcontrollers in a BASIC-like environment. This means it allows you to write programs that read the inputs and write to the outputs in a simple and easy-to-learn fashion. It means that communications are simplified and that the time it takes to get an application running is reduced manyfold. It also means that the programs are easier to follow and debug (though debugging can get quite complicated even on these seemingly simple devices). The compiler supports only integer math, but that is not a big handicap when we are working with these limited microprocessors.
It also means that the programs that are developed are shorter than assembly language programs, but they are slower in their execution than assembly language programs. (Complications also exist that have to do with the use of interrupts, and which must be addressed, but these are beyond the scope of this book.)
All the exercises and examples provided in the text are based on the PBP compiler. We will not go over the detailed instructions for using each of the PBP instructions in the text. It is expected and will be assumed that you will have purchased the software and thus will have the manual for the compiler in hand. However, some commands can be complicated to implement and so we will spend time on them as necessary.
The compiler is kept current by microEngineering Labs. for the latest MCUs released by Microchip Technology. The LAB-X1 uses the 16F877A MCU, and it is the MCU of choice, though other MCUs that have a general pin-for-pin compatibility with this MCU may also be used. All the experiments and exercises in this book will use the PIC 16F877A only. The compiler addresses almost all the capabilities of this MCU, and we will cover the use of all the devices provided on the LAB-X1 board to develop the comfort level you need to incorporate them into your instruments and controllers.
Detailed instructions for installing the software on your PC are provided in the compiler manual. It is not necessary to install the software from a DOS prompt. It is much easier to install it under Windows with the “Install.exe” or equivalent file provided in each package.
The software can be set up so that “one mouse click” will transfer the program from the editor to the PIC microcontroller and run the program in the PIC. In order to do this, you have to add a couple of functional codes to the programmer operating system. These codes tell the programmer to load the program and execute it. Installing the software is covered in detail in the chapter on getting started.
A SIMPLE EXAMPLE PROGRAM USING PICBASIC
A program that makes the LEDs blink ON and OFF is usually the first program written by beginners. The purpose of the program is not to blink the LEDs but rather to allow you to go through the programming procedures in a simple and straightforward way, and get a result that is easy to verify. Once you have the LEDs blinking, you will know you have followed all the steps necessary to write and execute a program. Larger, more complicated programs may be much more difficult to write and debug but they are no more difficult to compile, load, and run.
Program 4.1 outlines the keystrokes for writing and running the “blink” the LEDs program in PICBASIC.
Program 4.1 The first program (Blinking all eight LEDs on PORTD one at a time)
; ****************************************************************
; * Name myBlink8leds.BAS
; * Author Harprit Singh Sandhu
; * Notice Copyright © 2008
; * All Rights Reserved
; * Date 1/Feb/2008
; * Version 1.0
; * Notes Blinks all 8 LEDs on bargraph one at a time
; ****************************************************************
CLEAR ; clear RAM memory
DEFINE OSC 4 ; define the osc freq
LED_ID VAR BYTE ; call out the two variables LED_ID and I
I VAR BYTE ; as 8 bit bytes
TRISD =%00000000 ; set PORTD to all outputs ;
MAINLOOP: ; loop is executed forever
I=1 ; initialize the counter to 1
FOR LED_ID = 1 TO 8 ; do it for the 8 LEDs
PORTD=I ; puts number in PORTD
PAUSE 100 ; pause so you can see the display
I=I * 2 ; multiplying by 2 moves lit LED left 1 pos
NEXT LED_ID ; go up and increment counter
GOTO MAINLOOP ; do it all forever
END ; always end with END statement
PICBASIC PRO TIPS AND CAUTIONS
1. To get context-sensitive help, move the cursor over a PICBASIC command, click to set the cursor, and press F1.
2. Programs assume the PIC is running at 4 MHz. To change the default setting (for example, to 20 MHz), simply add DEFINE OSC 20 at the top of your program and set the oscillator jumpers on the LAB-X1 accordingly. It is good practice to always specify the oscillator speed in a program. Beginners should start with 4 MHz designs. The LAB-X1 is set up to run at 4 MHz as received from the factory. See the manual. The defined OSC speed has to match the hardware crystal for the hardware and software to work correctly. This is especially important for time-sensitive instructions.
3. Before you can use the LCDisplay on the LAB-X1, ADCON1 must be set (to %00000111 to make PORTE [and PORTA] digital) and you must pause about 500 msec to allow the LCD to start up before issuing it its first command. You may not need a pause, or a shorter pause may be specified, if there are many time-consuming instructions before the first LCDOUT instruction is executed. (Other values of ADCON1 can also be used, depending on how you want the A and E ports configured. See the discussion in Chapter 18.)
4. I have used binary notation (%01010101) throughout this book to set relevant bytes and registers so you can readily see which bit is being set to what. The compiler accepts hexadecimal and decimal notation just as willingly. Binary notation does not permit a space after the % sign, and all eight bits must be specified.
5. When using Word for programming support, if a single quotation mark (’) is copied from a Word file and pasted into the MicroCode Studio editor, it will be interpreted as a (’) and will therefore not properly start the comment section of the line. All these have to be changed in the editor after pasting. Pasting from the editors into Word does not exhibit the same effect. If you use (;) for the comments, this problem does not occur.
6. All the named registers can be called by name when using the compiler. The register names are the same as those used (defined) by the manufacturer in the datasheet and are the same across the entire family of PIC microcontrollers if they provide the same function. Uppercase or lowercase names can be used. The DEFINEs must be stated in uppercase only, and the spellings in the DEFINE lines are not always checked by the compiler! Therefore, be very careful with the spellings when adding DEFINEs to your program.
7. Circuits and segments of circuits are provided throughout this book to show you how to connect up the hardware when you design your own circuits. If you have access to AutoCAD, you can cut and paste the diagrams in the files on the support Web site into your own designs. All the diagrams in this book are on the Web site.
A FREE DEMO BASIC COMPILER
A free version of the PICBASIC PRO Compiler by microEngineering Labs can also be downloaded from the microEngineering Labs Web site. This is a fully functional compiler with the limitation that programs are limited to 30 lines of code. This is enough to allow you to test the compiler and any instruction that you might have a special interest in. This version can give you a good idea of the power and ease of use of the language. I encourage you to try out this compiler before you make your compiler purchase.