Electronics Projects: Block DC while Passing AC
Electronics Projects: How to Use LEDs to Detect Polarity
Electronics Projects: How to Flash an LED with a BASIC Stamp

Electronics Projects: How to Write a PBASIC Program

You can use a BASIC Stamp microcontroller to create some special effects in your digital electronics projects. But first you have to learn how to write programs to control the controller. If you’ve never done any form of computer programming before, you’re in for a fun and fascinating adventure, during which you learn how computers really work.

In a nutshell, a computer program is a set of written instructions that a computer knows how to read, interpret, and carry out. The instructions are written in a language that both humans and computers can read. The instructions aren’t quite English, but they resemble English enough that English-speaking people can understand what they mean.

Computer programs are stored in text files that consist of one or more lines of written instructions. In most cases, each line of the computer program contains one instruction. Each instruction tells the computer to do something specific, such as add two numbers together or make one of the output pins go HIGH.

The trick of computer programming is to put the right instructions together in the right sequence to get the program to do exactly what you want it to do. Of course, to do that, you need to have a solid understanding of what you want the program to do, and you need to have a solid knowledge of the variety of instructions that are available to you.

The PBASIC programming language consists of about 70 different types of instructions. But don’t be discouraged; you can write useful programs using only a handful of these commands.

In just about every book on programming languages, the first program presented is called Hello World. This simple program displays the string “Hello, World!” as a way of demonstrating what the simplest possible program looks like.

In PBASIC (the official name of the BASIC language that’s used on BASIC Stamps), the Hello World program consists of three lines:

' {$STAMP BS2}
' {$PBASIC 2.5}
DEBUG "Hello, World!"

The first two lines are called directives. They don’t tell the BASIC Stamp to actually do anything; instead, they provide information that the Stamp Editor needs to know to prepare your program so that it can be downloaded to the Stamp.

The first line indicates that the microcontroller you'll run the program on is a BASIC Stamp 2 (BS2). The second line indicates that this program uses version 2.5 of PBASIC for this program. (That’s the current version.)

Every program you write must include these two lines. Fortunately, you don’t have to type them yourself. Instead, you can use menu commands or toolbar buttons to insert the directives automatically:

  • Directive→Stamp→BS2: Inserts the $STAMP BS2 directive to indicate that you're using BASIC Stamp 2.

  • Directive→PBASIC→Version 2.5: Inserts the $PBASIC 2.5 directive to indicate that you're using version 2.5 of PBASIC.

The third line of the Hello World program is the only line that actually tells the BASIC Stamp to do something. This command, called DEBUG, tells the BASIC Stamp to send a bit of text to the computer connected via the USB port (The DEBUG command always consists of two parts: The word DEBUG followed by some text that must be enclosed in quotation marks. For example:

DEBUG "Hello, World!”

This line sends the message “Hello, World!” to the computer. The message is displayed in a window called the Debug Terminal window within the Stamp Editor.

image2.jpg image3.jpg image4.jpg
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Electronics Projects: How to Use a Diode to Block Reverse Polarity
Electronics Projects: How to Build Series and Parallel Circuits
Electronics Projects: How to Build a NOT Gate Circuit
Electronics Projects: How to Build an LED Flasher Using an Oscillator
Electronics Projects: How to Build Series and Parallel Switched Circuits

Inside Dummies.com