<?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>SHIFTOUT</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">SHIFTOUT</h1> <div class="ImagePlusCaption"> <div class="Col2"> <p> <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" /> </p> </div> <p style="text-align: right;"><a href="../ExampleTopics/ShiftoutEx.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">SHIFTOUT Example</a> </p> <p> </p> </div> <p class="clear"> </p> <p> </p> <p class="PlainText">Syntax: <span class="keyword_in_text">SHIFTOUT</span> <![CDATA[ ]]><i>Dpin</i>,<i> Cpin</i>,<i> Mode, </i>[<i>OutputData </i>{\<i>Bits</i>} {,<i>OutputData </i>{\<i>Bits</i>}...}]</p> <h2>Function</h2> <p class="PlainText">Shift data out to a synchronous serial device. </p> <ul> <li value="1"><b><i>Dpin</i></b> is a variable/constant/expression (0 - 15) that specifies the I/O pin that will be connected to the synchronous serial device's data input. This pin will be set to output mode.</li> <li value="2"><b><i>Cpin</i></b> is a variable/constant/expression (0 - 15) that specifies the I/O pin that will be connected to the synchronous serial device's clock input. This pin will be set to output mode.</li> <li value="3"><b><i>Mode</i></b> is a variable/constant/expression (0 - 1), or one of two predefined symbols, that tells <span class="keyword_in_text">SHIFTOUT</span> the order in which data bits are to be arranged. See the table below for value and symbol definitions.</li> <li value="4"><b><i>OutputData</i></b> is a variable/constant/expression containing the data to be sent. </li> <li value="5"><b><i>Bits</i></b> is an optional variable/constant/expression (1 - 16) specifying how many bits are to be output by <span class="keyword_in_text">SHIFTOUT</span>. If no <i>Bits</i>entry is given <span class="keyword_in_text">SHIFTOUT</span> defaults to 8 bits. When the <i>Bits</i> entry is given, the BASIC Stamp transmits the rightmost number of bits specified, regardless of the <i>Mode</i>.</li> </ul> <h2>Quick Facts</h2> <table width="100%" cellpadding="4" cellspacing="0" border="1"> <tr bgcolor="#CFCFCF"> <td width="250" align="center"> </td> <td width="25%" align="center">BS2, BS2e, and BS2pe</td> <td width="25%" align="center">BS2sx and BS2p</td> <td width="25%" align="center">BS2px</td> </tr> <tr> <td align="center" bgcolor="#CFCFCF">Timing of T<sub>h</sub>, T<sub>l</sub>, T<sub>a</sub> and T<sub>b</sub></td> <td align="center">14 µs / 46 µs / 15 µs / 30 µs</td> <td align="center">5.6 µs / 18 µs / 6.3 µs / 12.5 µs</td> <td align="center">3.6 µs / 11.8 µs / 4.0 µs / 7.8 µs</td> </tr> <tr> <td align="center" bgcolor="#CFCFCF">Transmission Rate</td> <td align="center">~ 16 kBits/Sec</td> <td align="center">~ 42 kBits/Sec</td> <td align="center">~ 65 kBits/Sec</td> </tr> <tr> <td align="center" bgcolor="#CFCFCF">Related Commands</td> <td align="center" colspan="2"> <p colspan="2" align="center"><a href="SHIFTIN.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">SHIFTIN</a> </p> </td> </tr> </table> <h2>Explanation</h2> <p class="PlainText"><span class="keyword_in_text">SHIFTIN</span> and <span class="keyword_in_text">SHIFTOUT</span> provide an easy method of acquiring data from synchronous serial devices. Synchronous serial differs from asynchronous serial (like <span class="keyword_in_text">SERIN</span> and <span class="keyword_in_text">SEROUT</span>) in that the timing of data bits (on a data line) is specified in relationship to clock pulses (on a clock line). Data bits may be valid after the rising or falling edge of the clock line. This kind of serial protocol is commonly used by controller peripherals like ADCs, DACs, clocks, memory devices, etc. </p> <p class="PlainText">At their heart, synchronous-serial devices are essentially shift-registers; trains of flip-flops that pass data bits along in a bucket brigade fashion to a single data output pin. Another bit is output each time the appropriate edge (rising or falling, depending on the device) appears on the clock line. </p> <p class="PlainText">The <span class="keyword_in_text">SHIFTOUT</span> instruction first causes the clock pin to output low and the data pin to switch to output mode. Then, <span class="keyword_in_text">SHIFTOUT</span> sets the data pin to the next bit state to be output and generates a clock pulse. <span class="keyword_in_text">SHIFTOUT</span>continues to generate clock pulses and places the next data bit on the data pin for as many data bits as are required for transmission.</p> <p class="PlainText">Making <span class="keyword_in_text">SHIFTOUT</span> work with a particular device is a matter of matching the mode and number of bits to that device's protocol. Most manufacturers use a timing diagram to illustrate the relationship of clock and data. One of the most important items to look for is which bit of the data should be transmitted first; most significant bit (MSB) or least significant bit (LSB). The table below shows the values and symbols available for the <i>Mode</i> argument</p> <table width="100%" cellpadding="4" cellspacing="0" border="1"> <tr bgcolor="#CFCFCF"> <td width="75" align="center">Symbol</td> <td width="65" align="center">Value</td> <td width="80%" align="center">Meaning</td> </tr> <tr> <td align="center">LSBFIRST</td> <td align="center">0</td> <td align="left">Data is shifted out LSB-first</td> </tr> <tr> <td align="center">MSBFIRST</td> <td align="center">1</td> <td align="left">Data is shifted out MSB-first</td> </tr> </table> <p> </p> <p class="PlainText">(MSB is most-significant bit; the highest or left-most bit of a Nibble, Byte, or Word. LSB is the least-significant bit; the lowest or right-most bit of a Nibble, Byte, or Word.)</p> <h3>SHIFTOUT Timing</h3> <center> <img src="../../graphics/shiftout_timing.gif" border="0"> </img> </center> <p class="PlainText">Here is a simple example:</p><pre class="BScode" xml:space="preserve"> SHIFTOUT 0, 1, MSBFIRST, [250] </pre> <p class="PlainText">Here, the <span class="keyword_in_text">SHIFTOUT</span> command will write to I/O pin 0 (the Dpin) and will generate a clock signal on I/O 1 (the <i>Cpin</i>). The <span class="keyword_in_text">SHIFTOUT</span> command will generate eight clock pulses while writing each bit (of the 8-bit value 250) onto the data pin (<i>Dpin</i>). In this case, it will start with the most significant bit first as indicated by the <i>Mode</i> value of MSBFIRST.</p> <p class="PlainText">By default, <span class="keyword_in_text">SHIFTOUT</span> transmits eight bits, but you can set it to shift any number of bits from 1 to 16 with the <i>Bits</i> argument. For example:</p><pre class="BScode" xml:space="preserve"> SHIFTOUT 0, 1, MSBFIRST, [250\4] </pre> <p class="PlainText">Will output only the lowest (rightmost) four bits (%1010 in this case). But what if you want to output the leftmost bits of a given value? By adding the right-shift operator (>>) to the code you can adjust the output as required:</p><pre class="BScode" xml:space="preserve"> SHIFTOUT 0, 1, MSBFIRST, [(250 >> 2)\6] </pre> <p class="PlainText">...will output the upper six bits (%111110 in this case).</p> <p class="PlainText">Some devices require more than 16 bits. To solve this, you can use a single <span class="keyword_in_text">SHIFTOUT</span> command with multiple values. Each value can be assigned a particular number of bits with the <i>Bits</i> argument. As in: </p><pre class="BScode" xml:space="preserve"> SHIFTOUT 0, 1, MSBFIRST, [250\4, 1045\16] </pre> <p class="PlainText">The preceding example will first shift out four bits of the number 250 (%1010) and then 16 bits of the number 1045 (%0000010000010101). The two values together make up a 20 bit value.</p> <p class="PlainText">In the examples above, specific numbers were entered as the data to transmit, but, of course, the <span class="keyword_in_text">SHIFTOUT</span> command will accept variables and expressions for the <i>OutputData</i> and even for the <i>Bits</i> argument.</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 © <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>