How Microcontrollers Work

You program a BASIC Stamp using the BASIC programming language. If you already know BASIC, then you will find that the BASIC used in a Stamp is straightforward but a little stripped-down. If you don't know BASIC, but you do know another language like C, Pascal or Java, then picking up BASIC will be trivial. If you have never programmed before, you probably want to go learn programming on a desktop machine first. Here is a quick rundown on the instructions available in Stamp BASIC. (For complete documentation, go to Parallax: BASIC Stamp Documentation.)

Standard BASIC instructions:

for...next - normal looping statement

gosub - go to a subroutine

goto - goto a label in the program (e.g. - "label:")

if...then - normal if/then decision

let - assignment (optional)

return - return from a subroutine

end - end the program and sleep

Instructions having to do with I/O pins:

button - read a button on an input pin, with debounce and auto-repeat

high - set an I/O pin high

input - set the direction of an I/O pin to input

low - set an I/O pin low

output - set the direction of an I/O pin to output

pot - read a potentiometer on an I/O pin

pulsin - read the duration of a pulse coming in on an input pin

pulsout - send a pulse of a specific duration out on an output pin

pwm - perform pulse width modulation on an output pin

reverse - reverse the direction of an I/O pin

serin - read serial data on an input pin

serout - write serial data on an output pin

sound - send a sound of a specific frequency to an output pin

toggle - toggle the bit on an output pin

Instructions specific to the BASIC Stamp:

branch - read a branching table

debug - send a debugging string to the console on the desktop computer

eeprom - download a program to EEPROM

lookdown - return the index of a value in a list

lookup - array lookup using an index

nap - sleep for a short time

pause - delay for the specified time

random - pick a random number

read - read a value from EEPROM

sleep - power down for the specified time

write - write data to EEPROM

Operations:

+ - addition

- - subtraction

* - multiplication (low-word)

** - multiplication (high-word)

/ - division

// - mod

max - return maximum of 2 values

min - return minimum of 2 values

& - AND

| - OR

^ - XOR

&/ - NAND

|/ - NOR

^/ - XNOR

If statement logic:

=

Keep Reading Below

<>

<

<=

>

>=

AND

OR

Variables

All variables in the BS-1 have pre-defined names (which you can substitute with names of your own). Remember that there are only 14 bytes of RAM available, so variables are precious. Here are the standard names:

w0, w1, w2...w6 - 16-bit word variables

b0, b1, b2...b13 - 8-bit byte variables

bit0, bit1, bit2...bit15 - 1-bit bit variables

Because there are only 14 bytes of memory, w0 and b0/b1 are the same locations in RAM, and w1 and b2/b3 are the same, and so on. Also, bit0 through bit15 reside in w0 (and therefore b0/b1 as well).

I/O pins

You can see that 14 of the instructions in the BS-1 have to do with the I/O pins. The reason for this emphasis is the fact that the I/O pins are the only way for the BASIC Stamp to talk to the world. There are eight pins on the BS-1 (numbered 0 to 7) and 16 pins on the BS-2 (numbered 0 to 15).

The pins are bi-directional, meaning that you can read input values on them or send output values to them. The easiest way to send a value to a pin is to use the HIGH or LOW functions. The statement high 3 sends a 1 (+5 volts) out on pin 3. LOW sends a 0 (Ground). Pin 3 was chosen arbitrarily here -- you can send bits out on any pin from 0 to 7.

There are a number of interesting I/O pin instructions. For example, POT reads the setting on a potentiometer (variable resistor) if you wire it up with a capacitor as the POT instruction expects. The PWM instruction sends out pulse-width modulated signals. Instructions like these can make it a lot easier to attach controls and motors to the Stamp. See the documentation for the language for details. Also, a book like Scott Edward's Programming and Customizing the BASIC Stamp Computer can be extremely helpful because of the example projects it contains.