Subtotal: 0
View Cart

BASIC Stamp Programming Information

How do I program the BASIC Stamp microcontroller?
You can write your PBASIC program using the BASIC Stamp 1, 2, 2e, 2sx, 2p, and 2pe editor on a standard IBM compatible PC. Currently there are BASIC Stamp editors for DOS, Windows, and Macintosh.  After you write the code for your application, you simply connect the BASIC Stamp microcontroller to the computer’s parallel port (BASIC Stamp 1 module) or serial port (Basic Stamp 2, 2e, 2sx, 2p, and 2pe modules), provide power to the BASIC Stamp microcontroller and press ALT-R (DOS version) or CTRL-R (Windows version) within the appropriate BASIC Stamp editor to download your program into the BASIC Stamp module’s EEPROM. As soon as the program has been downloaded successfully, it begins executing its new program from the first line of code.

How do I program the Javelin Stamp module?

You can write your Javelin program using the Javelin IDE on a standard IBM compatible PC in Windows only. Windows 95 is the minimum OS that can be used to program the Javelin Stamp modules.  After you write the code for your application, you simply connect the Javelin Stamp microcontroller to the computer’s serial port, provide power to the Javelin Stamp module and press CTRL-R  within the Javelin Stamp IDE to download your program into the Javelin Stamp module’s EEPROM. As soon as the program has been downloaded successfully, it begins executing its new program from the first line of code.

How do I connect the Stamp to my computer for Programming?
The Basic Stamp 1, Rev. D, or BS1-IC modules require a three-wire connection to any available parallel port on your PC. The Basic Stamp 2, 2e, 2sx, 2p, 2pe and Javelin Stamp modules require a standard, straight-through, serial cable connection to a 9-pin or 25-pin serial port on your PC. Only 6 wires are used for programming the Basic Stamp 2, 2e, 2sx, 2p, 2pe modules, and for the Javelin Stamp module: 4, of which are absolutely required (RX, TX, DTR, GND). You cannot use a null-modem cable to program the Stamps. Refer to the schematics in our Stamp Manuals for the details of the cable connections.

How does my program get stored in the Stamp?
Your code is tokenized (compressed) and stored within the Stamp’s EEPROM memory. This memory is non-volatile, meaning it retains its program even without power. Every time the Stamp receives power, it starts running its code starting with the first executable line. This EEPROM can be rewritten immediately, without any lengthy erase cycle or procedure, by simply re-downloading the code from the Stamp editor again. Each location in the EEPROM is guaranteed for 10,000,000 write cycles before it wears out for the BS 1 and BS 2.  The BS 2e, BS 2sx, BS 2p, and BS 2pe EEPROM is guaranteed for 100,000 write cycles before it wears out.  The Javelin EEPROM is guaranteed for 1,000,000 write cycles before it wears out. Source code elements like comments, and constant and variable names, are not stored in the Stamp, thus you may feel free to use as many comments and descriptive symbol names in your code as you like without worrying about increasing your code size.

How do I erase the BASIC Stamp module's program space?

An erase cycle is performed at the beginning of every programming process (in the BASIC Stamp 1 module) or during the programming process (in the BASIC Stamp 2, 2e, 2sx, 2p, and 2pe modules), thus, by downloading from the BASIC Stamp editor the necessary areas of the EEPROM are erased and then reprogrammed with the current PBASIC program in the editor. The BASIC Stamp 2, 2e, 2sx, 2p, and 2pe modules only erase data in 16-byte increments. It is possible to completely erase the EEPROM by downloading the following line of code (in a program by itself):

DATA 0(2048)

How do I erase the Javelin Stamp module's program space?

There is no single command for erasing the code from the Javelin Stamp module’s EEPROM.  It is possible to write a simple program that can clear the EEPROM.

How do I reprogram the Stamp?
Simply re-connect it to the computer, run the BASIC Stamp editor and press ALT-R (DOS software) or CTRL-R (Windows software) using a code.  For the Javelin Stamp module, simply re-connect it to the computer, run the Javelin Stamp IDE and press CTRL-R using a code

