4
THE SOFTWARE, THE COMPILERS, AND THE EDITOR

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 Basic Compiler Instruction Set

The following is the smaller compiler of the two:

ASM..ENDASM

Insert assembly language code section.

BRANCH

Computed GOTO (equivalent to ON..GOTO).

BUTTON

De-bounce and auto-repeat input on specified pin.

CALL

Call assembly language subroutine.

EEPROM

Define initial contents of on-chip EEPROM.

END

Stop execution and enter low power mode.

FOR..NEXT

Repeatedly execute statement(s).

GOSUB

Call BASIC subroutine at specified label.

GOTO

Continue execution at specified label.

HIGH

Make pin output high.

I2CIN

Read bytes from I2C device.

I2COUT

Send bytes to I2C device.

IF..THEN - GOTO

If specified condition is true.

INPUT

Make pin an input.

[LET]

Assign result of an expression to a variable.

LOOKDOWN

Search table for value.

LOOKUP

Fetch value from table.

LOW

Make pin output low.

NAP

Power down processor for short period of time.

OUTPUT

Make pin an output.

PAUSE

Delay (1 msec resolution).

PEEK

Read byte from register.

POKE

Write byte to register.

POT

Read potentiometer on specified pin.

PULSIN

Measure pulse width (10 μsec resolution).

PULSOUT

Generate pulse (10 μsec resolution).

PWM

Output pulse width modulated pulse train to pin.

RANDOM

Generate pseudorandom number.

READ

Read byte from on-chip EEPROM.

RETURN

Continue execution at statement following last executed GOSUB call.

REVERSE

Make output pin an input or an input pin an output.

SERIN

Asynchronous serial input (8N1).

SEROUT

Asynchronous serial output (8N1).

SLEEP

Power down processor for a period of time (1 sec resolution).

SOUND

Generate tone or white noise on specified pin.

TOGGLE

Make pin output and toggle state.

WRITE

Write byte to on-chip EEPROM.

MATH OPERATIONS

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

The PICBASIC PRO Compiler Instruction Set

This is the larger compiler of the two:

@

Insert one line of assembly language code.

ADCIN

Read on-chip analog to digital converter.

ASM..ENDASM

Insert assembly language code section.

BRANCH

Computed GOTO (equivalent to ON..GOTO).

BRANCHL

Branch out of page (long BRANCH).

BUTTON

De-bounce and auto-repeat input on specified pin.

CALL

Call assembly language subroutine.

CLEAR

Zero all variables.

CLEARWDT

Clear (tickle) watchdog timer.

COUNT

Count number of pulses on a pin.

DATA

Define initial contents of on-chip EEPROM.

DEBUG

Asynchronous serial output to fixed pin and baud.

DEBUGIN

Asynchronous serial input from fixed pin and baud.

DISABLE

Disable ON DEBUG and ON INTERRUPT processing.

DISABLE DEBUG

Disable ON DEBUG processing.

DISABLE INTERRUPT

Disable ON INTERRUPT processing.

DTMFOUT

Produce touch-tones on a pin.

EEPROM

Define initial contents of on-chip EEPROM.

ENABLE

Enable ON DEBUG and ON INTERRUPT processing.

ENABLE DEBUG

Enable ON DEBUG processing.

ENABLE INTERRUPT

Enable ON INTERRUPT processing.

END

Stop execution and enter low power mode.

ERASECODE

Erase block of code memory.

FOR ..NEXT

Repeatedly execute statements.

FREQOUT

Produce up to two frequencies on a pin.

GOSUB

Call BASIC subroutine at specified label.

GOTO

Continue execution at specified label.

HIGH

Make pin output high.

HPWM

Output hardware pulse width modulated pulse train.

HSERIN

Hardware asynchronous serial input.

HSERIN2

Hardware asynchronous serial input, second port.

HSEROUT

Hardware asynchronous serial output.

HSEROUT2

Hardware asynchronous serial output, second port.

I2CREAD

Read from I2C device.

I2CWRITE

Write to I2C device.

IF..THEN..ELSE..ENDIF

Conditionally execute statements.

INPUT

Make pin an input.

LCDIN

Read from LCD RAM.

LCDOUT

Display characters on LCD.

{LET}

Assign result of an expression to a variable.

LOOKDOWN

Search constant table for value.

LOOKDOWN2

Search constant/variable table for value.

LOOKUP

Fetch constant value from table.

LOOKUP2

Fetch constant/variable value from table.

LOW

Make pin output low.

NAP

Power down processor for short period of time.

ON DEBUG

Execute BASIC debug monitor.

ON INTERRUPT

Execute BASIC subroutine on an interrupt.

OWIN

One-wire input.

OWOUT

One-wire output.

OUTPUT

Make pin an output.

PAUSE

Delay (1 msec resolution).

PAUSEUS

Delay (1 μsec resolution).

PEEK

Read byte from register.

PEEKCODE

Read byte from code space

POKE

Write byte to register.

POKECODE

Write to code space at device programming time.

POT

Read potentiometer on specified pin.

PULSIN

Measure pulse width on a pin.

PULSOUT

Generate pulse to a pin.

PWM

Output pulse width modulated pulse train to pin.

RANDOM

Generate pseudorandom number.

RCTIME

Measure pulse width on a pin.

READ

Read byte from on-chip EEPROM.

READCODE

Read word from code memory.

REPEAT..UNTIL

Execute statements until condition is true.

RESUME

Continue execution after interrupt handling.

RETURN

Continue at statement following last GOSUB call.

REVERSE

Make output pin an input, or an input pin an output.

SELECT CASE

Compare a variable with different values.

SERIN

Asynchronous serial input (BS1 style).

SERIN2

Asynchronous serial input (BS2 style).

SEROUT

Asynchronous serial output (BS1 style).

SEROUT2

Asynchronous serial output (BS2 style).

SHIFTIN

Synchronous serial input.

SHIFTOUT

Synchronous serial output.

SLEEP

Power down processor for a period of time.

SOUND

Generate tone or white noise on specified pin.

STOP

Stop program execution.

SWAP

Exchange the values of two variables.

TOGGLE

Make pin output and toggle state.

USBIN

USB input.

USBINIT

Initialize USB.

USBOUT

USB output.

WHILE..WEND

Execute statements while condition is true.

WRITE

Write byte to on-chip EEPROM.

WRITECODE

Write word to code memory.

XIN

X-10 input.

XOUT

X-10 output.

MATH FUNCTIONS/OPERATORS

The math operations are unsigned and performed with 16-bit precision:

+

Addition

Subtraction

*

Multiplication

**

Top 16 bits of multiplication

*/

Middle 16 bits of multiplication

/

Division

//

Remainder (modulus)

<<

Shift left

>>

Shift right

ABS

Absolute value

COS

Cosine

DCD

2n decode

DIG

Digit

DIV32

31-bit × 15-bit divide

MAX

Maximum

MIN

Minimum

NCD

Encode

REV

Reverse bits

SIN

Sine

SQR

Square root

&

Bitwise AND

|

Bitwise OR

^

Bitwise exclusive OR

˜

Bitwise NOT

&/

Bitwise NOT AND

|/

Bitwise NOT OR

^/

Bitwise NOTexclusive OR

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:

sqr 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.

sqr 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.)

sqr 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.

PICBASIC PRO Compiler

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.