The
Basic Stamp
Purpose of Tutorial
Part One-Using the Editor
Part Two-Controlling Pins With BASIC Commands,
Binary System
Part Three-Addresses. Variables. Variable
Commands
Part Four-Execution Order Commands
Part Five-Reading High/Low Pin Conditions And
Variable Resistances
Command Index with Syntax
Purpose Of This tutorial
The Basic Stamp computer is a product of
Parallax Incorporated. The Stamp manual provided by Parallax is often
confusing and advanced for the person with no programming experience.
This tutorial attempts to give the beginning programmer a little more
detailed information about the function and use of the most common
P-Basic commands used with the Stamp.
The Stamp is a microcontroller used to control
electronic devices. It sends output signals to these devices and
"listens" to the input information they provide. It is necessary to have
some knowledge of the how basic electronic components operate and how
they should be hooked into a circuit. This tutorial will give example
programs which use the following components:
-
Light Emitting Diodes
(LED's)
-
Resistors
-
Capacitors
-
Switches
-
Speakers
-
Thermistors
-
Photo Resistors
-
Transistors
The applications described in this tutorial
are very basic electronic circuits. They were selected primarily to
provide the beginner with programming experience. There are countless
other applications which engineers and electronic experimenters are glad
to share with the public. A wealth of information can be found at
Parallax's web site, Parallaxinc.com. This web site also give
information on joining the company sponsored chat line and additionally
provides interesting links to related web sites.
Part 1: Using the Editor
The editor is a kind of word processor program
which is supplied on a disk from the manufacturer. We download this
program from the disk to the STAMP directory in our PC. We use the
editor to type our lines of programming. The editor also has a section
which talks and listens to the Stamp using special commands. The PBASIC
commands are run line by line. That is, the computer does what the
commands tell it to do one line after another and only one command at a
time. The commands tell the stamp which operations to perform and which
of the 8 I/O (input/output) pins are to be involved.
To enter the editor from the c:> prompt, type:
cd\stamp [Enter]
"STAMP" [Enter]
Once in the editor, programs can be saved and
loaded. To save a file, hold down Alt and press S. Then type a file name
using 8 characters or less. To load a file, hold down Alt and type L,
then enter the name of the saved file. To exit the program, hold down
alt and type Q.
Editor Function Keys
F1 Editor
command help
Alt L
List programs available
Alt R Run
program
Enter
Enter information
Shift (hold) arrows highlights text with arrow
direction
Escape Key
Cancel highlighted text
Delete Key
Delete highlighted text
Alt X Cut
highlighted text and place in clipboard
Alt C
Copy highlighted text to clipboard
Alt V
Paste clipboard into document
Alt P Get
Scale of Potentiometer
NOTE:
More detailed editor commands are in Stamp manual supplied by Parallax.
Comments may be added to program lines. Begin
comments with an apostrophe. (') The computer will not read anything
after an apostrophe until the next line begins. For example:
pause 100 'pause for 0.1 second
Part 2 - Controlling Pins and Time
Topics
covered:
LOW, HIGH, DIRS, PINS, OUTPUT, REVERSE,
TOGGLE, PAUSE, SLEEP, END, AND SOUND
The Basic Stamp has 8 I/O (input/output) pins
which can be connected to various electrical and electronic components.
These pins can be programmed to receive electrical information or send
out electrical information in various forms. When the pins are in the
output mode, they can be either high (positive) or low (negative).
Command
Function
Output 0 or Dir0=1 makes pin 0 an output pin
Input 7 or Dir7=0 makes pin 7 an input pin
Dirs=%00001010 or Dirs=10 Sets all pins marked
0 to input and all pins marked 1 to output.
Low 3 or Pin3=0 Makes pin 3 low or negative.
High 4 or Pin4=1 Makes pin 4 high or positive.
Pins=%11001001 (binary) Makes all pins marked
1(high) and makes all pins marked 0(low). In this example, pins 0,3,6,&
7 are high. pins 1,2,4,& 5 are low
NOTE: The
% signs means the number system in use is BINARY. Pins are numbered from
right to left 7,6,5,4,3,2,1,0.
The Conversion of Binary into Decimal
A decimal number can be used to control the
pins the same as a binary number. Each pin has a decimal value as shown
below.
pin 0=1 pin 4=16
pin 1=2 pin 5=32
pin 2=4 pin 6=64
pin 3=8 pin 7=128
To convert a binary number to a decimal
number, determine which pins are to be made high, add decimal value of
each pin to be made high and the total is the decimal conversion value.
Above, the command PINS=%11001001 was used.
This could also have been written PINS=201 (1+8+64+128=201).
Reverse
Reverse 6
(Reverses Input/Output state of Pin 6.)
Toggle
Toggle 0
(Reverses the high/low state of pin 0 .)
Pause
Pause 100
(Pauses the program for 100 milliseconds or 0.1 second (100 x 0.001
seconds = .1 second) before moving on to the next command line. Pause
can be set from 1 to 65535 milliseconds.)
Nap
Nap,
period Nap enters a "Sleep" mode for a short period of time. Period can
be any number from 0 to 7, each interval representing about 18ms.
Example
Nap 7
This reduces the computers power to 20
micro-amps for 2304ms, or 2.304 seconds.
Sleep
Sleep
seconds Sleep mode is similar to NAP, just longer. Seconds is a number
from 1 to 65535. This is from 1 second to a little more than 18 hours.
Example
Sleep 120 This tells the stamp to reduce power
to 20 micro-amps for 2 minutes
Example:
BLINK:
Dirs=11111110
High 6
Pause 10
Low 6
Sleep 3600 'Go to sleep for 1 hour (60 secs x 60 =1 hr)
Goto BLINK
End (End
stops the line by line execution of the program. The reset switch will
have to be activated, or the program reloaded, or power taken off and
then applied in order for the program to run again.)
Sound,(tone,duration,...tone, duration)
Sound 0,(50,100) Sends a tone (50) for a
duration of 100 out of pin 0. The best tone range to use with small
speakers is 30 to 120. Speakers The duration can be from 0 to 255.
Multiple tones may be programmed into one command set.
Sound 0,(70,100,0,100,120,100) Note that the
first and third sound sets produce a tone of 70 for a duration of 100
and 120 for a duration of 100 while the second set produces silence for
a duration of 100. Using a tone of 0 produces no sound or a pause in
sound between the first and third sound sets.
The following is an approximate music scale:
C 35, C# 40, D 44, D# 49, E 53, F 58, F #60, G
65, G# 68, A 72, A #75, B 78, C 81
Multiple commands can be put on the same line
separated with a colon.
Example:
Sound 0,(90,120): High 3: Pause 100
Part 3 - Branching And Looping
Topics
Coverd:
Branching - GOTO, GOSUB/RETURN, BRANCH,FOR...NEXT,
IF...THEN, END
Address
An ADDRESS is a label for a portion of a program. It is useful when you
wish to execute different portions of the program out of line-by-line
sequence. To go to an address.
Goto
Goto address (To go to an address, use the
command GOTO.)
The following is a sample program with an
address and the GOTO command:
Dirs=%11111110
Start: The colon tells the Stamp that this is an address. Don't use
P-basic command words as address titles.
High 6 Makes pin 6 positive 5 volts
Pause 120 Pauses the program for 120 milliseconds.
Low 6 Makes pin 6 negative
Pause 20 Pauses the program for 20 milliseconds.
Goto start Loops back up to the Start: and begins the program again
Gosub...Return
Gosub address
Return (sends program back to latest Gosub.
The following program uses the Gosub/Return
pair command. Gosub directs the program to go to another address and
then return to where it left.
Dirs=%11111111
Begin:
High 4
Pause 200
Low 4
Pause 200
Gosub TOOT
Goto begin
TOOT:
Sound 0,(100,200)
Return
Variables
A variable is a location in the computer's
memory where numbers can be stored. There are three different types of
variables - bits, bytes, and words. There are 8 bits in each byte and 2
bytes in each word.
High or low values, 0 or 1, can be stored in
bits.
Values up to 255 can be stored in bytes.
Very large values up to 65,535 can be stored
as words.
Bytes are used with most commands. To store a
number in a byte, state the name of the variable, and set it equal to
that number. For example, to store the number 0 in a bit, you would type
bit2=0. To store the number 55 in a byte, type b3=55. To store the
number 1233 in a word, type w4=1233. Only bits 0 through 15 can be
addressed. There are 14 bytes (b0 through b13) and 7 words (w0 to w6).
W0 is composed of b0 and b1, w1 is composed of
b2 and b3 and so on to w6 which is composed of b12 and b13.
B0 is composed of bit0 to bit 7 and b1 is
composed of bit8 to bit 15.
Caution should be taken not to try to store
data in variable already being used.
For more detailed information about variables,
see the stamp manual.
RANDOM with DEBUG
Random
wordvariable
Debug
variable
This will generate a random number from 1 to
65535 and store it in a word (two byte) variable specified in the
command line. In the example which follows, the random number will be
stored in word W1 which is made up of byte 2 (b2) byte 3 (b3). The
program then uses the lower byte (b2) of w1 as part of the Sound
command.
Example:
ANNOY: Address
Random w1
Debug W1 'displays the value of w1 on editor screen
Debug B2 'B2 is lower byte of W1 (0 to 256)
Sound 0, (b2, 100)
Goto ANNOY
If...Then
Syntax: If, condition, Then,
address
If the "condition" is true than the program
will goto the address. Otherwise, it will simply proceed down to the
next program line.
Condition can be any logical statement, such
as b2 < 3, or pins=255. Operators allowed are:
-
= Equals
-
< Less Than
-
> Greater Than
-
>= Greater than or equal
to
-
<= Less than or equal to
-
<> Not equal to
The "address" part must be an address in the
program.
In this program example we use the Annoy
program from the RANDOM introduction and eliminate any sounds below 30
or above 120.
ANNOY:
Random w1
Debug W1 'displays the value of w1 on editor screen
Debug B2 'B2 is lower byte of W1 (0 t0 256)
If b2<30 or b2>120 Then ANNOY
Sound (b2, 100)
Goto ANNOY
For-Next
For
variable = start To end Step increment
The For-Next command provides a method of
repeating commands for a specified number of times. The following
program will blink an LED attached to pin 1, 6 times:
For b2= 0 to 5 (Note that the 0 counts for one
time.)
high 1
pause 100
low 1
pause 100
next b2
The following program will blink the LED 4
times.
For b2 = 2 to 8 step 2
High 1
Pause 100
Low 1
Pause 100
Next b2
Note that the program increments through in
steps of 2. Unless the step is written, the program will increment
through plus one step or one number at a time.
The following program will blink the seven
LED's in sequence.
For b2 = 1 to 7 'step 1
high b2
pause 100
low b2
pause 100
next b2
For b2 = 8 to 2 step -2 'increments in steps of -2
high b2
pause 100
low b2
pause 100
next b2
Note that this program used the For...Next to
count the number of times the program looped through. And, it also used
the value of b2 to determine which pin would be made high.
Branch
Branch
offset variable
This command allows us to send the program
operation to an address selected from a list of several addresses.
Example:
For b2= 0 to 2
Branch b2, (Noise, Blink, Flash) b2 is called the OFFSET
Proceed:
next b2
Noise:
Sound 0,(50,100,70,120)
Goto Proceed
Blink:
For b3=0 to 8 step 2
high b3
pause 100
low b3
next b3
Goto Proceed
Flash:
Pins =255
pause 200
pins=0
Goto Proceed
When the value of b2 is 0, the program will
branch and run the NOISE subroutine. When the value of b2 is 1, it will
branch to and run the BLINK subroutine. etc
PART 4-Execution Order Commands
Let
The let command allows the programmer
to assign numerical values to variables.
Example:
Let b4=120 Assigns the value of 120 to the
variable b2
Let b6=3 + 7 Assigns the value of 10 to
variable b6
Below is a list of the math operator symbols:
-
+ add
-
- subtract
-
* multiply
-
/ divide (returns
quotient)
-
// divide (returns
remainder)
-
MIN keep variable greater
than or equal to value
-
MAX keep variable less
than or equal to value
-
& Logical AND
-
| Logical OR
-
^ Logical XOR (Exclusive
or)
-
&/ Logical AND NOT
-
|/ Logical OR NOT
-
^/ Logical XOR NOT
Example:
let b2= 10/5 b2 is now equal to 2
let b2= b2*3 b2 is now equal to 2 x 3 or 6
let b2= b2//4 b2 is now equal to 2 or the
remainder of 6 divided by 4.
The Stamp performs mathematical operations
strictly from left to right. 4+2*10 would normally equal 24 as most
calculators would give the multiplier first priority and then add the 4.
However, the Stamp returns a result of 60.
Remember that values up to 255 may be stored
as BYTES but higher values up to 655,35 must be stored as words ie, w0
to w6
Lookup
This command asks the program to lookup a
value specified by an offset value stored in a variable. Once it has
"looked up" the value, that value is then stored as another variable.
Example:
Lookup b2, (50,100,87,94,60),b3
If the value of b2 (offset value) is 2, then
87 will be stored in b3. If b2 is 0 then 50 will be stored in b3, ect.
Example:
for b2= 0 to 5 'loops b2 six times
lookup b2,(103,104,105,106,107,101), b3 'Assigns one of the six values
to b3, depending on b2's value.
sound 0, (b3,100) 'makes a different sound pitch, based on the value of
b2
pause 10
next b2
It is important to remember that the first
value is zero, not 1, therefore the loop starts at 0, not 1.
Lookdown
Lookdown
variable, (value0, value1, value2, ...), variable
Like the Lookup command, the
Lookdown command checks the variable, and gives it a specified
value. If variable matches one of the values, than the position value is
assigned to another variable.
Example:
lookdown b2 (10, 9, 8 , 7),b3
If b2 were equal to 8, then it store the value
of 2 in b3. If b2 were 7 then it would assign the value of 3 to b3. If
the offset value of b2 were 25 then nothing would be stored in b3
because there is no 25 in the list of values. The program would just
proceed to the next command line. Once again, remember that the position
value numbers start with zero, not one.
Part 5- Reading High/Low Pin Conditions And Variable Resistances
The Stamp can check one or all the pins to see
if there is a positive (high) voltage or negative (low) voltage applied
to one or all of the pins. The following is a sample program to perform
a check for voltage conditions on pin 1.
CHECK:
Dirs=00000000
Pins=00000000
If pin1=0 then CHECK:
Goto ALERT
ALERT:
Sound 0,(100,100,0,100,120,100)
If pin1=1 then ALERT
low 0
Goto check
Pot
Pot pin,
scale, variable
The POT command allows the stamp to check for
changes in resistance attached to a particular pin. A pin is connected
to a variable resistance then to a capacitor and the other side of the
capacitor is connected to the negative buss bar.
POT 2, 75, b2 This checks the resistance
connected to pin 2 and saves the result in variable b2.
To use the POT statement, it is first
necessary to find the scale value. Press ALT/P and select and enter the
pin number. Vary the resistance full range and record the lowest number
shown in the SCALE window. This is the number to use in the POT command.
Press the space bar and note the READING window is now highlighted. Vary
the resistance through the full range and record the highest and lowest
numbers. These are the numbers which will be stored in the variable
specified in the POT command. The resistances may be a thermistor, photo
resistor, potentiometer, etc.
In the following program, the SCALE value and
READING value will not be the same as yours.
START:
Dirs=00000000
Pins=00000000
FIRECHECK:
Pot 1, 100, b2
If b2>130 then START
For b3= 1 to 10
Sound 0,(90,100)
Next b3
Low 0
Goto FIRECHECK
Pulsin
Pulsin
pin, state, variable
This command measures an input pulse in 10
second units. Pin states at which pin the pulse is measured (0-7). State
specifies which state, high or low (0 or 1), must occur before beginning
to measure. Variable is the variable where the result of the measurement
(1 to 65536) is to be stored. If a byte variable is used, then if the
result is greater than 0.65536 seconds, then the result will be 0.
Example:
Pulsin 4,0 measures an input pulse on pin 4
and starts measuring when a high-to-low transition occurs. Measurement
stops when the next low-to-high transition is sensed.
Pulsout
Pulsout
pin, duration
This command sends a pulse in 10 second units
to a specified pin. The Stamp actually inverts the voltage output of the
pin for this pulse duration. Duration can be any number from 0 to 65535.
Thus, PULSOUT 0, 1000 produces a pulse for 10 milliseconds on pin 0.
This command is especially useful for driving
servo motors.
Pulseout 100 moves the servo to a zero degree
position. Pulsout 200 moves the servo to a ninety degree position.
Numbers in between move the servo proportional degrees. CAUTION! Do not
use values that are lower than 100 or higher than 200.
In the following program FOR/NEXT loops and
PAUSES are used because the servo needs to be pulsed and paused several
times to allow enough time for the servo motor to move.
START:
Dirs=00001000
Pins=00000000
For b2= 1 to 20 Repeats following commands 20 times
Pulsout 3, 100 Sends a pulse to the servo at pin 3 for 100 milleseconds.
Pause 10
Next b2
Pause 100
For b3= 1 to 20
Pulsout 3, 200 Sends a pulse to the servo at pin 3 for 200 milleseconds.
Pause 10
Next b3
Goto START
Pulse Width Modulation
PWM
This command is used to produce a pulse, with
a user defined modulation for a given amount of cycles, then returning
the pin to its original state. This command is used to produce analog
levels using a capacitor. When using PWM, the capacitor is placed in
parallel with the output device, going to ground. A resistor should be
placed before the parallel portion.
PWM pin, duty, cycles Pin is the pin you want
to use. Duty (0-255) specifies the analog level desired (0-5 volts).
Cycles (0-255) is the number of 5ms cycles given to charge the
capacitor. Larger capacitors will require more cycles.
Command Index
Branch-
Part 4
Debug- Part 3
Dirs- Part 2
End- Part 4
For-Next- Part 3
Gosub- Part 3
Goto- Part 2
High- Part 2
If-Then Part 4
Input- Part 2
Let- Part 4
Lookdown- Part 4
Lookup- Part 4
Low- Part 2
Nap- Part 2
Output- Part 2
Pause- Part 2
Pins- Part 2
Pot- Part 5
Pulsout- Part 5
PWM- Part 5
Random- Part 4
Return- Part 3
Reverse- Part 2
Sleep- Part 2
Sound- Part 3
Toggle- Part 2
Command Syntax
Branch
offset,(address0,address1,.....addressN)
Debug
'sends variable contents to PC for viewing.
End 'puts
stamp to sleep until reset or repowered.
For...Next
FOR variable = start TO end STEP increment
Gosub
address 'Use with RETURN
Goto
address
High pin
If....Then
IF variable {math operator} value THEN address
Input pin
Let
variable = value LET variable = value {math operator}value
Lookdown
target value,(value0,value1,.....valueN) variable '0toN
Lookup
offset,(value0,value1,......valueN) variable 'value
Low pin
Nap
period 'Period is 0 to 7 (18 ms to 2.3 seconds)
Output
pin
Pause
milliseconds '1 to 65535
Pot
pin,scale,variable
Pulsin
pin,state,variable
Pulsout
pin,#*10 micro-seconds '# = 1 to 65,535
PWM
pin,duty,cycles
Random
wordvariable
Read
location, variable
Return
'Moves program execution back to most recent GOSUB
Reverse
pin
Sleep
seconds '1 sec to 18 hours
Sound
pin,(note, duration,note,duration,.......)
Toggle
pin
NOTE: Not all commands are listed. See The
Basic Stamp manual for more