How big of a program can I store in the BASIC Stamp module?
The BASIC Stamp 1 module has 256 bytes of program storage; enough for 80 to 100 lines of PBASIC1 code. The BASIC Stamp 2 module has 2048 bytes of program storage; enough for 500 to 600 lines of PBASIC2 code. The Stamps 2e, 2sx, and 2p each have 16,384 bytes of program storage separated into 8 pages of 2048 bytes. Each page (or code section) can hold 500 to 600 lines of PBASIC code, respectively, for an overall total of approx. 4000 instructions. The BS2pe has 16384 bytes of program space and 16384 byte of dedicated data storage space, unused program space can be used for data storage as well. Each page (or code section) can hold 500 to 600 lines of PBASIC code, respectively, for an overall total of approx. 4000 instructions. The Javelin Stamp microcontroller has 32768 bytes of program space which can be used for program variable storage.

Can I expand the program memory?
No, the program memory is not expandable as the interpreter chip expects the memory to be a specific, fixed size.

Can I expand the data memory?
Yes, you may interface external EEPROMs, or other memory devices to the Stamp’s I/O pins to gain more data storage area. You will have to include the appropriate code within your program to interface to the particular device you choose.

How difficult is it to program the BASIC Stamp microcontroller?
When compared to other programmable microcontrollers, the BASIC Stamp microcontrollers are perhaps the easiest to use because of their simple, but powerful, language structure and straightforward method of downloading and debugging. If you have some experience programming in BASIC, C or Pascal, you should find the learning process with the BASIC Stamp module to be a simple one. If, however, you have never had any programming experience, you may have to spend some extra time studying the examples and application notes before you feel comfortable with the BASIC Stamp module.

How difficult is it to program the Javelin Stamp module?
This will depend on how well you know Java. If you are familiar with Java or “C” you will find the Javelin Stamp module to be enjoyable to program. On the other hand, if you have never programmed in a structured language it could be a very difficult transition.

Can I program the BASIC Stamp microcontroller in Visual BASIC or QBASIC?
No. The Stamp is a microcontroller, not a miniature PC. It does not have a graphical user interface, a hard drive or a lot of RAM. The BASIC Stamp module must only be programmed with PBASIC, and the Javelin Stamp module with the Java-like language, which has been specifically designed to exploit all of the stamp’s capabilities.

What is PBASIC?
PBASIC (Parallax BASIC) is a hybrid form of the BASIC programming language in which many people are familiar. Currently there are three versions of PBASIC: PBASIC1 for the BASIC Stamp 1 module, PBASIC2 for the BASIC Stamp 2 module and PBASIC2sx for the BASIC Stamp 2sx module. Each version is specifically tailored to take advantage of the inherent features of the hardware it runs on. PBASIC is called a hybrid because, while it contains some simplified forms of standard BASIC commands, it also has special commands to efficiently control the I/O pins.

Can I embed assembly language routines in my PBASIC code?
No. The BASIC Stamp microcontroller is a PBASIC interpreter only and cannot accept assembly language routines.

How much space does each PBASIC command take?
Each command takes a variable amount of space within the BASIC Stamp module’s EEPROM. It varies due to complexities of the command itself and the types of arguments you supply each command. Generally, each command takes 2 to 4 bytes of memory space, however, commands like SERIN, SEROUT, LOOKUP and LOOKDOWN, which accept a variable length list of arguments, may take tens of bytes or more.

How do I know how much space my PBASIC program consumes?
On the BASIC Stamp 1 module, enter the following code at the start of your PBASIC1 program: READ 255,B0
DEBUG #B0
Upon running the program, a number will display in the debug window of the editor. Use the following equation to determine how many bytes are used by your PBASIC1 code: 255 - # - 6; where # is the number displayed on the debug window. Note, the “- 6” in the equation results from the fact that the above two lines of code take 6 bytes of program space, thus without those two lines, your program takes 6 fewer bytes of space. On the BASIC Stamp 2, 2e or 2sx modules from within the editor and with your PBASIC program on the screen, press ALT-M (DOS software) or CTRL-M (Windows software) to view the memory map screens. In the DOS software, the first screen shows your RAM space, the second screen shows an expanded view of the program (EEPROM) space and the third screen shows a detailed view of the program space. Press the spacebar to alternate between the three screens. In the Windows software, all EEPROM and RAM is shown on one window.

