PBASIC Programming Tutorial
This is a brief tutorial for programming in PBASIC, the
variant of BASIC used for programming Stamp chips. The
tutorial assumes zero knowledge of programming and zero
programming experience. It is in bullet form because these
same notes are also used for an in-class lecture. It assumes
you have read or have access to the "BASIC Stamp Starter
Kit" manual. References to the manual are made by "Manual
- Computers are dumb machines
- Follow what you tell them EXACTLY
- Make NO assumptions
- Garbage in, garbage out
- Programs are the list of instructions (recipe) for
telling the computer what to do. Just like a recipe, the
computer will follow what you tell it to do. Will follow
exactly, even if what you wrote was wrong.
- Programming language defines a particular vocabulary set
and syntax. You must follow without deviations. Mistakes
often hard to find.
- PBASIC is (a little) like BASIC, but greatly simplified,
and tailored to running I/O ports on the Stamp chip.
Programs are sets of instructions, done in order. Most
instructions are for doing particular mathematical
operations (e.g. multiply, divide, add). There are special
instructions for handling program branching, that is for
when you don't want the program to go step by step. Other
instructions are for looping when you want sections of the
program to be performed multiple times. Still other
instructions are for subroutines (which will not be covered
in this tutorial)
Numbering and Data Sizes
Think of numbers being stored on the computer in memory
"cells", where cells can only hold numbers up to a certain
size. Since computers are based on binary (two-state)
logic, information on computers is can only be stored as
ones and zeros, and it is only sets of these ones and zeros
stored in memory cells
which gives us numbers. Memory cells (or numbers) come in
chunks of bits, bytes and words whose properties are listed
in the table below
| Cell Type || Size || number range
| bit || 1 || 0-1
| byte || 8 || 0-255
| word || 16 || 0-65,535
A byte is made of up 8 bits, and a word is made up of 2
You can write numeric constants in PBASIC in three different
ways: base 10 (decimal), base 2 (binary) and base 16
(hexidecimal). Decimal is most common and requires no
special notation. Binary is indicated by a '%' and hex by
a '$'. The following three lines are all ways of
writing the number 100 (base 10)
100 ' decimal
$64 ' hex
%01100100 ' binary
Variables can hold numbers. In PBASIC, you have a limited
number of variables which have pre-determined names. There
are 14 byte size variables (B0-B13) and seven word size
variables (W0-W6). Mostly, you will be using byte variables.
Pay attention to the following points:
- Avoid using W6, B12, B13 unless you really know what you
are doing because these are also used by the GOSUB
- The set of W variables resides in the same space as the
set of B variables. Thus, if you use B0 or B1, you cannot
use W0; if you use B2 or B3, you cannot use W1, and so on.
Page 9 of the manual lists the complete set of conflicts.
Labels and Comments
Labels are used to mark locations in the program for either
human reader convenience, or for program branch points.
As a marker, labels are followed by a colon. When used in a
branching instruction, they do not have a colon.
Comments make the program readable by humans. Anything on a
the right of an apostrophe is a comment which is ignored by
computer. Comments should be explanatory and should not
repeat the information stated by the instruction.
Here is a code chunk which shows use of a label (loop) and
comments. The comment "pause for 250" is useless because it
tells you the same thing as the instruction statement.
toggle 0 ' toggle motor
pause 250 ' pause for 250
To make the code more understandable, you may want to give
variables or constants more readable names. At the top of
your program you can define symbols. The computer will then
do a straight text substitution when it gets to the part of
the program where the symbol is used. By convention,
symbols are written in upper case. Here is an example where
an LED is connected to port 1 and a motor to port 0. Symbols
are used to make the instructions which manipulate the port
symbol LED = 1
symbol MOTOR = 0
Another advantage of using symbols is that
if you change the hardware (for example, you connect the
motor to port 4), you only have to change the symbol
definition, and not go through the entire program changing
Other Formatting Conventions
- Program is case insensitive, "MOTOR" is the same is
"motor" is the same as "Motor". Pick a convention for case
which makes sense to you. Many programmers use lower case
for everything but symbols.
Math and Expressions
You can add, subtract, multiply and divide using the
symbols +, -, *, /. That's pretty much it. See page 17 of
the manual for more.
Remember the following:
- Positive integers only.
- The input and results of math operations should be kept
in the range 0-255. If you go over and are not using word
variables, the answers will be truncated.
- Watch for multiply overflows. The instruction line
"b4 = 50*50" will give you the wrong answer because
the result will be clipped to the lower eight bits.
- Division truncates. The line "b4 = 5/2" will
result in b4 having a value of 2.
- Match expressions are evaluated strictly left to right
and there are no parentheses. Make sure you read about this
on page 12 of the manual.
The equals sign as a special meaning in computer programs.
Think of it not as an equation to be solved, but rather as
an assignment operator. The computer evalutes the expression
to the right of the equals sign and puts the result in the
variable to the left of the sign. For example the
instruction line "b4 = b4/2"
will divide variable b4 by 2 and put the result into b4. If
b4 contained 14 before the instruction, it will contain 7
after. Note that the line "2 = b2 + b3"
will result in an error because the computer there is no
variable on the left and the computer cannot solve the
IF/THEN for Program Branching
Use an if/then construct for a program that must do one
thing if a condition is true (e.g. the switch is closed)
and another thing if the condition is false (switch open).
Here is an example:
b2 = 5
b3 = 7
if b2 < b3 then motor
high 1 'led on
high 0 ' motor on
The program will branch to the statement after the label
"motor" because the expression between the "if" and the
"then" in the if/then statement evaluated as true. See page
36 of the manual for more.
FOR/NEXT for Program Looping
Us a for/next statement to repeat a chunk of code multiple
times. Here is an example:
for b4 = 1 to 5
high 1 ' led on
pause 250 ' wait
low 1 ' led off
pause 500 ' wait
high 0 ' motor on
This will flash an LED five times. The variable b4 is
initially set to 1. Then the section of code between the
"for" line and the "next" line is executed.
Each time through the loop, b4 is incremented by
one. After the fifth time through, the program goes on to
the next section of code, which in the example is to turn
on the motor. See page 31 of the manual for more.
Differences Between PBASIC and BASIC
- PBASIC has no line numbers, and will not take line
- PBASIC variables and math only work on positive
integers. There is no such thing as a negative number, no
such thing as a decimal point and no such thing as a
floating point number in PBASIC
- PBASIC has a VERY limited set of instructions. Those
listed on pages 23 and 23 of the BASIC Stamp Starter Kit
Manual are it. No more no less.
- PBASIC variables must be named B0-B13 and W0-W6 (see
page 9 of the manual). No
variables named I, K, or data can be used.
Page by firstname.lastname@example.org
Last modified: Wed Feb 12 17:03:58 CST 1997