Electronics Projects: Block DC while Passing AC
Electronics Projects: How to Build a Three Way Lamp Switch
Electronics Projects: How to Create a Transistor NOR Gate Circuit

Electronics Projects: How to Assign Names to I/O Pins in PBASIC Code

Programmable circuits in electronics projects require a bit of programming. If your programmable circuit is a BASIC Stamp, you’ll write that code in PBASIC. PBASIC lets you assign a name to an I/O pin. This feature makes the program a bit more self-documenting.

For example, you can use the HIGH and LOW statements to set the output status of an I/O pin. The following statement sets pin 6 to HIGH:

HIGH 6

Here, the number 6 indicates that pin 6 should be set to HIGH.

The problem with using just the pin number to identify which pin you want to control is that you can’t tell what kind of device is connected to pin 6 simply by looking at the statement. It could be an LED, but it could also be a motor or a servo or even a pneumatic valve that causes a Frankenstein creature to pop up.

To remedy this situation, PBASIC lets you assign a name to an I/O pin by placing a statement similar to this one near the beginning of your program:

Led1 PIN 0

Here, the name Led1 is assigned to pin 0. Now, you can use the name Led1 in a HIGH or LOW statement, like this:

HIGH Led1

This statement sets the I/O pin referenced by the name Led1 to HIGH.

Here is a program that uses pin names instead of the pin numbers. The real advantage of creating PIN names is that it makes it much easier to change the pin configuration of your project later on.

For example, suppose you decide that instead of connecting the six LEDs to pins 0, 2, 4, 6, 8, and 10, you want to connect them to pins 0, 1, 2, 3, 4, and 5. By using pin names, you must change the pin assignments just once when you modify the program, in the PIN statements near the beginning of the program.

' LED Flasher Program
' Doug Lowe
' July 10, 2011
'
' This program flashes LEDs connected to pins 0, 2, 4, 6, 8, and 10
' at one-half second intervals.
'
' This version of the program uses pin names instead of numbers.
' {$PBASIC 2.5}
' {$STAMP BS2}
Led1 PIN 0
Led2 PIN 2
Led3 PIN 4
Led4 PIN 6
Led5 PIN 8
Led6 PIN 10
Main:
 HIGH Led1
 HIGH Led2
 HIGH Led3
 HIGH Led4
 HIGH Led5
 HIGH Led6
 PAUSE 500
 LOW Led1
 LOW Led2
 LOW Led3
 LOW Led4
 LOW Led5
 LOW Led6
 PAUSE 500
 GOTO Main
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Electronic Project Plan Step 4: Construct Your Circuit
Digital Electronics: Types of Flip-Flop Circuits?
Electronics Projects: How to Build a Latch Circuit
Electronics Projects: Materials for a Crystal Radio Circuit
Electronics Projects: How to Write a PBASIC Program
Advertisement

Inside Dummies.com