Why doesn't the memory map feature show me data I have already stored in the BASIC Stamp 2, 2e or 2sx module's EEPROM?
The memory map feature (ALT-M, DOS software or CTRL-M, Windows software) shows only what the memory space will look like immediately after you program the Stamp 2, 2e or 2sx. The editor determines this by examining your PBASIC source code within the editor, not the PBASIC code in the Stamp. It does not read the BASIC Stamp module’s EEPROM. It will only display data in the EEPROM that you’ve explicitly defined with DATA commands; no data which is operated upon by READ and WRITE commands can be shown since those are run-time, rather than compile-time, functions.

How do I debug my PBASIC programs?

You may use two features of the BASIC Stamp editor to debug your PBASIC program: 1) Syntax checking, and 2) the DEBUG command. Syntax checking alerts you to any syntactical errors and is automatically performed on your code the moment you try to download to the BASIC Stamp microcontroller. Any syntax errors will cause the download process to abort and the editor will display an error message with a highlight cursor pointing to the source of the error in your code. Logical errors are sometimes more difficult to find but may be more easily discovered by using the DEBUG command. Debug operates similar to the PRINT command in the BASIC language and can be used to print the current status of specific variables within your PBASIC program as it is executed within the BASIC stamp microcontroller. If your PBASIC code includes a DEBUG command, the editor opens up a special window at the end of the download process to display the results for you. In the Windows editor, the Debug window (called the Debug Terminal) can be left open, while you edit your code.

How do I print out my PBASIC programs?

The BASIC Stamp DOS editors do not have a print option built-in, however, the source file created by the editor is simply a DOS text file. This means it can be printed with the standard PRINT or TYPE commands as in: PRINT source.bas      -or- TYPE source.bas > LPT1 where source.bas is the name of your PBASIC program. The Windows software features built-in printing support.

Can I read-out, or upload, the PBASIC program which is already stored in the BASIC Stamp microcontroller?
No. For security reasons, this feature was not made available. It is possible to read the tokenized form of the PBASIC code out of the BASIC Stamp module’s EEPROM, but there is no easy way to “reassemble” it into usable source code.

Since the BASIC Stamp 2, 2e and 2sx modules use the serial port for progamming, can I use a simple terminal program to download my PBASIC code?
No. The download process requires a very fast, bi-directional communication between the BASIC Stamp module and PC to program and verify the contents of the BASIC Stamp microcontroller’s memory.

Will a program and circuitry designed for the BASIC Stamp 1 module work with the BASIC Stamp 2, 2e or 2sx modules?
Yes, however, some changes, usually in the PBASIC code only, will have to be made for it to run properly or to take advantage of some of the new features of the BASIC Stamp 2, 2e or 2sx modules. For help in performing these modifications or for insight into how the PBASIC1 language differs from PBASIC2, review Appendix C in the BASIC Stamp Manual.

How do I make my PBASIC program start over or continue running forever?
All BASIC programs will simply end if one or more of their executable paths don’t lead them to a continuous loop. In the simplest case, a program can be made to continue endlessly by adding a label (a unique name followed by a colon ‘:’) to the start of code and adding a GOTO statement at the end of code directing logical execution back to that label. Without this, the BASIC Stamp module will run the program only once and then will remain unresponsive until the power is cycled or a reset condition is created.

How do I set an I/O pin to input or output mode?
There are many ways to set the I/O pin directions. The most readable and intuitive method is to use the INPUT and OUTPUT commands. For example: INPUT 5
OUTPUT 6
will set I/O pin 5 to an input and I/O pin 6 to an output. By default, upon reset or startup, all I/O pins are inputs, thus only the second line of code above is really necessary.

