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

327 lines
18 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>I2COUT</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">I2COUT</h1>
<div class="ImagePlusCaption">
<div class="Col2">
<p>
<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" />
</p>
</div>
<p style="text-align: right;"><a href="../ExampleTopics/I2cInOutEx.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">I2CIN / I2COUT Example</a>
</p>
<p>&#160;</p>
</div>
<p class="clear">&#160;</p>
<p>&#160;</p>
<p class="PlainText">Syntax: <span class="keyword_in_text">I2COUT</span> <![CDATA[ ]]><i>Pin</i>,<i> SlaveID</i>,<i><![CDATA[ ]]></i>{<i>Address </i>{\<i>LowAddress</i>},} [<i>OutputData</i>]</p>
<h2>Function</h2>
<p class="PlainText">Send data to a device using the I<sup>2</sup>C<sup>®</sup> protocol.
</p>
<ul>
<li value="1"><b><i>Pin</i></b> is a variable/constant/expression (0 or 8) that specifies
which I/O pins to use. I<sup>2</sup>C devices require two I/O pins to communicate.
The <i>Pin</i> argument serves a double purpose; specifying the first pin (for
connection to the device's SDA pin) and, indirectly, the other required pin (for
connection to the device's SCL pin). See explanation below. Both I/O pins will be
toggled between output and input mode during the <span class="keyword_in_text">I2COUT</span> command and both
will be set to input mode by the end of the <span class="keyword_in_text">I2COUT</span> command.</li>
<li value="2"><b><i>SlaveID</i></b> is a variable/constant/expression (0 - 255) indicating
the unique ID of the I<sup>2</sup>C device.</li>
<li value="3"><b><i>Address</i></b> is a variable/constant/expression (0 - 255) indicating
the desired address within the I<sup>2</sup>C device to send data to. The
<i>Address</i> argument may be used with the optional <i>LowAddress</i> argument
to indicate a word-sized address value. Note that some devices like the
PCF8574 do not require an internal address, so this parameter is optional (BS2p
firmware 1.3 or later is required to omit the <i>Address</i> parameter).</li>
<li value="4"><b><i>LowAddress</i></b> is a variable/constant/expression (0 - 255) indicating
the low-byte of the word-sized address within the I<sup>2</sup>C device to send
data to. This argument must be used along with the <i>Address</i> argument.</li>
<li value="5"><b><i>OutputData</i></b> is a list of variables, constants, expressions and
formatters that tells <span class="keyword_in_text">I2COUT</span> how to format outgoing data. <span class="keyword_in_text">I2COUT</span>can transmit individual or repeating bytes, convert values into decimal,
hexadecimal or binary text representations, or transmit strings of bytes from
variable arrays. These actions can be combined in any order in the
<i>OutputData</i> list.</li>
</ul>
<h2>Quick Facts</h2>
<table width="100%" cellpadding="4" cellspacing="0" border="1">
<tr bgcolor="#CFCFCF">
<td align="center">&#160;</td>
<td align="center" colspan="2">BS2p, BS2pe, and BS2px</td>
</tr>
<tr>
<td width="250" align="center" bgcolor="#CFCFCF">Value of <i>Pin</i></td>
<td width="40%" align="center">0</td>
<td width="40%" align="center">8</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">I/O Pin Arrangement</td>
<td>0: Serial Data (SDA) pin<br></br>1: Serial Clock (SCL) pin</td>
<td>8: Serial Data (SDA) pin<br></br>9: Serial Clock (SCL) pin</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">Transmission Rate</td>
<td colspan="2">Approximately 81 kBits/sec (BS2p), 45 kBits/sec (BS2pe),
83 kBits/sec (BS2px) -- not including overhead.</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">Special Notes</td>
<td colspan="2">Both the SDA and SCL pins must have 4.7 kΩ pull-up
resistors.</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">Related Commands</td>
<td align="center" colspan="2">
<p colspan="2" align="center"><a href="I2CIN.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">I2CIN</a>
</p>
</td>
</tr>
</table>
<h2>Explanation</h2>
<p class="PlainText">The I<sup>2</sup>C protocol is a form of synchronous serial communication developed
by Philips Semiconductor. It only requires two I/O pins and both pins can be
shared between multiple I<sup>2</sup>C devices. The <span class="keyword_in_text">I2COUT</span> command allows
the BASIC Stamp to send data to an I<sup>2</sup>C device.</p>
<p class="PlainText">The following is an example of the <span class="keyword_in_text">I2COUT</span> command:</p><pre class="BScode" xml:space="preserve"> I2COUT 0, $A0, 5, [100]</pre>
<p class="PlainText">This code will transmit a "write" command to an I<sup>2</sup>C device (connected
to I/O pins 0 and 1), followed by an address of 5 and finally will transmit the
number 100.</p>
<p class="PlainText">The example above will write a byte of data to location 5 of a 24LC16B EEPROM from
Microchip. The figure below shows the proper wiring for this example to work. The
<i>SlaveID</i> argument ($A0) is both the ID of the chip and the command to write to
the chip; the 0 means write. The <i>Address</i> argument (5) is the EEPROM location
to write to.</p>
<p class="PlainText"><b>Note</b>: The 4.7 kΩ resisters are required for the <span class="keyword_in_text">I2COUT</span>
command to function properly.</p>
<center>
<img src="../../graphics/24lc16_sch.gif" border="0">
</img>
</center>
<p class="PlainText">The <span class="keyword_in_text">I2COUT</span> command's <i>OutputData</i> argument is similar to the
<span class="keyword_in_text">DEBUG</span> and <span class="keyword_in_text">SEROUT</span> command's <i>OutputData</i> argument. This means
data can be sent as literal text, ASCII character values, repetitive values,
decimal, hexadecimal and binary translations and string data as in the examples
below. (Assume the 24LC16B EEPROM is being used).</p><pre class="BScode" xml:space="preserve">
value VAR Byte
Setup:
value = 65
Main:
I2COUT 0, $A0, 0, [value] ' send ASCII character "A"
I2COUT 0, $A0, 0, [REP value\5] ' send "A" five times, i.e., "AAAAA"
I2COUT 0, $A0, 0, [DEC value] ' send "6" and "5"
I2COUT 0, $A0, 0, [HEX value] ' send "4" and "1"
I2COUT 0, $A0, 0, [BIN value] ' send "1000001"
END
</pre>
<p class="PlainText">The tables below list all the available conversion formatters and special
formatters available to the <span class="keyword_in_text">I2COUT</span> command. See the <a href="DEBUG.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">DEBUG</a> and <a href="SEROUT.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">SEROUT</a> commands for
additional information and examples of their use.</p>
<table width="100%" cellpadding="4" cellspacing="0" border="1">
<tr bgcolor="#CFCFCF" align="center" valign="top">
<td width="20%">Conversion Formatter</td>
<td width="70%">Type of Number</td>
<td width="10%">Notes</td>
</tr>
<tr align="center">
<td>DEC{1..5}</td>
<td align="left">Decimal, optionally fixed to 1 - 5 digits</td>
<td>1</td>
</tr>
<tr align="center">
<td>SDEC{1..5}</td>
<td align="left">Signed decimal, optionally fixed to 1 - 5 digits</td>
<td>1,2</td>
</tr>
<tr align="center">
<td>HEX{1..4}</td>
<td align="left">Hexadecimal, optionally fixed to 1 - 4 digits</td>
<td>1</td>
</tr>
<tr align="center">
<td>SHEX{1..4}</td>
<td align="left">Signed hexadecimal, optionally fixed to 1 - 4 digits </td>
<td>1,2</td>
</tr>
<tr align="center">
<td>IHEX{1..4}</td>
<td align="left">Indicated hexadecimal, optionally fixed to 1 - 4 digits</td>
<td>1</td>
</tr>
<tr align="center">
<td>ISHEX{1..4}</td>
<td align="left">Signed, indicated hexadecimal, optionally fixed to 1 - 4
digits ($ prefix)</td>
<td>1,2</td>
</tr>
<tr align="center">
<td>BIN{1..16}</td>
<td align="left">Binary, optionally fixed to 1 - 16 digits</td>
<td>1</td>
</tr>
<tr align="center">
<td>SBIN{1..16}</td>
<td align="left">Signed binary, optionally fixed to 1 - 16 digits</td>
<td>1,2</td>
</tr>
<tr align="center">
<td>IBIN{1..16}</td>
<td align="left">Indicated binary, optionally fixed to 1 - 16 digits</td>
<td>1</td>
</tr>
<tr align="center">
<td>ISBIN{1..16}</td>
<td align="left">Signed, indicated binary, optionally fixed to 1 - 16 digits</td>
<td>1,2</td>
</tr>
</table>
<ol>
<li value="1">Fixed-digit formatters like DEC4 will pad the number with leading 0s if
necessary; ex: DEC4 65 sends 0065. If a number is larger than the specified
number of digits, the leading digits will be dropped; ex: DEC4 56422 sends
6422.</li>
<li value="2">Signed modifiers work under two's complement rules.</li>
</ol>
<p>&#160;</p>
<table width="100%" cellpadding="4" cellspacing="0" border="1">
<tr bgcolor="#CFCFCF" align="center" valign="top">
<td width="20%">Special Formatter</td>
<td width="80%">Action</td>
</tr>
<tr>
<td align="center">?</td>
<td>Displays "symbol = x' + carriage return; where x is a number. Default
format is decimal, but may be combined with conversion formatters (ex: BIN
? x to display "x = binary_number").</td>
</tr>
<tr>
<td align="center">ASC ?</td>
<td>Displays "symbol = 'x'" + carriage return; where x is an ASCII character.</td>
</tr>
<tr>
<td align="center">STR ByteArray {\L}</td>
<td>Send character string from an array. The optional \L argument can be used
to limit the output to L characters, otherwise, characters will be sent up to
the first byte equal to 0 or the end of RAM space is reached.</td>
</tr>
<tr>
<td align="center">REP Byte\L</td>
<td>Send a string consisting of Byte repeated L times (ex: REP "X"\10
sends "XXXXXXXXXX").</td>
</tr>
</table>
<p>&#160;</p>
<p class="PlainText">The I<sup>2</sup>C protocol has a well-defined standard for the information
passed at the start of each transmission. First of all, any information sent must
be transmitted in units of one byte (8-bits). The first byte, we call the
<i>SlaveID</i>, is an 8-bit pattern whose upper 7-bits contain the unique ID of
the device you wish to communicate with. The lowest bit indicates whether this
is a write operation (0) or a read operation (1). Here is the <i>SlaveID</i> <![CDATA[ ]]>format:</p>
<center>
<table cellpadding="4" cellspacing="0" border="1">
<tr align="center" bgcolor="#CFCFCF">
<td width="35">7</td>
<td width="35">6</td>
<td width="35">5</td>
<td width="35">4</td>
<td width="35">3</td>
<td width="35">2</td>
<td width="35">1</td>
<td width="35">0</td>
</tr>
<tr align="center">
<td>A<sub>6</sub></td>
<td>A<sub>5</sub></td>
<td>A<sub>4</sub></td>
<td>A<sub>3</sub></td>
<td>A<sub>2</sub></td>
<td>A<sub>1</sub></td>
<td>A<sub>0</sub></td>
<td>R/W</td>
</tr>
</table>
</center>
<p>&#160;</p>
<p class="PlainText">The second byte, immediately following the <i>SlaveID</i>, is the <i>Address</i>.
It indicates the 8-bit address (within the device) where the <i>OutputData</i> is
to be written (the first location if more than one byte is written). Note that
the <i>Address</i> argument is optional and may be left unspecified for devices
that don't require an address parameter.</p>
<p class="PlainText">Some devices require more than eights bits of address. For this case, the optional
<i>LowAddress</i> argument can be used for the low-byte of the required address.
When using the <i>LowAddress</i> argument, the <i>Address</i> argument is
effectively the high-byte of the address value. For example, if the entire
address value is 2050, use 8 for the <i>Address</i> argument and 2 for the
<i>LowAddress</i> argument (8 * 256 + 2 = 2050).</p>
<p class="PlainText">Following the last address byte is the first byte of data. This data byte may
be transmitted or received by the BASIC Stamp. In the case of the <span class="keyword_in_text">I2COUT</span>
command, this data byte is transmitted by the BASIC Stamp and received by the
device. Additionally, multiple data bytes can follow the address, depending on
the I<sup>2</sup>C device. Note that every device has different limitations
regarding how may contiguous bytes they can receive or transmit in one session.
Be aware of these device limitations and program accordingly.</p>
<p class="PlainText">Every I<sup>2</sup>C transmission session begins with a Start Condition and
ends with a Stop Condition. Additionally, immediately after every byte is
transmitted, an extra clock cycle is used to send or receive an acknowledgment
signal (ACK). All of these operations are automatically taken care of by the
<span class="keyword_in_text">I2CIN</span> command so that you need not be concerned with them. The general
I<sup>2</sup>C transmission format is shown below.</p>
<center>
<img src="../../graphics/i2c_packet.gif" border="0">
</img>
</center>
<p>&#160;</p>
<p class="PlainText">Since the <span class="keyword_in_text">I2COUT</span> command is intended for output only, it actually
overrides the "R/W" bit (bit 0) in the <i>SlaveID</i> argument. This is done to
avoid device conflicts should the value be mistyped. Put simply, this means
commands such as:
</p><pre class="BScode" xml:space="preserve"> I2COUT 0, $A0, 10, [0] </pre>
<p class="PlainText">...and:</p><pre class="BScode" xml:space="preserve"> I2COUT 0, $A1, 10, [0]</pre>
<p class="PlainText">...both transmit the same thing ($A0, then 10, then the data). Even though the
<span class="keyword_in_text">I2COUT</span> command really doesn't care what the value of the <i>SlaveID</i>'s
LSB is, it is suggested that you still set it appropriately for clarity.</p>
<p class="PlainText">Also note that the <span class="keyword_in_text">I2COUT</span> command does not support multiple I<sup>2</sup>C
masters and the BASIC Stamp cannot operate as an I<sup>2</sup>C slave device.</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>