pbi-ide/help/BasicStampHelp/Content/FAQTopics/UsbBoardResets.htm

134 lines
13 KiB
HTML
Raw Permalink 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="Connection Troubleshooting" 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>USB Resets BASIC Stamp</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="TFAQ.htm">Connection Troubleshooting</a><span class="MCBreadcrumbsDivider"> &gt; </span><span class="MCBreadcrumbs">USB Resets BASIC Stamp</span>
</div>
<h1>USB Resets BASIC Stamp</h1>
<p class="PlainText">A BASIC Stamp connected to a PC via a USB can be reset by the USB connection. This can occur with USB-based boards, and with serial boards using the USB to Serial Adapter. Resets can happen when:</p>
<ul>
<li value="1">Connecting the board to the USB&#160;port.</li>
<li value="2">Disconnecting the board from the USB port, at either end of the USB&#160;cable. </li>
<li value="3">Connecting or disconnecting another USB device on a port adjacent to your BASIC Stamp board's USB port.</li>
</ul>
<p class="PlainText">The resets are caused by the PC's operating system checking to see if a new plug-and-play device (such as a mouse) was just connected. Disconnecting your board may cause a single reset. Reconnecting typically causes several resets in a row during the first few seconds.</p>
<p class="Tip" MadCap:autonum="Tip: &#160;"><span class="autonumber"><span class="TipSpan">Tip: &#160;</span></span>If you are using the USB&#160;to Serial Adapter, resets are easy to avoid. After connecting your board to your PC&#160;the first time, always disconnect and reconnect at the serial (9-pin) end of the USB to Serial Adapter. Leaving the Adapter connected to your PC keeps the virtual communications port open, preventing an unwanted reset.</p>
<h2>What BASIC Stamp Resets Do</h2>
<p class="PlainText"> Each time a BASIC Stamp is reset:</p>
<ul>
<li value="1">The program execution restarts from the beginning of code. </li>
<li value="2">All variables are set to their initial value (zero by default).</li>
</ul>
<p class="PlainText">Make sure to keep this in mind while working on your BASIC Stamp projects. Some programs have variable values that you do not want to lose when you disconnect or reconnect your board to the PC. This is common with datalogging activities.</p>
<h2>Datalogging Activities</h2>
<p class="PlainText">Some Stamps in Class activities use BASIC Stamp programs that log sensor data. Typically, the programmed board is disconnected from the PC, and taken someplace to record data with sensors. Afterward, the board is reconnected to the PC so the sensor data can be displayed on the Debug Terminal or saved to the PC&#160;for later use. </p>
<p class="PlainText">If a program only records sensor data in RAM as variable values, a USB-based board will lose that data when reconnecting to the PC because the BASIC Stamp will reset. So, USB-based boards must stay connected to the computer with programs that hold data RAM as variable values. </p>
<p class="PlainText">For USB-based boards and datalogging programs with applications where the board is disconnected and reconnected to the PC, the program should:</p>
<ul>
<li value="1">Save values to EEPROM</li>
<li value="2">Use a one-second delay at the beginning of the program.</li>
</ul>
<h2>Save Values to EEPROM&#160;using <span class="keyword_in_text">READ</span>, <span class="keyword_in_text">WRITE</span>,&#160;and <span class="keyword_in_text">DATA</span> <![CDATA[ ]]></h2>
<p class="PlainText">A simple solution for retaining values between resets is to save them in portions of the EEPROM program memory that are not used to store the actual program. Since the program is stored starting at the highest memory address, the low memory addresses are usually available for storing these values.</p>
<p class="PlainText">Use the <span class="keyword_in_text">WRITE</span> command to store values to addresses in BASIC Stamp EEPROM memory. Then, use <span class="keyword_in_text">READ</span> commands to retrieve those values from EEPROM, even after connecting it to the PC. You can build <span class="keyword_in_text">READ</span> commands into your main program, or even run a separate program to retrieve those values. </p>
<p class="PlainText"> Pre-initialized values can also be stored in EEPROM with the <span class="keyword_in_text">DATA</span> directive. </p>
<p class="PlainText" MadCap:conditions="BSEconditions.BSEWebHelp (Primary)-INCLUDE">See the PBASIC Language Reference's <a href="../LanguageTopics/Commands/READ.htm" target="" title="" alt="" class="MCXref_0">READ</a>, <a href="../LanguageTopics/Commands/WRITE.htm" target="" title="" alt="" class="MCXref_0">WRITE</a>, and <a href="../LanguageTopics/Commands/DATA.htm" target="" title="" alt="" class="MCXref_0">DATA</a> commands for more information.</p>
<h2>Prevent Unexpected Bugs by Starting Your Programs with a One-Second Delay</h2>
<p class="PlainText">If your application requires reconnecting your USB board to the PC while the BASIC Stamp program is running, always add a one-second delay to the beginning of your program. Examples of one-second delays for BS2 modules include: <span class="code_in_text">PAUSE 1000</span> and <span class="code_in_text">FREQOUT Pin, 1000, <i>Frequency</i></span>. </p>
<p class="PlainText"> Adding a one-second delay at the start of your program could prevent these two unwanted behaviors:</p>
<ol>
<li value="1">If the BASIC Stamp transmits serial messages immediately after these resets, the operating system could mistake it for a serial mouse or other plug-and play-device.</li>
<li value="2"> If your program advances a value stored in EEPROM with each reset, the value will increase by one or more (ten is not uncommon) each time you connect your USB BASIC Stamp board to a PC.</li>
</ol>
<p class="PlainText">Again, the one-second delay at the very beginning of the program prevents these issues. Other ways to prevent them include leaving the power off until one second after you have connected your USB board, or pressing and holding the reset button for that time.</p>
<h2>Programs that Use the Reset Button</h2>
<p class="PlainText">Some programs use your board's Reset button to advance a value stored in EEPROM with each reset, or use the reset to trigger a "record" or "playback" mode.</p>
<p class="PlainText">For reset button applications with USB boards, it's best to design the application assuming the reset will happen when you disconnect and reconnect the board to the PC. Then, make sure to press and hold the reset button each time you disconnect or reconnect your board. This will ensure that a single, expected reset occurs, and not multiple resets as can happen with reconnection.</p>
<p class="PlainText">This example program uses a <span class="keyword_in_text">DATA</span> directive to pre-initialize the value stored by EEPROM address 0 to 255 when the program is loaded. The <span class="keyword_in_text">READ</span> command fetches this value from EEPROM and adds 1 to it. For a byte variable, 255 + 1 = 0 since 255 is the largest it can store, so it rolls over to zero. Taking the remainder of this <span class="code_in_text">eeVal/2</span> results in either 0 or 1. The <span class="keyword_in_text">WRITE</span> command stores this modified value back to EEPROM address 0. Then, the <span class="keyword_in_text">ON…GOTO…</span> command uses <span class="code_in_text">eeVal</span>, which stores 0 or 1. With each reset of the BASIC Stamp, the value advances from 0 to 1, to 0 again, and so on…</p><pre class="SICcode">
' {$STAMP BS2} ' Select BS2 as target module
' {$PBASIC 2.5} ' Select PBASIC 2.5 as language
eeVal VAR Byte ' EEPROM value variable
DATA 255 ' Pre-initialize EEPROM address 0 to 255
PAUSE 1000 ' Always wait 1 s before advancing EEPROM
READ 0, eeVal ' Fetch value from EEPROM address 0
eeval = eeVal + 1 ' Add 1 to eeVal
eeval = eeval // 2 ' Take remainder of eeVal / 2
WRITE 0, eeVal ' Write new result back to EEPROM
ON eeVal GOTO Playback, Record ' Use eeVal result to select routine
Playback: ' Playback label
DEBUG "Playing back..." ' Debug message indicating playback started
' Report logged data code here ' Your playback code goes here
END ' End, don't record after playback
Record: ' Record label
DEBUG "Recording..." ' Message indicates recording started
' Datalogging code here ' Your datalogging code goes here
END </pre>
<h2>Check for the PC Connection with <span class="keyword_in_text">SERIN</span></h2>
<p class="PlainText">Another useful strategy is to write a program routine that checks to see if the board is connected to the PC. Then the program can make decisions based on this information. For example, "if not connected, then log data” and “if connected, then display data.”</p>
<p class="PlainText">The code example below demonstrates one way to do this. It displays a message to press any key, then a <span class="keyword_in_text">SERIN</span> command that waits for a message from P16 (the programming port) for 2000 ms. If a character is typed into the Debug Terminals Transmit windowpane during that time, the program continues through the <span class="code_in_text">Playback</span> routine. If not, it jumps to the <span class="code_in_text">Record</span> routine.</p><pre class="SICcode">
' {$STAMP BS2} ' Select BS2 as target module
' {$PBASIC 2.5} ' Select PBASIC 2.5 as language
char VAR Byte ' Variable for getting character
PAUSE 1000 ' Always wait 1 s before serial transmit
DEBUG "Press any key...", CR ' User prompt
' Wait 2 s for character from Debug Terminals Transmit windowpane. Jump to
' Record label if no character received during that time. Continue to Playback
' label if character received.
SERIN 16, 84, 2000, Record, [char]
Playback: ' Playback label
DEBUG "Playing back..." ' Debug message indicating playback started
' Report logged data code here ' Your playback code goes here
END ' End, dont record after playback
Record: ' Record label
DEBUG "Recording..." ' Message indicates recording started
' Datalogging code here ' Your datalogging code goes here
END </pre>
<p class="PlainText" MadCap:conditions="BSEconditions.BSEWebHelp (Primary)-INCLUDE">See the PBASIC Language Reference's <![CDATA[ ]]><a href="../LanguageTopics/Commands/SERIN.htm" target="" title="" alt="" class="MCXref_0">SERIN</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>