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

245 lines
15 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>POLLIN</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">POLLIN</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/PollinOutEx.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">POLLIN / POLLOUT / POLLMODE Example</a>
</p>
<p>&#160;</p>
</div>
<p class="clear">&#160;</p>
<p>&#160;</p>
<p class="PlainText">Syntax: <span class="keyword_in_text">POLLIN</span> <![CDATA[ ]]><i>Pin, State</i></p>
<h2>Function</h2>
<p class="PlainText">Specify a polled-input pin and active state.
</p>
<ul>
<li value="1"><b><i>Pin</i></b> is a variable/constant/expression (0 - 15) that specifies
the I/O pin to use. This I/O pin will be set to input mode.</li>
<li value="2"><b><i>State</i></b> is a variable/constant/expression (0 - 1) that specifies
whether to poll the I/O pin for a low (0) or a high (1) level.</li>
</ul>
<h2>Quick Facts</h2>
<table cellpadding="4" cellspacing="0" border="1">
<tr bgcolor="#CFCFCF">
<td width="150" align="center">&#160;</td>
<td width="1000" align="center">BS2p, BS2pe, and BS2px</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">Available actions in response to reaching
the desired <i>State</i></td>
<td align="left">
<ol>
<li value="1">Nothing,</li>
<li value="2">Set polled-output pins to a specified state,</li>
<li value="3">Run another program (in a specified program-slot),</li>
<li value="4">Wait (pause program execution) until desired State is reached,</li>
<li value="5">Any combination of 2, 3 and 4, above.</li>
</ol>
</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">Special Notes</td>
<td align="left">
<ul>
<li value="1">The polled-input pins are monitored (polled) in-between each command
within the PBASIC code.</li>
<li value="2">On the BS2p40 polled-input pins can be defined on both main and
auxiliary I/O pins. These are all active regardless of which group
the program happens to be using at the time of a polling event.</li>
<li value="3">Polled input pins are stored in special Scratchpad RAM locations. Use
<a href="GET.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">GET</a> to retrieve.<br /><ul type="square"><li value="1">Location 128 : MAINIO Pins 0 - 7</li><li value="2">Location 129 : MAINIO Pins 8 - 15</li><li value="3">Location 130 : AUXIO Pins 0 - 7</li><li value="4">Location 131 : AUXIO Pins 8 - 15</li></ul></li>
</ul>
</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">Related Commands</td>
<td align="center" colspan="1">
<p colspan="1" align="center"><a href="POLLMODE.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">POLLMODE</a>, <a href="POLLOUT.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">POLLOUT</a>, <a href="POLLRUN.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">POLLRUN</a>, <a href="POLLWAIT.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">POLLWAIT</a></p>
</td>
</tr>
</table>
<h2>Explanation</h2>
<p class="PlainText">The <span class="keyword_in_text">POLLIN</span> command is one of a family of unique "polling" commands on the
BS2p, BS2pe, and BS2px modules. The other commands in this family include <span class="keyword_in_text">POLLMODE</span>,
<span class="keyword_in_text">POLLOUT</span>, <span class="keyword_in_text">POLLRUN</span> and <span class="keyword_in_text">POLLWAIT</span>. The <span class="keyword_in_text">POLLIN</span> command
is used to specify an input pin to monitor, or "poll", in-between instructions
during the rest of the PBASIC program. The BASIC Stamp will then perform some
activity (in between instructions) when the specified State is detected. The
activity performed depends on the <span class="keyword_in_text">POLLMODE</span>, <span class="keyword_in_text">POLLOUT</span> and <span class="keyword_in_text">POLLRUN</span>commands.</p>
<p class="PlainText">The "polling" commands allow the BASIC Stamp to respond to certain I/O pin
events at a faster rate than what is normally possible through manual PBASIC
programming. The term "poll" comes from the fact that the BASIC Stamp's interpreter
periodically checks the state of the designated polled-input pins. It "polls"
these pins after the end of each PBASIC instruction and before it reads the next
PBASIC instruction from the user program; giving the appearance that it is polling
"in the background". </p>
<p class="PlainText">This feature should not be confused with the concept of interrupts, as
<i><b>the BASIC Stamp does not support true interrupts.</b></i></p>
<p class="PlainText">The following is an example of the <span class="keyword_in_text">POLLIN</span> command:</p><pre class="BScode" xml:space="preserve">
POLLIN 0, 0
POLLMODE 2
</pre>
<p class="PlainText">The <span class="keyword_in_text">POLLIN</span> command in the above code will cause the BASIC Stamp to set
I/O pin 0 to an input mode and get ready to poll it for a low (0) state. The
BASIC Stamp will not actually start polling until it is set to the appropriate
mode, however. The second line, <span class="keyword_in_text">POLLMODE</span>, initiates the polling process
(see the <a href="POLLMODE.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">POLLMODE</a> description for more information).
From then on, as the BASIC Stamp executes the rest of the program, it will check
for a low level (logic 0) on I/O pin 0, in-between instructions. </p>
<p class="PlainText">In the code above, no obvious action will be noticed since we didn't tell the
BASIC Stamp what to do when it detects a change on the I/O pin. One possible
action the BASIC Stamp can be instructed to take is to change the state of an
output, called a polled-output. Take a look at the next example:</p><pre class="BScode" xml:space="preserve">
Setup:
POLLIN 0, 0
POLLOUT 1, 1
POLLMODE 2
Main
DEBUG "Looping...", CR
GOTO Main
</pre>
<p class="PlainText">In this example, in addition to an endless loop, we've added another polling
command called <span class="keyword_in_text">POLLOUT</span> (see the <a href="POLLOUT.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">POLLOUT</a> description
for more information). Our <span class="keyword_in_text">POLLOUT</span> command tells the BASIC Stamp to set
I/O pin 1 to an output mode and set it high (1) when it detects the desired poll
state. The poll state is the low (0) level on I/O pin 0 that <span class="keyword_in_text">POLLIN</span> told
it to look for. If the polled-input pin is high, it will set polled-output
pin 1 to low (0), instead.</p>
<p class="PlainText">Once the program reaches the endless loop, starting at Main, it will continuously
print "Looping..." on the PC screen. In between reading the <span class="keyword_in_text">DEBUG</span> command
and the <span class="keyword_in_text">GOTO</span> command (and vice versa) it will check polled-input pin 0 and
set polled-output pin 1 accordingly. In this case, when I/O pin 0 is set low,
the BASIC Stamp will set I/O pin 1 high. When I/O pin 0 is set high, the BASIC
Stamp will set I/O pin 1 low. It will continue to perform this operation,
in-between each command in the loop, endlessly.</p>
<p>It's important to note that, in this example, only the <span class="keyword_in_text">DEBUG</span> and <span class="keyword_in_text">GOTO</span>commands are being executed over and over again. The first three lines of code
are only run once, yet their effects are "remembered" by the BASIC Stamp throughout
the rest of the program.</p>
<p>If the polling commands were not used, the program would have to look like the
one below in order to achieve the same effect.</p><pre class="BScode" xml:space="preserve">
Setup:
INPUT 0
OUTPUT 1
Main:
OUT1 = ~IN0
DEBUG "Looping...", CR
OUT1 = ~IN0
GOTO Main
</pre>
<p class="PlainText">In this example, we create the inverse relationship of input pin 0 and output
pin 1 manually, in-between the <span class="keyword_in_text">DEBUG</span> and <span class="keyword_in_text">GOTO</span> lines. Though the
effects are the same as when using the polling commands, this program actually
takes a little longer to run and consumes 7 additional bytes of program (EEPROM)
space. Clearly, using the polling commands is more efficient.</p>
<p class="PlainText">You can have as many polled-input and polled-output pins as you have available.
If multiple polled-input pins are defined, any one of them can trigger changes on
the polled-output pins that are also defined. For example:</p><pre class="BScode" xml:space="preserve">
Setup:
POLLIN 0, 1
POLLIN 1, 1
POLLOUT 2, 1
POLLMODE 2
Main:
DEBUG "Looping...", CR
GOTO Main
</pre>
<p class="PlainText">This code sets I/O pins 0 and 1 to polled-input pins (looking for a high (1)
state) and sets I/O pin 2 to be a polled-output pin (with a high-active state).
If either I/O pin 0 or 1 goes high, the BASIC Stamp will set I/O pin 2 high.
This works similar to a logical OR operation. The truth table below shows all the
possible states of these two polled-input pins and the corresponding states the
BASIC Stamp will set the polled-output pin to.</p>
<center>
<table width="200" cellpadding="4" cellspacing="0" border="1">
<tr bgcolor="#CFCFCF">
<td colspan="2" align="center">Polled-Inputs</td>
<td colspan="1" align="center">Polled-Output</td>
</tr>
<tr bgcolor="#CFCFCF">
<td width="50" align="center">0</td>
<td width="50" align="center">1</td>
<td width="100" align="center">2</td>
</tr>
<tr>
<td align="center">0</td>
<td align="center">0</td>
<td align="center">0</td>
</tr>
<tr>
<td align="center">0</td>
<td align="center">1</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">0</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">1</td>
<td align="center">1</td>
</tr>
</table>
</center>
<p>&#160;</p>
<p class="PlainText">Normally, any polled-output pins reflect the state changes continuously, as
described above. The <span class="keyword_in_text">POLLMODE</span> command supports another feature, however,
where the polled-output pins will latch the active state; they will change only
once (when the poll state is reached) and stay in the new state until the PBASIC
program tells it to change again. See the
<a href="POLLMODE.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">POLLMODE</a> description for more information.</p>
<p class="PlainText">Other possible actions in response to polled-input states are: 1) Running
another program (in a specified program slot), 2) Waiting (pausing program
execution with or without low-power mode) until the poll state is reached, or 3)
Any combination of the above-mentioned actions</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>