# Memory Organization

(See also: Variables)

The BASIC Stamp has two kinds of memory; RAM (for variables used by your program) and EEPROM (for storing the program itself). EEPROM may also be used to store long-term data in much the same way that desktop computers use a hard drive to hold both programs and files.

An important distinction between RAM and EEPROM is this:

- RAM loses its contents when the BASIC Stamp loses power; when power returns, all RAM locations are cleared to 0s.
- EEPROM retains the contents of memory, with or without power, until it is overwritten (such as during the program-downloading process or with a WRITE instruction.)

## BASIC Stamp 1 RAM Organization

The BS1 has 16 bytes (8 words) of RAM space arranged as shown in the table below. The first word, called PORT, is used for I/O pin control. It consists of two bytes, PINS and DIRS. The bits within PINS correspond to each of the eight I/O pins on the BS1. Reading PINS effectively reads the I/O pins directly, returning an 8-bit set of 1's and 0's corresponding to the high and low state of the respective I/O pin at that moment. Writing to PINS will store a high or low value on the respective I/O pins (though only on pins that are set to outputs).

Word Name | Byte Names | Bit Name | Special Notes
PORT | PINS DIRS | PIN0 - PIN7 DIR0 - DIR7 | I/O pins; bit addressable I/O pins directions; bit addressable
W0 | B0 B1 | BIT0 - BIT7 BIT8 - BIT15 | Bit addressable Bit addressable
W1 | B2 B3 |  |
W2 | B4 B5 |  |
W3 | B6 B7 |  |
W4 | B8 B9 |  |
W5 | B10 B11 |  |
W6 | B12 B13 |  | Used by GOSUB instruction Used by GOSUB instruction

The second byte of PORT, DIRS, controls the direction of the I/O pins. Each bit within DIRS corresponds to an I/O pin's direction. A high bit (1) sets the corresponding I/O pin to an output direction and a low bit (0) sets the corresponding I/O pin to an input direction.

The remaining words (W0 – W6) are available for general-purpose use. Each word consists of separately addressable bytes and the first two bytes (B0 and B1) are bit addressable as well. You may assign other names (symbols) to these RAM registers as shown in section "Defining and Using Variables", below.

When the BS1 is powered up, or reset, all memory locations are cleared to 0, so all pins are inputs (DIRS = %00000000). Also, if the PBASIC program sets all the I/O pins to outputs (DIRS = %11111111), then they will initially output low, since the output latch (PINS) is cleared to all zeros upon power-up or reset, as well.

## BASIC Stamp 2 Series RAM Organization

The BS2, BS2e, BS2sx, BS2p, BS2pe, and BS2px have 32 bytes of Variable RAM space arranged as shown in the table below. Of these, the first six bytes are reserved for input, output, and direction control of the I/O pins. The remaining 26 bytes are available for general-purpose use as variables.

Note: There are 16 words, consisting of two bytes each for a total of 32 bytes. All bits are individually addressable through modifiers and the bits within the upper three words are also individually addressable through the pre-defined names shown.

Word Name | Byte Names | Nibble Names | Bit Name | Special Notes
INS | INL INH | INA, INB INC, IND | IN0 - IN7 IN8 - IN15 | Input pins
OUTS | OUTL OUTH | OUTA, OUTB OUTC, OUTD | OUT0 - OUT7 OUT8 - OUT15 | Output pins
DIRS | DIRL DIRH | DIRA, DIRB DIRC, DIRD | DIR0 - DIR7 DIR8 - DIR15 | I/O pin direction control
W0 | B0 B1 |  |  |
W1 | B2 B3 |  |  |
W2 | B4 B5 |  |  |
W3 | B6 B7 |  |  |
W4 | B8 B9 |  |  |
W5 | B10 B11 |  |  |
W6 | B12 B13 |  |  |
W7 | B14 B15 |  |  |
W8 | B16 B17 |  |  |
W9 | B18 B19 |  |  |
W10 | B20 B21 |  |  |
W11 | B22 B23 |  |  |
W12 | B24 B25 |  |  |