Another method for setting the pin directions is to use the predefined variable DIRS or any one of its derivatives. The following code is functionally equivalent to the first example: DIR5 = 0
DIR6 = 1
DIR0 through DIR15 (or DIR7 on the BASIC Stamp 1 module) are bit variables which control the direction of the corresponding I/O pin. DIRS is a 16-bit variable (or 8-bit variable on the BASIC Stamp 1 microcontroller) in which each bit represents the direction of the corresponding I/O pin. A ‘1’ bit means output and a ‘0’ bit means input. The advantage of this method of declaring pin directions is that multiple pin directions can be defined at once: DIRS = %00101110 The above line of code defines I/O pins 7, 6, 4 and 0 as inputs and 5, 3, 2 and 1 as outputs. Review the BASIC Stamp manual for more examples of this.

How do I make an I/O pin output a high or a low?

As with setting I/O pin directions, there are many ways to set the I/O pin states. The most readable and intuitive method is to use the HIGH and LOW commands. For example: HIGH 5
LOW 6
will first set I/O pin 5 to output mode and then set it to a high (+5V) state. The second line will first set I/O pin 6 to output mode and then set it to a low (0V) state. Notice that these commands automatically set the pin direction for you; it’s not necessary to manually set the pins to output when using the HIGH and LOW commands. Keep in mind that if a pin is set to input, its logical state, high or low, remains the same internally within the BASIC Stamp microcontroller but will no longer affect the voltage on the I/O pin.

Another method for setting the pin state is to use the predefined variable OUTS (or PINS in the BASIC Stamp 1 microcontroller) or any one of its derivatives. The following code is functionally equivalent to the first example: DIR5 = 1
DIR6 = 1
OUT5 = 1
OUT6 = 0
Notice here that we had to explicitly define the I/O pins as outputs first. If we had left the pins as inputs, the OUT5 and OUT6 lines would have changed the state of the pins internally, but those changes would not appear externally from the BASIC Stamp module. OUT0 through OUT15 (or PIN0 through PIN7 on the BASIC Stamp 1 module) are bit variables which control the logical state of the corresponding I/O pin. OUTS is a 16-bit variable (PINS is an 8-bit variable on the BASIC Stamp 1 module) in which each bit represents the state of the corresponding I/O pin. A ‘1’ bit means high and a ‘0’ bit means low. The advantage of this method of declaring pin states is that multiple pin states can be defined at once: DIRS = %00101110
OUTS = %00001010
The above line of code defines I/O pins 7, 6, 4 and 0 as inputs and 5, 3, 2 and 1 as outputs. And sets I/O pins 7, 6, 5, 4, 2 and 0 to logical 0’s and I/O pins 3 and 1 to logical 1’s. Only pins 5, 3, 2 and 1 will output the voltage set in the OUTS variable because they are the only pins defined as outputs by the DIRS variable. Review the BASIC Stamp manual for more examples of this.

How do I input or output data in parallel on the I/O pins?
All BASIC Stamp microcontrollers have predefined variables that refer to the I/O pins. On the BASIC Stamp 1 module, the PINS variable refers to all eight I/O pins. Thus code such as: SYMBOL Snapshot = B0
Snapshot = PINS
would copy the current state of all I/O pins (an 8-bit byte) into the variable Snapshot. Similarly: DIRS %11111111     ‘Set all eight pins as outputs
PINS = 150
would set the eight output pins to the binary form of 150, or %10010110. On the BASIC Stamp 2, 2e and 2sx modules the PINS variable was replaced with the INS and OUTS variables. Both INS and OUTS refer to all sixteen pins, while their derivatives, INL, INH, OUTL and OUTH refer to the low and high bytes (groups of 8 pins). For example, the following code will input 8-bits in parallel from the lower 8 pins: Snapshot   VAR  BYTE
Snapshot = INL
The following code will output 8-bits in parallel from the upper 8 pins: DIRS = %1111111100000000     ‘Set upper 8 pins to outputs
OUTH = 255


