Basic Stamp tokenizing and flashing from the Linux CLI

For those of you who aren’t familiar with the Basic Stamp, its a small microcontroller available from Parallax. It runs at a whopping 20Mhz and has a full 2K of storage on board for instructions. Although it may not sound like much, its more than enough to program the stamp to do some interesting things. The Basic Stamp is programmed in PBASIC, parallax’s version of BASIC the stamp interprets.

Rather than waste your time explaining the coolness of the Basic Stamp, (if you’ve found this page you’re probably already interested) I want to focus on how it can be utilized under linux. Currently, Parallax has a very nice PBASIC IDE but its available for windows only. It can however be run under Wine, and with the proper font settings it doesn’t look too horrible and is quite functional. But what is the fun in that?

If you’re more of a ‘vim’ guy like me, there’s a better alternative. A quick visit over to sourceforge and you’ll find a set of command line tools available for download that make it easy to tokenize code and send it to the stamp without needing to reboot into windows.

After downloading the tarball, unpack it and move into the directory. All you need to do now is build it, and it shouldn’t require much more than a simple ‘make’.

unpack…

tdavis@tdavis-64:~$ tar xvzf bstamp-2006.05.31.tar.gz
bstamp/
bstamp/bstamp_run.cpp
bstamp/tokenizer.h
bstamp/PBASIC_Tokenizer_Software_Distribution_License.txt
bstamp/pbasic_examples/
bstamp/pbasic_examples/hello.bs2
bstamp/pbasic_examples/Makefile
bstamp/pbasic_examples/touch.bs2
bstamp/Makefile
bstamp/PBASIC_Tokenizer_Software_Distribution_License.pdf
bstamp/TODO.txt
bstamp/GPL.txt
bstamp/README.txt
bstamp/CHANGES.txt
bstamp/COPYING.txt
bstamp/bstamp_tokenize.cpp
bstamp/tokenizer.so
bstamp/error_handling.cpp
tdavis@tdavis-64:~$

move into the directory where you unpacked the tarball…

tdavis@tdavis-64:~$ cd bstamp/
tdavis@tdavis-64:~/bstamp$

and build it…

tdavis@tdavis-64:~/bstamp$ make
tdavis@tdavis-64:~/bstamp$ make install

The last step is to make a symbolic link to whatever serial port your stamp is hooked up to. In my case it was /dev/ttyUSB0 because I’m using a serial to usb converter, but for a regular serial connection its likely to be /dev/ttyS0 or /dev/ttyS1. The symlink needs to point the serial device to a new location, /dev/bstamp. If at any point you encounter any problems, don’t forget to check out the README.txt that comes with the program.

tdavis@tdavis-64:~/bstamp$ sudo ln -s /dev/ttyUSB0 /dev/bstamp

Now you just need some code to tokenize, as an example here’s a simple program I wrote that does nothing more than monitor the light levels off a photo resistor and produce output accordingly. (It beeps and blinks!) Its certainly not the most beautiful code, but it does the trick.

'for Basic Stamp 2
'basic light meter that shows on 7 segment display
'and controls LEDs related to the amount
'of light detected; can also produce
'audio output through a piezo electric
'speaker based on the amount of light detected
'@ Tyler Davis 2007
' {$STAMP BS2}
' {$PBASIC 2.5}
DEBUG "program running!"
index VAR Nib
time VAR Word
dark CON 25
OUTH = %00000000
DIRH = %11111111
'FREQOUT 2, 2000, 4500 'test spk on p2

DO
GOSUB Get_RC 'grab light level info
GOSUB Delay 'delay between refreshes
GOSUB Update_Display
GOSUB sound 'play sound that changes as light
'measurments do
LOOP

Get_RC:
HIGH 0 '0 pin
PAUSE 3
RCTIME 0, 1, time
DEBUG HOME, "time = ", DEC5 time
IF (time > 200) THEN HIGH 6 'for green and
IF (time < 200) THEN LOW 6 'red lights
IF (time < 200) THEN HIGH 4
IF (time > 200) THEN LOW 4
IF (time < 35) THEN HIGH 5
IF (time > 35) THEN LOW 5
IF (time > 400) THEN HIGH 3
IF (time < 400) THEN LOW 3
RETURN
Delay:
PAUSE time
RETURN

Update_Display: 'to adjust 7 segment display
IF index = 6 THEN index = 0
LOOKUP index, [ %01000000,
%10000000,
%00000100,
%00000010,
%00000001,
%00100000 ], OUT
index = index + 1
RETURN

sound: 'to create audible sounds related to
'detected illumination levels
FREQOUT 1, 50, time + 4000
RETURN

Now I’m gonna assume you’re using your own code since my code is kind of worthless without a corresponding schematic, but I guess it could be figured out. Since I don’t feel like drawing up one I’ll just post a picture and if someone wants to try and figure it out they’re welcome to. (Sorry but they’re terrible pictures, I’ll try and get better ones up as soon as I get a chance).



Otherwise the process of tokenizing the code and writing it to the stamp is quite straightforward.

tdavis@tdavis-64:~/bstamp$ ./bstamp_tokenize lightmeter.bs2 lightmeter.tokenized
: Success
PBASIC Tokenizer Library version 1.23

tdavis@tdavis-64:~/bstamp$

tdavis@tdavis-64:~/bstamp$ cat lightmeter.tokenized | bstamp_run

If you run into any problems, be sure to verify you’re working with the correct serial device. Try a ‘dmesg | grep ttyS’ and see what it brings up. Or replace ‘ttyS’ with ‘ttyU’ if you have a USB connection.

This entry was posted in Eletronics, Linux. Bookmark the permalink.

One Response to Basic Stamp tokenizing and flashing from the Linux CLI

  1. Donn says:

    Nice. I just got the USB version of the Homework Board you have pictured. But I also have the Parallax Serial LCD w/speaker. I’ve noticed that the 9V battery tends to drain while connected to the board. So I’ll be looking to rig up a 9V adapter sometime. Or maybe those “Enercell” 9V batteries RadioShack sells kinda suck. :-)

    I also have a NetDuino, pretty cool also, and a regular Arduino board. The thing I like about bstamps is that they’re real easy to get up and running in a hurry, and the homework board provides a built-in (mini) breadboard. The Netduino would be even cooler if it had full Mono integration.

    As for the bigger, more powerful board, I’m looking forward to the UDOO when it finally comes out. It’s supposed to be compatible with Arduino shields.

    http://www.udoo.org/

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>