BASIC stamp manual v2.2

5: BASIC Stamp Command Reference – PWM
BASIC Stamp Syntax and Reference Manual 2.2 www.parallax.com Page 357
state of the pin would change the voltage on the capacitor and undo the
voltage setting established by PWM. Keep in mind that leakage currents
of up to 1 µA can flow into or out of thisdisconnected 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 PWM occasionally will reset
the capacitor voltage to the intended value.
PWM 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 PWM 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 PWM output with a simple op-amp follower if your
load or stability requirements are more than the passive circuit of Figure
5.31 can handle.
The term “PWM” applies only loosely to the action of the BASIC Stamp's
PWM command. Most systems that output PWM 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 / 255. Conventional PWM 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 PWM 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.
BASIC Stamp's PWM does not work this way. 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 PWM 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 models also use this high-speed
PWM to generate pseudo-sine wave tones with the DTMFOUT and
FREQOUT instructions.
Demo Program (PWM.bs1)
' PWM.BS1
' Connect a voltmeter (such as a digital multimeter set to its voltage
' range) to the output of the circuit shown in the figure for the PWM
' command (in the manual). Run the program and observe the readings on
' the meter. They should come very close to 1.96V, then decrease slightly
H
OW PULSE-WIDTH-MODULATION IS
GENERATED
.
1