Why do the BASIC Stamp microcontroller's output pins toggle briefly when I use the SLEEP, NAP or END commands or allow my program to end naturally?

Inside the BASIC Stamp module’s interpreter chip is a watchdog timer whose main purpose is to reset the interpreter chip if, for some reason, it should stop functioning properly. The SLEEP and NAP commands also utilize the watchdog timer to periodically (every 2.3 seconds to be exact) “wake-up” the BASIC Stamp microcontroller from its low-power mode. Upon reset, the I/O pins are set to inputs for approximately 18 mS before returning to their previous directions and states. If you have an output pin set to a logical 1 state (+5V) and you use the SLEEP command, every 2.3 seconds during sleep mode that I/O pin will switch to an input for 18 mS causing a momentary signal loss. This “power glitch” is easily viewable with an LED and a 470 ohm resister tied to an I/O pin and switched on just before entering sleep mode. In many cases this problem can be remedied by tying a pull-up or pull-down resistor to the I/O pin in question to provide a constant source of power should the I/O pin change directions. Allowing a PBASIC program to end naturally, or using the END command, will exhibit the same “power glitch” behavior because the interpreter chip enters a low-power state. The BASIC Stamp 2, 2e, 2sx, 2p24, 2p40, and the 2pe modules include the STOP command which acts just like END, except output pins remain driven.

The LET command is not available on the BASIC Stamp 2, 2e and 2sx, 2p24 and 2p40 modules. Does this mean these BASIC Stamp modules cannot perform mathematical operations?
No. The LET command on the BASIC Stamp 1 module was optional; i.e.: you could leave it out of the variable assignment statement or equation and it would work normally. To save some space in the interpreter code of the BASIC Stamp 2, 2e, 2sx, 2p24, 2p40, and the 2pe modules the LET command was removed completely, however, the mathematical ability was not removed and, in fact, it is greatly improved on the BASIC Stamp 2, 2e, 2sx, 2p24, 2p40, and the 2pe modules.

Can he BASIC Stamp microcontroller store data, such as temperature readings, for review or use at a later time?
Yes. The READ and WRITE commands allow the BASIC Stamp microcontroller to manipulate memory locations in its EEPROM. Any space in the built-in EEPROM that is not occupied by PBASIC code may be used for data storage and is just as non-volatile as the PBASIC code itself. You may also attach external EEPROMs of various sizes to gain an even greater data storage area. The READ and WRITE commands will not function on external EEPROMs so you must write the appropriate code to interface with the device.

How can I store a word value into the internal EEPROM?
The internal EEPROM of the BASIC Stamp module (used for both program and data storage) is organized and accessed in units of bytes only. Since a word value is actually two bytes it requires two consecutive memory locations to store it in EEPROM. You must break the word value into its lower and upper byte parts and use two WRITE commands to store the value, or two READ commands to retrieve it.

The following code demonstrates this for the BASIC Stamp 1 module: W0 = 1250
WRITE 0, B0
WRITE 1, B1
The following code demonstrates this for the BASIC Stamp 2, 2e,2sx, 2p24, 2p40, and the 2pe modules: Temp VAR WORD
Temp = 1250
WRITE 0, Temp.LOWBYTE
WRITE 1, Temp.HIGHBYTE


How does the fpin (flow control pin) work in the SERIN and SEROUT commands on the 2, 2e, 2sx, 2p24, 2p40, and the 2pe?
The optional fpin argument in the SERIN and SEROUT commands allows the use of an I/O pin as a hardware flow control line between two BASIC Stamp 2, 2e and 2sx module’s at baud rates of up to 19.2 kBps. The flow control pin is always controlled by the receiving device (SERIN) and monitored by the sending device (SEROUT). The logical state of this pin and its meaning depend on the data mode, true or inverted, as specified within the baudmode parameter. If true data mode is selected, the receiving BASIC Stamp 2, 2e, 2sx, 2p24, 2p40, and the 2pe modules hold the fpin low to indicate it is ready to receive or high to indicate it is not ready to receive. If inverted data mode is selected, the receiving BASIC Stamp 2, 2e, 2sx, 2p24, 2p40, and the 2pe modules hold the fpin high to indicate it is ready to receive or low to indicate it is not ready to receive. Assuming true data mode: the fpin is set to output high the moment the SERIN command is first encountered and remains high until the SERIN command is complete at which point the fpin is set to low and remains there until the SERIN command is executed again. The transmitting BASIC Stamp 2, 2e or 2sx module’s SEROUT command monitors this line and halts transmission whenever a low on the fpin is detected.

