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 page xx".

Programming Basics

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

Numeric constants

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:

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 line to 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
  goto loop

Symbol Definition

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 more readable.
symbol LED = 1
symbol MOTOR = 0

  high LED
  pause 1000
  toggle LED
  toggle MOTOR
  pause 250
  goto loop
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 pin numbers.

Other Formatting Conventions

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:

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

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
  goto continue
  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
  next b4
  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

Page by wkdurfee@tc.umn.edu
Last modified: Wed Feb 12 17:03:58 CST 1997