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/Reference/Pins.htm

155 lines
8.9 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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="PBASIC Language Reference" 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>PIN</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>
<div class="MCBreadcrumbsBox_0"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="AlphaRef.htm">PBASIC Language Reference</a><span class="MCBreadcrumbsDivider"> &gt; </span><span class="MCBreadcrumbs">PIN&#160;Directive</span>
</div>
<h1 class="code">PIN</h1>
<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" /><span class="code_in_text">{PBASIC&#160;2.5}</span>
</p>
<p class="PlainText">&#160;</p>
<p class="PlainText">This section discusses the <span class="keyword_in_text">PIN&#160;</span>directive available with PBASIC 2.5</p>
<p class="PlainText">(For BASIC Stamp I/O pin definitions and ratings, see <a href="../../HardwareTopics/PinDesc.htm" target="" title="" alt="" class="MCXref_0">Pin Descriptions</a>)</p>
<p class="PlainText">(For BS2px I/O pin commands, see <a href="../Commands/COMPARE.htm" target="" title="" alt="" class="MCXref_0">COMPARE</a> and <a href="../Commands/CONFIGPIN.htm" target="" title="" alt="" class="MCXref_0">CONFIGPIN</a>.)</p>
<p class="PlainText">With the introduction of PBASIC 2.5 syntax comes the <span class="keyword_in_text">PIN</span> directive. The
purpose of the <span class="keyword_in_text">PIN </span>directive is to give an alias to an I/O pin and let the
compiler determine whether a given line of code requires a numeric value for the
pin, the output bit for that pin, or the input bit for that pin. Using the <span class="keyword_in_text">PIN</span> directive simplifies I/O pin aliasing in sophisticated programs.</p>
<h2>PIN Syntax</h2>
<p class="PlainText">Syntax: <i>PIN_Symbol</i> <![CDATA[ ]]><span class="keyword_in_text">PIN</span><i>Pin_Number</i></p>
<ul>
<li value="1"><b><i>PIN_Symbol</i></b> is a unique symbol name that will be
used as an alias for the I/O pin number <i>Pin_Number</i>.</li>
<li value="2"><b><i>Pin_Number</i></b> is an I/O pin number (0 to 15)</li>
</ul>
<h2>Summary of PIN&#160;Behaviors in Context</h2>
<p class="PlainText"><i>PIN_Symbol</i> behaves like a constant:</p>
<ol>
<li value="1">...when used in a commands <i>Pin</i> argument. Example:</li>
</ol><pre class="snippet">OUTPUT PIN_Symbol</pre>
<ol MadCap:continue="true">
<li value="2">...when used in the index of an array. Example:</li>
</ol><pre class="snippet">myArray(PIN_Symbol) = 25</pre>
<p class="PlainText"><i>PIN_Symbol</i> behaves like an input variable (INx):</p>
<ol>
<li value="1">...when used in a commands non-Pin argument that expects to read a variable/constant/expression. Example:</li>
</ol><pre class="snippet">DEBUG BIN PIN_Symbol</pre>
<ol MadCap:continue="true">
<li value="2">...when used in a commands <i>Condition</i> argument. Example:</li>
</ol><pre class="snippet">IF PIN_Symbol = 1 THEN…</pre>
<ol MadCap:continue="true">
<li value="3">...when used to the right of the equal sign (=) in an assignment statement. Example:</li>
</ol><pre class="snippet">myVariable = PIN_Symbol + 1</pre>
<p class="PlainText"><i>PIN_Symbol</i> behaves like an output variable (OUTx):</p>
<ol>
<li value="1">...when used in a commands non-<i>Pin</i> argument that expects to write a result to a variable. Example:</li>
</ol><pre class="snippet">LOOKUP index, [0, 1, 1, 0, 1], PIN_Symbol</pre>
<ol MadCap:continue="true">
<li value="2">...when used to the left of the equal sign (=) in an assignment statement. Example:</li>
</ol><pre class="snippet">PIN_Symbol = 1</pre>
<h2>PIN&#160;Examples</h2>
<p class="PlainText">Using the <span class="keyword_in_text">PIN</span> declaration is similar to <span class="keyword_in_text">CON</span>:</p><pre class="BScode" xml:space="preserve">
' {$PBASIC 2.5}
RedLED PIN 0 ' red LED is connected to pin 0
IsOn CON 1 ' LED is active high
IsOff CON 0
Setup:
OUTPUT RedLED ' same as OUTPUT 0
Main:
DO
RedLED = IsOn ' same as Out0 = 1
PAUSE 1000
RedLED = IsOff ' same as Out0 = 0
PAUSE 1000
LOOP
</pre>
<p class="PlainText">Most programs will work like the example above where a given I/O pin will
either be an input or an output. There are times, however, when a change in
function is required mid program. Prior to PBASIC 2.5, programmers wishing to
use named constants were forced to create redundant definitions or complicated
modifiers. For example:</p><pre class="BScode" xml:space="preserve">
' {$PBASIC 2.0}
SDA CON 8 ' I2C data I/O
SCL CON 9 ' I2C clock output
...
I2C_Start:
INPUT SDA
INPUT SCL
LOW SDA
Clock_Hold: ' monitor device clock hold
IF (INS.LOWBIT(SCL) = 0) THEN Clock_Hold ' &lt;-- tricky modifiers
RETURN
</pre>
<p class="PlainText">With PBASIC 2.5 the tricky modifiers can be eliminated:</p><pre class="BScode" xml:space="preserve">
' {$PBASIC 2.5}
SDA PIN 8 ' I2C data I/O
SCL PIN 9 ' I2C clock output
...
I2C_Start:
INPUT SDA
INPUT SCL
LOW SDA
Clock_Hold: ' monitor device clock hold
DO : LOOP WHILE (SCL = 0) ' easier, same as "IN9 = 0"
RETURN
</pre>
<p class="PlainText">The compiler is "smart" and knows how to replace a <span class="keyword_in_text">PIN</span> directive with a
constant value, an INS register bit or and OUTS register bit. Note that as in
the example above, the programmer is still responsible for setting the DIRS bit
of the I/O pin as required by the program.</p>
<p class="PlainText">&#160;</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>