How are arithmetic expressions evaluated within the BASIC Stamp microcontroller?
All expressions are evaluated using 16-bit, integer arithmetic. Even if byte and bit variables are used in expressions they are first expanded to 16-bits and then the expression is evaluated. In the BASIC Stamp 1 module, mathematical expressions are evaluated strictly from left to right. No order-of-precedence is utilized, parentheses are not allowed and expressions must be on a line by themselves; i.e.: they cannot be used as an argument for a command. For example, the expression: W0 = 10 - 2 * 3 results in the value 24 being stored into W0 (not the value 4 as you might think). To evaluate this expression properly, specify it as: W0 = 0 - 2 * 3 + 10. In the BASIC Stamp 2, 2e, 2sx, 2p, and 2pe modules mathematical expressions are evaluated from left to right, as in the BASIC Stamp 1 module. No order-of-precedence is utilized, however, unlike the BASIC Stamp 1 module, parentheses are allowed and expressions may be used as arguments within commands. For example, the expression: Answer = 10 - 2 * 3 results in the value 24 being stored into Answer, however: Answer = 10 - (2 * 3) results in the value 4.

Does the BASIC Stamp module handle signed numbers and arithmetic?
Yes. The BASIC Stamp microcontroller uses twos-compliment notation for signed numbers. This means that the expression: 0 - 10 + 5 will result in -5 if viewed as a signed number, however, most instructions see the number as a positive value, in this case 65531 (the twos-compliment value for -5). All mathematical operators, except division, will work properly with signed numbers in the BASIC Stamp and signed numbers can be formatted for output properly using the SDEC, SHEX and SBIN formatters within DEBUG and SEROUT statements on the BASIC Stamp 2, 2e, 2sx, 2p, and 2pe modules. Be careful how you use signed numbers elsewhere. For example, if the value -5 is stored in a variable called Temp, and you use the following statement: IF Temp < 0 THEN Loop it will evaluate to false and will not branch to Loop because -5 is actually 65531 in twos-compliment form and thus 65531 is not less than 0.

How can I define an alias to an I/O pin or another variable?
In the BASIC Stamp 1 module you could specify the following: SYMBOL Counter = B0
SYMBOL Index = B0
SYMBOL LED = PIN0
To designate the symbol Index as an alias to the Counter variable and the symbol LED as an alias to I/O pin 0. Since Counter and Index use the same register, B0, anytime Counter is changed, Index will change in the same way. The symbol LED will always contain either a 1 or a 0 depending on the logical state of I/O pin 0. In the BASIC Stamp 2, 2e or 2sx modules you could specify the following: Counter VAR BYTE
Index VAR Counter
LED VAR IN0
to designate the symbol Index as an alias to the Counter variable and the symbol LED as an alias to I/O pin 0. Since Index uses the same memory space as Counter, anytime Counter is changed, Index will change in the same way. The symbol LED will always contain either a 1 or a 0 depending on the logical state of I/O pin 0.

