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

201 lines
13 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>PWM</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">PWM</h1>
<div class="ImagePlusCaption">
<div class="Col2">
<p>
<img src="../../graphics/pgm_icon1.gif" border="0" alt="BS1 icon" title="BS1 icon" />
<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/PwmEx.htm" target="" title="" alt="" class="MCXref_0">PWM Examples</a>
</p>
<p>&#160;</p>
</div>
<p class="clear">&#160;</p>
<p>&#160;</p>
<p class="PlainText">Syntax: <span class="keyword_in_text">PWM</span> <![CDATA[ ]]><i>Pin</i>,<i> Duty</i>,<i> Duration</i></p>
<h2>Function</h2>
<p class="PlainText">Convert a digital value to analog output via pulse-width modulation.
</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 set to output mode initially then set to
input mode when the command finishes.</li>
<li value="2"><b><i>Duty</i></b> is a variable/constant/expression* (0 - 255) that specifies
the analog output level as a number of 256<sup>ths</sup> of 5 V (0 to 4.98 V).</li>
<li value="3"><b><i>Duration</i></b> is a variable/constant/expression* (0 - 255) that
specifies the duration of the PWM output.</li>
</ul>
<p class="PlainText">*<img src="../../graphics/bs1note.gif" style="vertical-align: super;" /> Note: expressions are not allowed as arguments on the BS1. The range
of the <i>Pin</i> argument on the BS1 is 07.</p>
<h2>Quick Facts</h2>
<table width="100%" cellpadding="4" cellspacing="0" border="1">
<tr align="center" bgcolor="#CFCFCF">
<td nowrap="" width="150">&#160;</td>
<td width="12%">BS1</td>
<td width="12%">BS2 and BS2e</td>
<td width="12%">BS2sx</td>
<td width="12%">BS2p</td>
<td width="12%">BS2pe</td>
<td width="12%">BS2px</td>
</tr>
<tr align="center">
<td bgcolor="#CFCFCF">Units in <i>Duration</i></td>
<td>5 ms</td>
<td>1 ms</td>
<td>0.4 ms</td>
<td>0.65 ms</td>
<td>1.62 ms</td>
<td>0.4 ms</td>
</tr>
<tr align="center">
<td bgcolor="#CFCFCF">Average voltage equation</td>
<td colspan="6">Average Voltage = (<i>Duty</i> ÷ 256) x 5 volts</td>
</tr>
<tr align="center">
<td bgcolor="#CFCFCF">Require charge time (<i>Duration</i> ) equation</td>
<td colspan="6">Charge time = 5 x R x C,
<i>Duration</i> = Charge time ÷ Units in Duration</td>
</tr>
<tr align="center">
<td bgcolor="#CFCFCF">Special Notes</td>
<td colspan="6"><i>Pin</i> is set to output initially, and set to input at end</td>
</tr>
<tr>
<td align="center" bgcolor="#CFCFCF">Related Commands</td>
<td align="center" colspan="6">
<p colspan="6" align="center"><a href="FREQOUT.htm" target="" title="" alt="" class="MCXref_0">FREQOUT</a>, <a href="DTMFOUT.htm" target="" title="" alt="" class="MCXref_0">DTMFOUT</a></p>
</td>
</tr>
</table>
<h2>Explanation</h2>
<p class="PlainText">Pulse-width modulation (<span class="keyword_in_text">PWM</span>) allows the BASIC Stamp (a purely digital device)
to generate an analog voltage. The basic idea is this: If you make a pin output
high, the voltage at that pin will be close to 5 V. Output low is close to 0 V.
What if you switched the pin rapidly between high and low so that it was high half
the time and low half the time? The average voltage over time would be halfway
between 0 and 5 V (2.5 V). <span class="keyword_in_text">PWM</span> emits a burst of 1s and 0s whose ratio is
proportional to the duty value you specify.</p>
<p class="PlainText">The proportion of 1s to 0s in <span class="keyword_in_text">PWM</span> is called the duty cycle. The duty
cycle controls the analog voltage in a very direct way; the higher the duty cycle
the higher the voltage. In the case of the BASIC Stamp, the duty cycle can range
from 0 to 255. <i>Duty</i> is literally the proportion of 1s to 0s output by the
<span class="keyword_in_text">PWM</span> command. To determine the proportional <span class="keyword_in_text">PWM</span> output voltage, use
this formula: (<i>Duty</i> ÷ 256) x 5 V. For example, if <i>Duty</i>
is 100, (100 ÷ 256) x 5 V = 1.953 V; <span class="keyword_in_text">PWM</span> outputs a train of pulses
whose average voltage is 1.953 V.</p>
<p class="PlainText">In order to convert <span class="keyword_in_text">PWM</span> into an analog voltage we have to filter out the
pulses and store the average voltage. The resistor/capacitor combination shown
below will do the job. The capacitor will hold the voltage set by <span class="keyword_in_text">PWM</span> even
after the instruction has finished. How long it will hold the voltage depends on
how much current is drawn from it by external circuitry, and the internal leakage
of the capacitor. In order to hold the voltage relatively steady, a program must
periodically repeat the <span class="keyword_in_text">PWM</span> instruction to give the capacitor a fresh
charge. </p>
<p class="PlainText">Just as it takes time to discharge a capacitor, it also takes time to charge
it in the first place. The <span class="keyword_in_text">PWM</span> command lets you specify the charging time
in terms of <span class="keyword_in_text">PWM</span> duration. The timing for the units in <i>Duration</i> is
shown in in the table above. So, on the BS2, to charge a capacitor for five
milliseconds, you would specify five units in <i>Duration</i>. </p>
<p class="PlainText">How do you determine how long to charge a capacitor? Use this rule-of-thumb
formula: Charge time = 5 x R x C. For instance, the circuit below uses a 10 kΩ
(10 x 10<sup>3</sup> ohm) resistor and a 1 µF (1 x 10<sup>-6</sup> F) capacitor:</p>
<p class="PlainText">Charge time = 5 x 10 x 10<sup>3</sup> x 1 x 10<sup>-6</sup> = 50 x 10<sup>-3</sup>
seconds, or 50 milliseconds.</p>
<center>
<img src="../../graphics/pwm_sch.gif" border="0">
</img>
</center>
<p>&#160;</p>
<p class="PlainText">Since, on the BS2, each unit in <i>Duration</i> is approximately a millisecond,
it would take at least 50 units to charge the capacitor. Assuming the circuit is
connected to P0, here's the complete <span class="keyword_in_text">PWM</span> instruction: </p>
<p>
<img src="../../graphics/bs2e_inline.gif" border="0">
</img>
</p><pre class="BScode" xml:space="preserve">
PWM 0, 100, 50 ' Put a 1.96V charge on cap (BS2, BS2e)
</pre>
<p class="PlainText">To charge the same circuit to the same level using a BS2sx, the <i>Duration</i>would require adjustment as follows:</p>
<p>
<img src="../../graphics/bs2sx_inline.gif" border="0">
</img>
</p><pre class="BScode" xml:space="preserve">
PWM 0, 100, 125 ' Put a 1.96V charge on cap (BS2sx)
</pre>
<p class="PlainText">After outputting the <span class="keyword_in_text">PWM</span> pulses, the BASIC Stamp leaves the pin in input
mode (0 in the corresponding bit of DIRS). In input mode, the pin's output driver
is effectively disconnected. If it were not, the steady output state of the pin
would change the voltage on the capacitor and undo the voltage setting established
by <span class="keyword_in_text">PWM</span>. Keep in mind that leakage currents of up to 1 µA can flow into
or out of this "disconnected" pin. Over time, these small currents will cause the
voltage on the capacitor to drift. The same applies for leakage current from an
op-amp's input, as well as the capacitor's own internal leakage. Executing
<span class="keyword_in_text">PWM</span> occasionally will reset the capacitor voltage to the intended value.</p>
<p class="PlainText"><span class="keyword_in_text">PWM</span> charges the capacitor; the load presented by your circuit discharges
it. How long the charge lasts (and therefore how often your program should repeat
the <span class="keyword_in_text">PWM</span> command to refresh the charge) depends on how much current the
circuit draws, and how stable the voltage must be. You may need to buffer <span class="keyword_in_text">PWM</span>output with a simple op-amp follower if your load or stability requirements are
more than the passive circuit can handle. </p>
<p class="PlainText">The term "PWM" applies only loosely to the action of the BASIC Stamp's <span class="keyword_in_text">PWM</span>
command. Most systems that output <span class="keyword_in_text">PWM</span> do so by splitting a fixed period
of time into an on time (1) and an off time (0). Suppose the interval is 1 ms
and the duty cycle is 100 ÷ 256. Conventional <span class="keyword_in_text">PWM</span> would turn the
output on for 0.39 ms and off for 0.61 ms, repeating this process each millisecond.
The main advantage of this kind of <span class="keyword_in_text">PWM</span> is its predictability; you know the
exact frequency of the pulses (in this case, 1 kHz), and their widths are
controlled by the duty cycle. </p>
<p class="PlainText">BASIC Stamp's <span class="keyword_in_text">PWM</span> <![CDATA[ ]]><u>does not work this way</u>. It outputs a rapid
sequence of on/off pulses, as short as 1.6 µs in duration, whose overall proportion
over the course of a full <span class="keyword_in_text">PWM</span> cycle of approximately a millisecond is equal
to the duty cycle. This has the advantage of very quickly zeroing in on the desired
output voltage, but it does not produce the neat, orderly pulses that you might
expect. All BS2 modules also uses this high-speed <span class="keyword_in_text">PWM</span>technique to generate pseudo-sine wave tones with the
<a href="DTMFOUT.htm" target="" title="" alt="" class="MCXref_0">DTMFOUT</a> and <a href="FREQOUT.htm" target="" title="" alt="" class="MCXref_0">FREQOUT</a>
instructions. </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>