<?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>RCTIME</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">RCTIME</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> </p> <p style="text-align: right;"><a href="../ExampleTopics/RctimeEx.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">RCTIME Examples</a> </p> <p> </p> </div> <p class="clear"> </p> <p> </p> <p class="PlainText">Syntax: <span class="keyword_in_text">RCTIME</span> <![CDATA[ ]]><i>Pin</i>,<i> State</i>,<i> Variable</i></p> <h2>Function</h2> <p class="PlainText">Measure time while <i>Pin</i> remains in <i>State</i>; usually to measure the charge/discharge time of resistor/capacitor (RC) circuit.. </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 pin will be placed into input mode.</li> <li value="2"><b><i>State</i></b> is a variable/constant/expression (0 - 1) that specifies the desired state to measure. Once <i>Pin</i> is not in <i>State</i>, the command ends and stores the result in <i>Variable</i>.</li> <li value="3"><b><i>Variable</i></b> is a variable (usually a word) in which the time measurement will be stored. The unit of time for <i>Variable</i> is described below.</li> </ul> <p>Note: See <a href="POT.htm" target="" title="" alt="" class="MCXref_0" xrefformat="{paratext}">POT</a> for the BS1.</p> <h2>Quick Facts</h2> <table width="100%" cellpadding="4" cellspacing="0" border="1"> <tr bgcolor="#CFCFCF" align="center"> <td nowrap="" width="150"> </td> <td width="23%">BS2, BS2e, and BS2pe</td> <td width="23%">BS2sx</td> <td width="23%">BS2p</td> <td width="23%">BS2px</td> </tr> <tr align="center"> <td bgcolor="#CFCFCF">Units in <i>Variable</i></td> <td>2 µs</td> <td>0.8 µs</td> <td>0.75 µs</td> <td>0.75 µs</td> </tr> <tr align="center"> <td bgcolor="#CFCFCF"><i>Maximum pulse width</i> </td> <td>131.07 ms</td> <td>52.428 ms</td> <td>49.151 ms</td> <td>49.151 ms</td> </tr> </table> <h2>Explanation</h2> <p class="PlainText"><span class="keyword_in_text">RCTIME</span> can be used to measure the charge or discharge time of a resistor/capacitor circuit. This allows you to measure resistance or capacitance; use R or C sensors such as thermistors or capacitive humidity sensors or respond to user input through a potentiometer. In a broader sense, <span class="keyword_in_text">RCTIME</span> can also serve as a fast, precise stopwatch for events of very short duration. </p> <p class="PlainText">When <span class="keyword_in_text">RCTIME</span> executes, it starts a counter (who's unit of time is shown above). It stops this counter as soon as the specified pin is no longer in <i>State</i> (0 or 1). If pin is not in <i>State</i> when the instruction executes, <span class="keyword_in_text">RCTIME</span> will return 1 in <i>Variable</i>, since the instruction requires one timing cycle to discover this fact. If pin remains in <i>State</i> longer than 65535 timing cycles <span class="keyword_in_text">RCTIME</span> returns 0. </p> <p class="PlainText">The figure below shows suitable RC circuits for use with <span class="keyword_in_text">RCTIME</span>. Circuits A are preferred, because the BASIC Stamp's logic threshold is approximately 1.4 volts. This means that the voltage seen by the pin will start at 5V then fall to 1.4V (a span of 3.6V) before <span class="keyword_in_text">RCTIME</span> stops. With Circuit B, the voltage will start at 0V and rise to 1.4V (spanning only 1.4V) before <span class="keyword_in_text">RCTIME</span> stops. For the same combination of R and C, Circuits A will yield a higher count, and therefore more resolution than Circuit B. </p> <center> <img src="../../graphics/rctime_sch_new.gif" border="0"> </img> </center> <p> </p> <p class="PlainText">Before <span class="keyword_in_text">RCTIME</span> executes, the capacitor must be put into the state specified in the <span class="keyword_in_text">RCTIME</span> instruction. For example, with Circuit A, the capacitor must be charged until the top plate at 5V.</p> <p class="PlainText">Here's a typical sequence of instructions for Circuit A (assuming I/O pin 7 is used):</p><pre class="BScode" xml:space="preserve"> result VAR Word Test: HIGH 7 ' charge the cap PAUSE 1 ' for 1 ms RCTIME 7, 1, result ' measure RC discharge time DEBUG DEC ? result ' display result END </pre> <p class="PlainText">Using <span class="keyword_in_text">RCTIME</span> is very straightforward, except for one detail: For a given R and C, what value will <span class="keyword_in_text">RCTIME</span> return? It's easy to figure, based on a value called the RC time constant, or tau (τ) for short. Tau represents the time required for a given RC combination to charge or discharge by 63 percent of the total change in voltage that they will undergo. More importantly, the value t is used in the generalized RC timing calculation. Tau's formula is just R multiplied by C: </p> <p class="PlainText">τ = R x C</p> <p class="PlainText">The general RC timing formula uses τ to tell us the time required for an RC circuit to change from one voltage to another: </p> <p class="PlainText">time = -τ x ( ln(V<sub>final</sub> / V<sub>initial</sub>) )</p> <p class="PlainText">In this formula ln is the natural logarithm; it's a key on most scientific calculators. Let's do some math. Assume we're interested in a 10 kΩ resistor and 0.1 µF capacitor. Calculate τ: </p> <p class="PlainText">τ = (10 x 10<sup>3</sup>) x (0.1 x 10<sup>-6</sup>) = 1 x 10<sup>-3</sup></p> <p class="PlainText">The RC time constant is 1 x 10<sup>-3</sup> or 1 millisecond. Now calculate the time required for this RC circuit to go from 5V to 1.4V (as in Circuit A): </p> <p class="PlainText">Time = -1 x 10<sup>-3</sup> x ( ln(1.4v ÷ 5.0v) ) = 1.273 x 10<sup>-3</sup></p> <p class="PlainText">On the BS2, the unit of time is 2 µs, that time (1.273 x 10<sup>-3</sup>) works out to about 635 units. With a 10 kΩ resistor and 0.1 µF capacitor, <span class="keyword_in_text">RCTIME</span> would return a value of approximately 635. Since V<sub>initial</sub>and V<sub>final</sub> doesn't change, we can use a simplified rule of thumb to estimate <span class="keyword_in_text">RCTIME</span> results for Circuit A: </p> <p class="PlainText"><span class="keyword_in_text">RCTIME</span> units = 635 x R (in kΩ) x C (in µF)</p> <p class="PlainText">Another handy rule of thumb can help you calculate how long to charge/discharge the capacitor before <span class="keyword_in_text">RCTIME</span>. In the example above that's the purpose of the <span class="keyword_in_text">HIGH</span> and <span class="keyword_in_text">PAUSE</span> commands. A given RC charges or discharges 98 percent of the way in five time constants (5 x R x C). In Circuits A and B, the charge/discharge current passes through the 220 Ω series resistor and the capacitor. So if the capacitor were 0.1 µF, the minimum charge/discharge time should be: </p> <p class="PlainText">Charge time = 5 x 220 x (0.1 x 10<sup>-6</sup>) = 110 x 10<sup>-6</sup></p> <p class="PlainText">So it takes only 110 µs for the capacitor to charge/discharge, meaning that the one millisecond charge/discharge time of the example is plenty. </p> <p class="PlainText">A final note about the circuits above: You may be wondering why the 220 Ω resistor is necessary at all. Consider what would happen if resistor R was a potentiometer, and were adjusted to 0 Ω. When the I/O pin went high to discharge the capacitor, it would see a short direct to ground. The 220 Ω series resistor would limit the short circuit current to 5V ÷ 220 Ω = 23 mA and protect the BASIC Stamp from damage. (Actual current would be quite a bit less due to internal resistance of the pin's output driver, but you get the idea.)</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>