Specifications

BASIC Stamp II
Parallax, Inc. • BASIC Stamp Programming Manual 1.8 • Page 295
2
How PWM is Generated
The term “PWM” applies only loosely to the action of the BS2’s PWM
instruction. 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, 1kHz), and their widths are controlled by the duty cycle.
BS2 PWM does not work this way. It outputs a rapid sequence of on/
off pulses as short as 4µ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. The BS2 also uses this high-speed PWM
to generate pseudo-sinewave tones with the DTMFout and Freqout
instructions.
Demo Program
Connect a voltmeter (such as a digital multimeter set to its voltage
range) to the output of the circuit shown in figure I-12. Connect BS2
pin 0 to point marked I/O pin. Run the program and observe the read-
ings on the meter. They should come very close to 1.96V, then decrease
slightly as the capacitor discharges. Try varying the interval between
PWM bursts (by changing the Pause value) and the number of PWM
cycles to see their effect.
again:
PWM 0,100,40 ' 40 cycles of PWM at 100/255 duty
pause 1000 ' Wait a second.
goto again ' Repeat.