This repository has been archived on 2024-09-22. You can view files and clone it, but cannot push or open issues or pull requests.
pbi-ide/help/BasicStampHelp/Content/LanguageTopics/Commands/EEPROM.htm

205 lines
13 KiB
HTML
Raw Normal View History

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="6" MadCap:lastHeight="94" MadCap:lastWidth="853" MadCap:disableMasterStylesheet="true" MadCap:tocPath="" MadCap:InPreviewMode="false" MadCap:PreloadImages="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" lang="en-us" xml:lang="en-us" MadCap:PathToHelpSystem="../../../" MadCap:HelpSystemFileName="Default.xml" MadCap:SearchType="Stem">
<head><title>EEPROM</title>
<link href="../../SkinSupport/MadCap.css" rel="stylesheet" />
<link href="../../Resources/Stylesheets/BSE_Help.css" rel="stylesheet" />
<script src="../../SkinSupport/MadCapAll.js" type="text/javascript">
</script>
</head>
<body>
<h1 class="code">EEPROM</h1>
<div class="ImagePlusCaption">
<div class="Col2">
<p>
<img src="../../graphics/pgm_icon1.gif" border="0" alt="BS1 icon" title="BS1 icon" /><span class="code_in_text"> {PBASIC&#160;1.0}</span>
</p>
</div>
<p style="text-align: right;"><a href="../ExampleTopics/EepromEx.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">EEPROM Example</a>
</p>
<p>&#160;</p>
</div>
<p class="clear">&#160;</p>
<p>&#160;</p>
<p class="PlainText">Syntax: <span class="keyword_in_text">EEPROM</span> {<i>Location</i>,} (<i> DataItem
</i>{,<i> DataItem</i>,...})</p>
<h2>Function</h2>
<p class="PlainText">Write data to the EEPROM during program download.</p>
<ul>
<li value="1"><b><i>Location</i></b> is an optional variable/constant (0 - 255) that
specifies the starting location in the EEPROM at which data should be stored.
If no location is given, data is written starting at the next available
location.</li>
<li value="2"><b><i>DataItem</i></b> is a constant (0 - 255) to be stored in EEPROM.</li>
</ul>
<h2>Quick Facts</h2>
<table cellpadding="4" cellspacing="0" border="1" style="caption-side: top;width: 100%;">
<col />
<col />
<tr bgcolor="#CFCFCF">
<td width="15%" align="center">&#160;</td>
<td width="85%" align="center">BS1</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">Special Notes</td>
<td>Writes values to EEPROM during download. Can be used to decrease program size.</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">Related Commands</td>
<td>
<p colspan="2" align="center"><a href="READ.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">READ</a>, <a href="WRITE.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">WRITE</a></p>
</td>
</tr>
</table>
<h2>Explanation</h2>
<p class="PlainText">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 <span class="keyword_in_text">EEPROM</span> 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
<span class="keyword_in_text">EEPROM</span> directive is not downloaded to the BASIC Stamp 1, but the data it
contains is downloaded).</p>
<p>The simplest form of the <span class="keyword_in_text">EEPROM</span> directive is something like the following:</p><pre class="BScode" xml:space="preserve">
EEPROM (100, 200, 52, 45)
</pre>
<p class="PlainText">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
<span class="keyword_in_text">READ</span> and <span class="keyword_in_text">WRITE</span> commands in your code to access these locations and
the data you've stored there</p>
<p class="PlainText">The <span class="keyword_in_text">EEPROM</span> 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 <span class="keyword_in_text">EEPROM</span> 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 <span class="keyword_in_text">EEPROM</span> directive, subsequent <span class="keyword_in_text">EEPROM</span> directives start
with the pointer value left by the previous <span class="keyword_in_text">EEPROM</span> directive. For example,
if the program contains:</p><pre class="BScode" xml:space="preserve">
EEPROM (72, 69, 76, 76, 79)
EEPROM (104, 101, 108, 108, 111)
</pre>
<p class="PlainText">The first <span class="keyword_in_text">EEPROM</span> directive will start at location 0 and increment the
pointer for each data value it stores (1, 2, 3, 4 and 5). The second <span class="keyword_in_text">EEPROM</span>directive 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:</p>
<table width="100%" cellpadding="4" cellspacing="0" border="1">
<tr>
<td width="20%" align="center" bgcolor="#CFCFCF" rowspan="2">&#160;</td>
<td align="center" bgcolor="#CFCFCF" colspan="10">EEPROM Location (address)</td>
</tr>
<tr>
<td width="8%" align="center" bgcolor="#CFCFCF">0</td>
<td width="8%" align="center" bgcolor="#CFCFCF">1</td>
<td width="8%" align="center" bgcolor="#CFCFCF">2</td>
<td width="8%" align="center" bgcolor="#CFCFCF">3</td>
<td width="8%" align="center" bgcolor="#CFCFCF">4</td>
<td width="8%" align="center" bgcolor="#CFCFCF">5</td>
<td width="8%" align="center" bgcolor="#CFCFCF">6</td>
<td width="8%" align="center" bgcolor="#CFCFCF">7</td>
<td width="8%" align="center" bgcolor="#CFCFCF">8</td>
<td width="8%" align="center" bgcolor="#CFCFCF">9</td>
</tr>
<tr>
<td width="20%" align="center" bgcolor="#CFCFCF">Contents</td>
<td width="8%" align="center">72</td>
<td width="8%" align="center">69</td>
<td width="8%" align="center">76</td>
<td width="8%" align="center">76</td>
<td width="8%" align="center">79</td>
<td width="8%" align="center">104</td>
<td width="8%" align="center">101</td>
<td width="8%" align="center">108</td>
<td width="8%" align="center">108</td>
<td width="8%" align="center">111</td>
</tr>
</table>
<p class="PlainText">What if you don't want to store values starting at location 0? Fortunately,
the <span class="keyword_in_text">EEPROM</span> 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 <i>Location</i> argument before the list of <i>DataItems</i>. The following
code writes the same data in the table above to locations 50 through 59:</p><pre class="BScode" xml:space="preserve">
EEPROM 50, (72, 69, 76, 76, 79, 104, 101, 108, 108, 111)
</pre>
<p class="PlainText">In this example, the <i>Location</i> argument is given and tells the <span class="keyword_in_text">EEPROM</span>directive to store the following <i>DataItem</i>(s) starting at location 50. The
<i>DataItems</i> in the list are stored in their respective locations (50, 51,
52... 59).</p>
<p class="PlainText">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
<i>DataItems</i> from an <span class="keyword_in_text">EEPROM</span> directive is written with a 0. </p>
<p class="PlainText">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 <span class="keyword_in_text">EEPROM</span> directive, and PBASIC will understand
it to mean a series of bytes (see the last line of code below). The following three
<span class="keyword_in_text">EEPROM</span> directives are equivalent: </p><pre class="BScode" xml:space="preserve">
EEPROM (72, 69, 76, 76, 79)
EEPROM ("H", "E", "L", "L", "O")
EEPROM ("HELLO")
</pre>
<p class="PlainText">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.</p>
<p class="PlainText">The EEPROM is organized as a sequential set of byte-sized memory locations.
The <span class="keyword_in_text">EEPROM</span> directive only stores bytes into EEPROM. If you try to store
a word-size value, for example: <span class="keyword_in_text">EEPROM</span> (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 <span class="keyword_in_text">EEPROM</span> 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:</p><pre class="BScode" xml:space="preserve">
EEPROM (101, 4)
</pre>
<p class="PlainText">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).</p>
<p class="PlainText">To retrieve a word-size value, you'll need to use two <span class="keyword_in_text">READ</span> commands and
a word-size variable. For example,</p><pre class="BScode" xml:space="preserve">
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
</pre>
<p class="PlainText">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 <span class="keyword_in_text">READ</span>commands 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 <a href="READ.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">READ</a>
and <a href="WRITE.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">WRITE</a> commands for more
information.</p>
<div class="Col2">
<div class="MasterFoot">
<p MadCap:conditions="BSEconditions.BSEWebHelp (Primary)-INCLUDE"><a href="../../HomeTopics/HomePage.htm">Go to Welcome page</a>
</p>
</div>
</div>
<div class="Col2">
<div class="MasterFoot">
<p style="text-align: right;"><span class="ContactInfoProjectName">BASIC Stamp Help</span> <![CDATA[ ]]><span class="ContactInfoVersion#">Version 2.5.4</span> <![CDATA[ ]]></p>
<p style="text-align: right;">Copyright ©&#160;<span class="ContactInfoCompanyName">Parallax Inc.</span></p>
<p style="text-align: right;"><span class="SystemShortDate">8/8/2012</span>
</p>
</div>
</div>
<script type="text/javascript">/* <![CDATA[ */
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-285614-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
/* ]]> */</script>
<script type="text/javascript" src="../../SkinSupport/MadCapBodyEnd.js">
</script>
</body>
</html>