How do I reference a specific bit within a byte or word variable?
On the BASIC Stamp 1 module there is only one general purpose word register, W0, and two general purpose byte registers, B0 and B1, which are bit addressable. The predefined symbols Bit0 through Bit15 refer to the corresponding bits within W0 as well as B0 and B1 since those two byte variables correspond to the lower and upper bytes of W0 respectively. Thus Bit0 is the lowest bit of both W0 and B0 while Bit8 is the lowest bit of B1 (and the 9th bit of W0). On the BASIC Stamp 2, 2e, 2sx, 2p, and 2pe modules all the variables are bit addressable, as well as nibble addressable. You can reference a specific part of a variable by specifying the variable name followed by a dot ‘.’ and then the name of the portion you’re interested in. For example, bit 2 of a word variable called Temp can be referenced with the notation: Temp.BIT2. Additionally, the second nibble of Temp can be referenced with: Temp.NIB1. The valid modifiers are: BIT0 through BIT15, LOWBIT, HIGHBIT, and NIB0 through NIB3, LOWNIB, HIGHNIB, and BYTE0 through BYTE3, LOWBYTE and HIGHBYTE.

How do I define a string variable?
A string variable is simply an array of bytes. Only the BASIC Stamp 2, 2e, 2sx, 2p, and 2pe modules allow the explicit definition of arrays. If, for example, you want to define a string variable called Text to hold 6 characters, use the following code:
Text    VAR BYTE(6) The elements of an array can be accessed by specifying the array name followed by an open parenthesis, an index value and a close parenthesis. The first element is always index 0. For example, the second element, or character in this case, can be referenced with: Text (1) and the last element with: Text (5). Byte arrays are useful for receiving a string of characters via the SERIN command using the STR modifier. The BASIC Stamp 2, 2e, 2sx, 2p, and 2pe modules allow you to define arrays of bits, nibbles and words as well.

How do I define aliases to specific bytes within a word array on the BASIC Stamp 2, 2e and 2sx modules?
The BASIC Stamp 2, 2e and 2sx modules use a very relaxed and flexible indexing scheme when it comes to arrays of RAM space. When trying to define aliases in this way it is best to view the entire RAM space as one array of word, byte, nibble and bit registers. The BASIC Stamp 2, 2e, and 2sx modules organize variables in the RAM space from largest entities (word declarations) to smallest entities (bit declarations), regardless of the order you define them, to be most efficient. The relaxed nature of indexing allows a user to cross array boundaries at will, or by accident, quite easily. For example, the following code defines two distinct byte variables: Result    VAR BYTE
Temp      VAR BYTE
Normally you would reference the Result and Temp variables by name, as in: Result = 10. Even though this variable is not an array, you can still access it as such. The code: Result(0) = 10 is equivalent to the previous example. The most interesting and powerful feature is that you can index beyond the boundaries of this byte variable, as in Result(1) = 20. This statement actually modifies the byte of RAM immediately following the Result variable. The next byte happens to have been defined as Temp, in this case, thus the variable Temp will now equal 20. By carefully defining variables we can take advantage of this flexible feature to define byte sized aliases to specific locations within a word array. Suppose we have the need for an array of three words in which the first three bytes have a special meaning; we’d like to be able to easily manipulate those bytes as well as access the three distinct word elements.

Array     VAR WORD
Special1  VAR Array.LOWBYTE
Special2  VAR Array.HIGHBYTE
Special3  VAR BYTE
TheRest   VAR BYTE(3)

Remember: the BASIC Stamp 2, 2e and 2sx modules will always organize RAM space from biggest elements to smallest. In the example above we defined a single word variable called array this becomes assigned to the very first word in RAM space, word 0). The next two definitions create aliases to the first and second bytes of Array (no additional RAM space is allocated here since we’re pointing at previously allocated space, byte 0 and byte
1). Next we define a single byte variable (this becomes assigned to the first byte of RAM immediately following the Array variable, byte
2). Finally, we define an array of three bytes (which are assigned to the next three bytes of RAM immediately following the previous byte declaration, byte 3, byte 4 and byte 5).

Now, Array(0), Array(1) and Array(2) refer to the three words of our array and Special1, Special2 and Special3 refer to the first three bytes within our three byte array. BASIC Stamp Specifications (click to see chart)
Home    |    Contact Us    |    Job Opportunities    |    About Parallax    |    Privacy Statement    |    Terms Of Use    |    Copyright 2008 by Parallax, Inc.