The INS variable always shows the state of the I/O pins themselves, regardless of the direction of each I/O pin. We call this, "reading the pins." If a pin was set to an input mode (within DIRS) and an external circuit connected the I/O pin to ground, the corresponding bit of INS would be zero. If a pin was set to be an output and the pin's state was set to a high level (within OUTS), the corresponding bit of INS would be high. If, however, that same pin was externally connected directly to ground, the corresponding bit of INS would be low; since we're reading the state of the pin itself and the BASIC Stamp cannot override a pin that is driven to ground or 5 volts externally. Note: The final example is an error; it will cause a direct short and can cause damage to the BASIC Stamp! Do not intentionally connect output pins directly to Vss (Ground) or Vdd (positive supply) or you risk destroying your BASIC Stamp module.

To summarize: DIRS determines whether a pin's state is set by external circuitry (input, 0) or by the state of OUTS (output, 1). INS always matches the actual states of the I/O pins, whether they are inputs or outputs. OUTS holds bits that will only appear on pins whose DIRS bits are set to output.

In programming the BASIC Stamp, it's often more convenient to deal with individual bytes, nibbles or bits of INS, OUTS and DIRS rather than the entire 16-bit words. PBASIC has built-in names for these elements, shown in table above.

Here's an example of what is described in the table above. The INS register is 16-bits (corresponding to I/O pins 0 through 15). The INS register consists of two bytes, called INL (the Low byte) and INH (the High byte). INL corresponds to I/O pins 0 through 7 and INH corresponds to I/O pins 8 through 15. INS can also be thought of as containing four nibbles, INA, INB, INC and IND. INA is I/O pins 0 through 3, INB is I/O pins 4 through 7, etc. In addition, each of the bits of INS can be accessed directly using the names IN0, IN1, IN2... IN15.

The same naming scheme holds true for the OUTS and DIRS variables as well.

As the table shows, the BASIC Stamp's memory is organized into 16 words of 16 bits each. The first three words are used for I/O. The remaining 13 words are available for use as general-purpose variables.

Just like the I/O variables, the general-purpose variables have predefined names: W0 through W12 and B0 through B25. B0 is the low byte of W0; B1 is the high byte of W0; and so on through W12 (B24=low byte, B25=high byte). Unlike I/O variables, there's no reason that your program variables have to be stuck in a specific position in the BASIC Stamp's physical memory. A byte is a byte regardless of its location. And if a program uses a mixture of variables of different sizes, it can be a pain in the neck to logically dole them out or allocate storage.

More importantly, mixing fixed variables with automatically allocated variables is an invitation to bugs. A fixed variable can overlap an allocated variable, causing data meant for one variable to show up in another! The fixed variable names (of the general-purpose variables) are only provided for power users who require absolute access to a specific location in RAM.

We recommend that you avoid using the fixed variables in most situations. Instead, let PBASIC allocate variables as described in the next section. The editor software will organize your storage requirements to make optimal use of the available memory.

## Scratch Pad RAM

The BS2e, BS2sx, BS2p, BS2pe, and BS2px have some additional RAM called Scratch Pad RAM. The BS2e and BS2sx have are 64 bytes of Scratch Pad RAM (0 – 63), and the BS2p, BS2pe, and BS2px have 128 bytes of Scratch Pad RAM (0 – 127). Scratch Pad RAM can only be accessed with the GET and PUT commands (see the GET and PUT command descriptions for more information) and cannot have variable names assigned to it.

The highest location in Scratch Pad RAM (location 63 on the BS2e and BS2sx, location 127 on the BS2p, BS2pe, and BS2px) is read-only, and always contains the number of the currently running program slot. This can be handy for programs that need to know which program slot they exist in.

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012