10. PICBASIC PRO™ / PicBasic / Stamp Differences

Compatibility is a two-edged sword. And then there is the pointy end. PICBASIC PRO™ has made some concessions to usability and code size. Therefore we call it ABASIC Stamp like@ rather than BASIC Stamp compatible. PBP has most of the BASIC Stamp I and II instruction set and syntax. However there are some significant differences.

The following sections discuss the implementation details of PBP programs that might present problems. It is hoped that if you do encounter problems, these discussions will help illuminate the differences and possible solutions.

10.1. Execution Speed

The largest potential problem is speed. Without the overhead of reading instructions from the EEPROM, many PBP instructions (such as GOTO and GOSUB) execute hundreds of times faster than their BASIC Stamp equivalents. While in many cases this is a benefit, programs whose timing has been developed empirically may experience problems.

The solution is simple - good programs don't rely on statement timing such as FOR..NEXT loops. Whenever possible, a program should use handshaking and other non-temporal synchronization methods. If delays are needed, statements specifically generating delays (PAUSE, PAUSEUS, NAP or SLEEP) should be used.

10.2. Digital I/O

Unlike the BASIC Stamp, PBP programs operate directly on the PORT and TRIS registers. While this has speed and RAM/ROM size advantages, there is one potential drawback.

Some of the I/O commands (e.g. TOGGLE and PULSOUT) perform read-modify-write operations directly on the PORT register. If two such operations are performed too close together and the output is driving an inductive or capacitive load, it is possible the operation will fail.

Suppose, for example, that a speaker is driven though a 10uF cap (just as with the SOUND command). Also suppose the pin is initially low and the programmer is attempting to generate a pulse using TOGGLE statements. The first command reads the pin's low level and outputs its complement. The output driver (which is now high) begins to charge the cap. If the second operation is performed too quickly, it still reads the pin's level as low, even though the output driver is high. As such, the second operation will also drive the pin high.

In practice, this is not much of a problem. And those commands designed for these types of interfacing (SOUND and POT, for example) have built-in protection. This problem is not specific to PBP programs. This is a common problem for PICmicro (and other microcontroller) programs and is one of the realities of programming hardware directly.

10.3. Low Power Instructions

When the Watchdog Timer time-out wakes a PICmicro from sleep mode, execution resumes without disturbing the state of the I/O pins. For unknown reasons, when the BASIC Stamp resumes execution after a low power instruction (NAP or SLEEP), the I/O pins are disturbed for approximately 18 mSec. PBP programs make use of the PIC's I/O coherency. The NAP and SLEEP instructions do not disturb the I/O pins.

10.4. Missing PC Interface

Since PBP generated programs run directly on a PICmicro, there is no need for the Stamp's PC interface pins (PCO and PCI). The lack of a PC interface does introduce some differences.

Without the Stamp=s IDE running on a PC, there is no place to send debugging information. Debugging can still be accomplished by using one of the serial output instructions like DEBUG or SEROUT in conjunction with a terminal program running on the PC such as Hyperterm.

Without the PC to wake the PICmicro from an END or STOP statement, it remains idle until /MCLR is lowered, an interrupt occurs or power is cycled.

10.5. No Automatic Variables

The PICBASIC PRO™ Compiler does not automatically create any variables like B0 or W0. They must be defined using VAR. Two files are provided: BS1DEFS.BAS and BS2DEFS.BAS that will define the standard BS1 or BS2 variables. However, it is recommended that you assign your own variables with meaningful names rather than using either of these files.

10.6. No Nibble Variable Types

The BS2's nibble variable type is not implemented in the PICBASIC PRO™ Compiler. As PBP allows many more variables than a BS2, simply change nibble variable types to bytes.

10.7. No DIRS 

The BASIC Stamp variable names Dirs, Dirh, Dirl and Dir0-Dir15 are not defined and should not be used with the PICBASIC PRO™ Compiler. TRIS should be used instead, but has the opposite state of Dirs.

This does not work in PICBASIC PRO™: 

Dir0 = 1 ' Doesn't set pin PORTB. 0 to output 

Do this instead: 

TRISB. 0 = 0 ' Set pin PORTB. 0 to output 

or simply use a command that automatically sets the pin direction.

10.8. No Automatic Zeroing of Variables 

The BASIC Stamp sets all the variables and registers to 0 when a program starts. This is not automatically done when a PBP program starts. In general, the variables should be initialized in the program to an appropriate state. Alternatively, CLEAR can be used to zero all the variables and registers when a program starts.

10.9. Math Operators

Mathematical expressions now have precedence of operation. This means they are not evaluated in strict left to right order as they are in the BASIC Stamp and original PicBasic Compiler. This precedence means that multiplication and division are done before adds and subtracts, for example.

Parenthesis should be used to group operations into the order in which they are to be performed. In this manner, there will be no doubt about the order of the operations.

The following table list the operators in hierarchal order:

Highest Precedence

( )
























&& AND

^^ XOR

|| OR

Lowest Precedence

10.10. [ ] Versus ( )

PBP uses square brackets, [], in statements where parenthesis, (), were previously used. This is more in keeping with BASIC Stamp II syntax.

For example, the BS1 and original PicBasic Compiler SEROUT instruction looks something like:

