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/WRITE.htm

244 lines
14 KiB
HTML

<?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>WRITE</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">WRITE</h1>
<div class="ImagePlusCaption">
<div class="Col2">
<p>
<img src="../../graphics/pgm_icon1.gif" border="0" alt="BS1 icon" title="BS1 icon" />
<img src="../../graphics/pgm_icon2.gif" border="0" alt="BS2 icon" title="BS2 icon" />
<img src="../../graphics/pgm_icon2e.gif" border="0" alt="BS2e icon" title="BS2e icon" />
<img src="../../graphics/pgm_icon2sx.gif" border="0" alt="BS2sx icon" title="BS2sx icon" />
<img src="../../graphics/pgm_icon2p.gif" border="0" alt="BS2p icon" title="BS2p icon" />
<img src="../../graphics/pgm_icon2pe.gif" border="0" alt="BS2pe icon" title="BS2pe icon" />
<img src="../../graphics/pgm_icon2px.gif" border="0" alt="BS2px icon" title="BS2px icon" /><span class="code_in_text"> {PBASIC&#160;2.5}</span>
</p>
</div>
<p style="text-align: right;"><a href="../ExampleTopics/WriteEx.htm" target="" title="" alt="" class="MCXref_0">WRITE Examples</a>
</p>
<p>&#160;</p>
</div>
<p class="clear">&#160;</p>
<p>&#160;</p>
<p class="PlainText">Syntax <img align="absmiddle" src="../../graphics/mini_1.gif" />:
<span class="keyword_in_text">WRITE</span> <![CDATA[ ]]><i>Location</i>,<i><![CDATA[ ]]>Value</i><br />Syntax <img align="absmiddle" src="../../graphics/mini_2_inline.gif" />:
<span class="keyword_in_text">WRITE</span> <![CDATA[ ]]><i>Location, </i>{Word}<i><![CDATA[ ]]>Value <![CDATA[ ]]></i>{, {Word}<i><![CDATA[ ]]>Value ...</i>}</p>
<h2>Function</h2>
<p class="PlainText">Write <i>Value</i> into <i>Location</i> in EEPROM. On the BS2 family, multiple
<i>Value</i> may be written to consecutive locations.‡
</p>
<ul>
<li value="1"><b><i>Location</i></b> is a variable/constant/expression* (0 - 255 on BS1,
0 - 2047 on all other BASIC Stamp modules) that specifies the EEPROM address to
write to.</li>
<li value="2"><b><i>Value</i></b> is a variable/constant/expression* (0 - 255, or 0 - 65535 if using the optional Word modifier‡) specifying the
value to be stored. </li>
</ul>
<p class="PlainText">*<img src="../../graphics/bs1note.gif" style="vertical-align: super;" /> Note: expressions are not allowed as arguments on the BS1.</p>
<p class="PlainText">‡Note: The optional arguments require PBASIC 2.5.</p>
<h2>Quick Facts</h2>
<table width="100%" cellpadding="4" cellspacing="0" border="1">
<col />
<col style="width: 214px;" />
<col />
<col />
<col />
<tr>
<td width="16%" align="center" bgcolor="#CFCFCF">&#160;</td>
<td align="center" bgcolor="#CFCFCF">BS1</td>
<td width="21%" align="center" bgcolor="#CFCFCF">BS2</td>
<td width="21%" align="center" bgcolor="#CFCFCF">BS2e and BS2sx</td>
<td width="21%" align="center" bgcolor="#CFCFCF">BS2p, BS2pe, and BS2px</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">Range of EEPROM locations</td>
<td align="center">0 to 255</td>
<td align="center">0 to 2047</td>
<td align="center">0 to 2047</td>
<td align="center">0 to 2047<br></br>(see notes below)</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">Maximum number of writes per location</td>
<td align="center">10 million</td>
<td align="center">10 million</td>
<td align="center">100,000</td>
<td align="center">100,000</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">Special Notes</td>
<td align="center">N/A</td>
<td align="center">N/A</td>
<td align="center"><span class="keyword_in_text">WRITE</span> only works with current program slot on BS2e
and BS2sx.</td>
<td align="center"><span class="keyword_in_text">WRITE</span> works with any program slot as set by the
<span class="keyword_in_text">STORE</span> command.</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">Related Commands</td>
<td align="center" colspan="1">
<p colspan="1" align="center"><a href="READ.htm" target="" title="" alt="" class="MCXref_0">READ</a>, <a href="EEPROM.htm" target="" title="" alt="" class="MCXref_0">EEPROM</a></p>
</td>
<td align="center" colspan="2">
<p colspan="2" align="center"><a href="READ.htm" target="" title="" alt="" class="MCXref_0">READ</a>, <a href="DATA.htm" target="" title="" alt="" class="MCXref_0">DATA</a></p>
</td>
<td align="center" colspan="1">
<p colspan="1" align="center"><a href="READ.htm" target="" title="" alt="" class="MCXref_0">READ</a>, <a href="DATA.htm" target="" title="" alt="" class="MCXref_0">DATA</a>, <a href="STORE.htm" target="" title="" alt="" class="MCXref_0">STORE</a></p>
</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">PBASIC 2.5 Syntax Options</td>
<td align="center">n/a</td>
<td align="center" colspan="3">Multiple sequential variables may be writtten to the Scratch Pad RAM, and the optional WORD modifier may be specified to store 16-bit values.</td>
</tr>
</table>
<h2>Explanation</h2>
<p class="PlainText">The EEPROM is used for both program storage (which builds downward from address
255 on BS1, 2047 on all other BASIC Stamp models) and data storage (which builds upward
from address 0). The <span class="keyword_in_text">WRITE</span> instruction stores a value to any EEPROM
address. Any location within the EEPROM can be written to (including your PBASIC
program's locations) at run-time. This feature is mainly used to store long-term
data to EEPROM; data stored in EEPROM is not lost when the power is removed.</p>
<p class="PlainText">The following <span class="keyword_in_text">WRITE</span> command stores the value 245 at location 100:</p><pre class="BScode" xml:space="preserve">
WRITE 100, 245
</pre>
<p class="PlainText">The EEPROM is organized as a sequential set of byte-sized memory locations.
With the BS1, the <span class="keyword_in_text">WRITE</span> instruction only stores byte-sized values into
EEPROM. This does not mean that you can't write word-sized values, however.
A word consists of two bytes, called a low-byte and a high-byte. If you wanted
to write a word-sized value, you'll need to use two <span class="keyword_in_text">WRITE</span> commands and a
word-size value or variable (along with some handy modifiers). For example:</p>
<p>
<img src="../../graphics/bs1_inline.gif" border="0">
</img>
</p><pre class="BScode" xml:space="preserve">
SYMBOL value = W1 ' word-sized variable
SYMBOL valLo = B2 ' B2 is the low-byte of W1
SYMBOL valHi = B3 ' B3 is the high-byte of W1
Main:
value = 1125
WRITE 0, valLo
WRITE 1, valHi
END
</pre>
<p>
<img src="../../graphics/bs2all_inline.gif" border="0">
</img>
</p><pre class="BScode" xml:space="preserve">
value VAR Word
Main:
value = 1125
WRITE 0, value.LowByte
WRITE 1, value.HighByte
END
</pre>
<p class="PlainText">When this program runs, the two <span class="keyword_in_text">WRITE</span> commands will store the low-byte
and high-byte of the number 1125 into EEPROM.</p>
<p class="PlainText">With the BS2 family, the Word modifier can be used to write 16-bit values.
The low byte of the value will be written to <i>Location</i>, the high byte will
be written to <i>Location + 1</i> (<i>"Little Endian"</i> ).</p>
<p>
<img src="../../graphics/bs2all_inline.gif" border="0">
</img>
</p><pre class="BScode" xml:space="preserve">
' {$PBASIC 2.5}
value VAR Word
value2 VAR Word
addr VAR Word ' EEPROM address
test VAR Byte ' test byte read back
Main:
value = $1125
value2 = $2003
WRITE 0, Word value, Word value2 ' write value at locations 0 &amp; 1
' and value2 at locations 2 &amp; 3
FOR addr = 0 TO 3 ' display "25 11 03 20"
READ addr, test
DEBUG HEX2 test, " "
NEXT
END
</pre>
<p class="PlainText">EEPROM differs from RAM, the memory in which variables are stored, in several
respects: </p>
<ol>
<li value="1">Writing to EEPROM takes more time than storing a value in a variable.
Depending on many factors, it may take several milliseconds for the EEPROM to
complete a write. RAM storage is nearly instantaneous. </li>
<li value="2">The EEPROM can only accept a finite number of write cycles per location
before it wears out. The table above indicates the guaranteed number of writes
before failure. If a program frequently writes to the same EEPROM location,
it makes sense to estimate how long it might take to exceed the guaranteed
maximum. For example, on the BS2, at one write per second (86,400 writes/day)
it would take nearly 116 days of continuous operation to exceed 10 million. </li>
<li value="3">The primary function of the EEPROM is to store programs (data is stored in
leftover space). If data overwrites a portion of your program, the program
will most likely crash. </li>
</ol>
<p class="PlainText">Check the program's memory map to determine what portion of memory your program
occupies and make sure that EEPROM writes cannot stray into this area. You may also
use the DATA directive on the BS2, BS2e, BS2sx, BS2p, and BS2pe to set aside EEPROM
space. </p>
<p>
<img src="../../graphics/bs1_inline.gif" border="0">
</img>
</p>
<p class="PlainText">On the BS1, location 255 holds the address of the last instruction in your
program. Therefore, your program can use any space below the address given in
location 255. For example, if location 255 holds the value 100, then your program
can use locations 0-99 for data.</p>
<p>
<img src="../../graphics/bs2all_inline.gif" border="0">
</img>
</p>
<p class="PlainText">On other BASIC Stamp models, you'll need to view the Memory Map of the program before
you download it, to determine the last EEPROM location used. </p>
<p>
<img src="../../graphics/bs2ppepx_inline.gif" border="0">
</img>
</p>
<p class="PlainText">On the BS2p, BS2pe and BS2px the <span class="keyword_in_text">READ</span> and <span class="keyword_in_text">WRITE</span> commands can
affect locations in any program slot as set by the <span class="keyword_in_text">STORE</span> command. See the <a href="STORE.htm" target="" title="" alt="" class="MCXref_0">STORE</a> command 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>