PBASIC Language Basics: Dhinesh Sasidaran
PBASIC Language Basics: Dhinesh Sasidaran
PBASIC Language Basics: Dhinesh Sasidaran
PBASIC INTRODUCTION
PBASIC stands for Parallax BASIC which is a variant of BASIC. This special language
has familiar BASIC instructions such as FOR..NEXT, IF..THEN and GOTO along with
some useful extra instructions that are specially for input and output (I/O). Programs can
be written using the STAMP programming software and downloaded on a serial port to
the BASIC Stamp.
You can use the DEBUG command to print values to the debugging terminal managed by
the STAMP programming software. The command allows you to view variables or track
the flow of execution through your program. Run the parallax software and write the fol-
lowing program in the STAMP editor window:
‘{$STAMP BS2}
DEBUG “My first STAMP Program!”
END
Run your program. A window should pop up and you should see the debugging message
on the screen.
What appears is what was sent from the BASIC Stamp, through the programming cable
to the PC. The first line of the program is a special comment (or directive) which indicates
which version of the Stamp is in use.
Write your program using the BASIC Stamp Windows Editor as shown in Figure 1.
After entering your program using the Stamp editor, select Run --> Run (or by pressing
Ctrl-R). This tokenizes your code and downloads it into the Stamp.
The BASIC Stamp has 32 bytes of variable RAM space arranged as shown in Figure 2.
INS/OUTS
INH/OUTH INL/OUTL
DIRS W6
DIRH DIRL B13 B12
W0 W7
B1 B0 B15 B14
W1 W8
B3 B2 B17 B16
W2 W9
B5 B4 B19 B18
W3 W10
B7 B6 B21 B20
W4 W11
B9 B8 B23 B22
W5 W12
B11 B10 B25 B24
IDENTIFIERS
Identifiers are names that a programmer makes up when writing a program. In PBASIC,
an identifier must begin with a letter and can be composed of a combination of letters,
digits and the underscore character (_) but cannot be the same as PBASIC keywords
(reserved words) or labels. PBASIC is not case sensitive and therefore both upper case
and lower case letters can be used in an identifier.
Appendix B of the STAMP manual provides a list of PBASIC keywords. You can down-
load the manual from :
(www.parallaxinc.com/downloads/download_documentation.htm)
For example:
This command selects one of the byte-sized registers to be assigned to the variable
named counter. The sizes of the variable can vary from WORD, BYTE, NIB (nibble) or
BIT.
Variables should be assigned by determining the largest value that will ever be stored
in it. The smallest size with respect to that should be chosen for the variable.
Aliases for variables can also be created using the VAR command. For example:
In the lines above, countodd is an alias to the variable counter. Anything stored in
counter shows up in countodd and vice versa. Both names will refer to the same phys-
ical address.
You can also use the alias as a window into a portion of another variable. This is done
using “modifiers”. For example:
The following table lists the modifiers and the definition of their use with variables.
Modifiers Definition
LOWBYTE Low byte of a word
HIGHBYTE high byte of a word
BYTE0 byte 0 (low byte) of a word
LOWNIB low nibble of word or byte
HIGHNIB high nibble of word or byte
NIB0 nib 0 of a word or byte
NIB1 nib 1 of a word or byte
NIB2 nib 2 of a word
NIB3 nib 3 of a word
LOWBIT low bit of a word, byte, or nibble
HIGHBIT high bit of a word, byte, or nibble
BIT0 bit 0 of a word, byte, or nibble
BIT1 bit 1 of a word, byte, or nibble
BIT2 bit 2 of a word, byte, or nibble
BIT3 bit 3 of a word, byte, or nibble
BIT4 ... BIT7 bits 4 through 7 of a word or byte
BIT8 ... BIT15 bits 8 through 15 of a word
3. Array variables
You can also declare arrays using the VAR command. An array has multiple items of
the same type. The following command is used to declare an array with a list of 3
bytes.
Math expressions can also be used when dealing with input and output pins. For
example:
B1 = 10
INPUT B1+1 ‘Make pin 11 an input pin
6. Constants
Constants can be specified using the CON keyword. For example:
delay CON 1000
Constants can also be defined in terms of another constant but must be kept fairly sim-
ple. For example
Whale CON 20
Dolphin CON Whale*2-1
NUMBERS
PBASIC allows you to use several numbering systems. By default, it assumes that num-
bers are in decimal, but you can identify binary and hexadecimal numbers with a prefix.
99 decimal
%1010 binary
$FE hex
BASIC Stamp performs integer match (whole numbers only) and drops any fractional por-
tion from the results of the computation. The size of the variables can be a bit (0-1), nibble
(0-15), byte (0-255) or word (0-65535) respectively.
For negative numbers, two’s complement math can be used for representation.
Negative Numbers
When the Stamp performs calculations, if the value is greater than the size of the variable,
then it just chops off the extra bits from left to right to form the appropriate size. For exam-
ple:
When adding $64 to $FFDF (DEC 100 and -33), the result would be $10043 but the
Stamp chops the extra bit(s) off, leaving the result to be $0043 = 67.
Sometimes there may be a need to add or subtract large numbers depending on the
application. In order to do this, we can concatenate (not physically) several registers
together. For example, suppose we wish to add 2, 32 bit numbers together:
W1 = $2
W2 = $FFFF
W3 = 0
W4 = 1
Program Example 1:
W1 = $2
W2 = $FFFF
W3 = 0
W4 = 1
nocy:
W1 = W1 + W3
W1 = 0
W2 = 1
W3 = $2
W4 = $FFFF
Program Example 2:
W1 = 0
W2 = 1
W3 = $2
W4 = $FFFF
W1 = W1 - 1
nobor:
W1 = W1 - W3
Non-integer numbers
The BASIC Stamp can only handle whole integer numbers. Therefore, methods of con-
version to integer numbers is necessary depending on your exact needs. For the expres-
sion:
F = 1.8 * C + 32
can be re-written as
F = 18 * C + 320
This conversion however means that the actual result 1/10th of the result obtained from
the calculation.
One way to deal with fractional numbers is to use the ‘ */ ’ operator. It has the effect of
multiplying a value by a whole number and a fraction. This operator places the whole
number portion in the upper byte, multiplies the fractional part by 256 and places the
result in the lower byte.
For example, if multiplying value with 1.8, using the ‘ */ ’ would mean 0.8 * 256 = 204.8 =
205 and therefore:
Upper byte : 01
Lower byte : CD
so 1.8 can be represented as $01CD. Therefore, some care will need to be taken when
dealing with non-integer numbers.
Now, F = 1.8 * C + 32 can be re-written as F = C */ $01CD + 32