EEPROM

BS1 icon {PBASIC 1.0}

EEPROM Example

 

 

 

Syntax: EEPROM {Location,} ( DataItem {, DataItem,...})

Function

Write data to the EEPROM during program download.

Quick Facts

  BS1
Special Notes Writes values to EEPROM during download. Can be used to decrease program size.
Related Commands

READ, WRITE

Explanation

When you download a program into the BASIC Stamp 1, it is stored in the EEPROM starting at the highest address (255) and working towards the lowest address. Most programs don't use the entire EEPROM, so the lower portion is available for other uses. The EEPROM directive allows you to define a set of data to store in the available EEPROM locations. It is called a "directive" rather than a "command" because it performs an activity at compile-time rather than at run-time (i.e., the EEPROM directive is not downloaded to the BASIC Stamp 1, but the data it contains is downloaded).

The simplest form of the EEPROM directive is something like the following:

EEPROM  (100, 200, 52, 45)

This example, when downloaded, will cause the values 100, 200, 52 and 45 to be written to EEPROM locations 0, 1, 2 and 3, respectively. You can then use the READ and WRITE commands in your code to access these locations and the data you've stored there

The EEPROM directive uses a counter, called a pointer, to keep track of available EEPROM addresses. The value of the pointer is initially 0. When a program is downloaded, the EEPROM directive stores the first byte value at the current pointer address, then increments (adds 1 to) the pointer. If the program contains more than one EEPROM directive, subsequent EEPROM directives start with the pointer value left by the previous EEPROM directive. For example, if the program contains:

EEPROM  (72, 69, 76, 76, 79)
EEPROM  (104, 101, 108, 108, 111)

The first EEPROM directive will start at location 0 and increment the pointer for each data value it stores (1, 2, 3, 4 and 5). The second EEPROMdirective will start with the pointer value of 5 and work upward from there. As a result, the first 10 bytes of EEPROM will look like the following:

  EEPROM Location (address)
0 1 2 3 4 5 6 7 8 9
Contents 72 69 76 76 79 104 101 108 108 111

What if you don't want to store values starting at location 0? Fortunately, the EEPROM directive has an option to specify the next location to use. You can specify the next location number (to set the pointer to) by using the optional Location argument before the list of DataItems. The following code writes the same data in the table above to locations 50 through 59:

EEPROM  50, (72, 69, 76, 76, 79, 104, 101, 108, 108, 111)

In this example, the Location argument is given and tells the EEPROMdirective to store the following DataItem(s) starting at location 50. The DataItems in the list are stored in their respective locations (50, 51, 52... 59).

It is important to realize that the entire BASIC Stamp 1 EEPROM is overwritten during programming. Any EEPROM location not containing a PBASIC program or DataItems from an EEPROM directive is written with a 0.

A common use for EEPROM is to store strings; sequences of bytes representing text. PBASIC converts quoted text like "A" into the corresponding ASCII character code (65 in this case). To make data entry easier, you can place quotes around a whole chunk of text used in a EEPROM directive, and PBASIC will understand it to mean a series of bytes (see the last line of code below). The following three EEPROM directives are equivalent:

EEPROM  (72, 69, 76, 76, 79)
EEPROM  ("H", "E", "L", "L", "O")
EEPROM  ("HELLO")

All three lines of code, above, will result in the numbers 72, 69, 76, 76, and 79 being stored into EEPROM upon downloading. These numbers are simply the ASCII character codes for "H", "E", "L", "L", and "O", respectively. See the Example program above for a demonstration of storing and reading multiple text strings.

The EEPROM is organized as a sequential set of byte-sized memory locations. The EEPROM directive only stores bytes into EEPROM. If you try to store a word-size value, for example: EEPROM (1125), only the lower byte of the value will be stored (in this case, 101). This does not mean that you can't store word-sized values, however. A word consists of two bytes, called a low-byte and a high-byte. If you wanted to store the value 1125 using the EEPROM directive you'll have to calculate the low-byte and the high-byte and insert them in the list in the proper order, as in:

EEPROM  (101, 4)

The directive above will store the two bytes into two sequential EEPROM locations (the low-byte first, followed by the high-byte). We calculated this in the following manner: 1) high-byte is INT(value / 256) and 2) low-byte is value - (high-byte * 256).

To retrieve a word-size value, you'll need to use two READ commands and a word-size variable. For example,

SYMBOL  result  =  W1                   ' word-sized variable
SYMBOL  resLo   =  B2                   ' B2 is low-byte of W1
SYMBOL  resHi   =  B3                   ' B3 is high-byte of W1

EEPROM  (101, 4)

Main:
  READ 0, resLo
  READ 1, resHi
  DEBUG #result
  END

This code would write the low-byte and high-byte of the number 1125 into locations 0 and 1 during download. When the program runs, the two READcommands will read the low-byte and high-byte out of EEPROM (reconstructing it in a word-size variable) and then display the value on the screen. See the READ and WRITE commands for more information.

Go to PBASIC Language home page

Open Getting Started with Stamps in Class

Open Connection Troubleshooting

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/21/2013