Serout 0,T2400,(B0)

The PICBASIC PRO™ Compiler SEROUT instruction looks like:

Serout 0,T2400,[B0]

Any instructions that previously used parenthesis in their syntax should be changed to include square brackets instead.

10.11. ABS 

ABS works slightly differently than on the Stamp in that it will take the absolute value of a byte as well as a word.


The BASIC Stamp allows EEPROM not used for program storage to store non-volatile data. Since PBP programs execute directly from the PICmicro's ROM space, EEPROM storage must be implemented in some other manner.

The PIC16F84 (the default target for PBP programs), PIC16F83 and PIC16C84 have 64 bytes of on-chip EEPROM. PBP programs may use this for EEPROM operations and supports the Stamp's DATA, EEPROM, READ and WRITE commands.

To access off-chip non-volatile data storage, the I2CREAD and I2CWRITE instructions have been added. These instructions allow 2-wire communications with serial EEPROMs like Microchip Technology=s 24LC01B.

READ and WRITE will not work on devices with on-chip I2C interfaced serial EEPROM like the 12CE67x and 16CE62x parts. Use the I2CREAD and I2CWRITE instructions instead.

10.13. DEBUG

DEBUG in PBP is not a special case of SEROUT as it is on the Stamps. It has its own much shorter routine that works with a fixed pin and baud rate. It can be used in the same manner to send debugging information to a terminal program or other serial device.

DEBUG sends serial data out on PORTB, pin 0 at 2400 baud, unless otherwise DEFINEd.

Question marks (?) in DEBUG statements are ignored. The modifier ASC? should not be used.

10.14. FOR..NEXT

The BS2 automatically sets the direction of the STEP for a FOR..NEXT loop. If the ending value is smaller than the starting value and a STEP value is not specified, -1 is assumed. PICBASIC PRO™ always defaults to 1 if a STEP value is not specified. If a STEP of -1 is desired to make the loop count backwards, it must be specified:

For i = 10 To 1 Step -1

10.15. GOSUB and RETURN

Subroutines are implemented via the GOSUB and RETURN statements. User variable W6 is used by the BS1 as a four nibble stack. Thus, Stamp programs may have up to 16 GOSUBs and subroutines can be nested up to four levels deep.

The PICmicro MCUs have Call and Return instructions as well as an eight level stack. PBP programs make use of these instructions and may use four levels of this stack, with the other four levels being reserved for library routines. Thus, W6 is unused, subroutines may still be nested up to four levels deep (12 for 17Cxxx and 27 for 18Cxxx) and the number of GOSUBs is limited only by the PICmicro MCU's code space.

10.16. I2CREAD and I2CWRITE

The I2CREAD and I2CWRITE commands differ from the original PicBasic Compiler=s I2CIN and I2COUT commands. The most obvious difference is that the data and clock pin numbers are now specified as part of the command. They are no longer fixed to specific pins.

The other difference is that the control byte format has changed. You no longer set the address size as part of the control byte. Instead, the address size is determined by the type of the address variable. If a byte-sized variable is used, an 8-bit address is sent. If a word-sized variable is used, a 16-bit address is sent.

10.17. IF..THEN

The BASIC Stamps and the original PicBasic compiler only allow a label to be specified after an IF..THEN. PICBASIC PRO™ additionally allows an IF..THEN..ELSE..ENDIF construct as well as allowing actual code to be executed as a result of an IF or ELSE.

10.18. MAX and MIN

The MAX and MIN operator=s function have been altered somewhat from the way they work on the Stamp and the original PicBasic Compiler.

MAX will return the maximum of two values. MIN will return the minimum of two values. This corresponds more closely to most other BASICs and does not have the 0 and 65535 limit problems of the Stamp=s MIN and MAX instructions.

In most cases, you need only change MIN to MAX and MAX to MIN in your Stamp programs for them to work properly with PBP.

10.19. SERIN and SEROUT

SERIN and SEROUT use BS1 syntax. SERIN2 and SEROUT2 use BS2 syntax. A BS2 style timeout has been added to the SERIN command.

SERIN and SEROUT have been altered to run up to 9600 baud from the BS1 limit of 2400 baud. This has been accomplished by replacing the little used rate of 600 baud with 9600 baud. Modes of T9600, N9600, OT9600 and ON9600 may now be used.

600 baud is no longer available and will cause a compilation error if an attempt is made to use it.

10.20. SLEEP

PBP=s SLEEP command is based solely on the Watchdog Timer. It is no longer calibrated using the system clock oscillator. This change was necessitated by the effect Watchdog Timer resets have on the PICmicro.

Whenever the PICmicro was reset during SLEEP calibration, it altered the states of some of the internal registers. For smaller PICmicros with few registers, these registers could be saved before and restored after calibration resets. However, since PBP may be used on many different PICmicros with many registers that are altered upon reset, this save and restore proved to be too unwieldy.

Therefore SLEEP runs in an uncalibrated mode based strictly upon the accuracy of the Watchdog Timer. This ensures the stability of the PICmicro registers and I/O ports. However, since the Watchdog Timer is driven by an internal R/C oscillator, its period can vary significantly based on temperature and individual chip variations. If greater accuracy is needed, PAUSE, which is not a low-power command, should be used.