PWM - BASIC Stamp Command Reference
Page 248
•
BASIC Stamp Programming Manual 2.0b
•
www.parallaxinc.com
use this formula: (Duty/255) * 5V. For example, if Duty is 100, (100/255) *
5V = 1.96V; PWM outputs a train of pulses whose average voltage is 1.96V.
In order to convert PWM into an analog voltage we have to filter out the
pulses and store the average voltage. The resistor/capacitor combination
in Figure 5.27 will do the job. The capacitor will hold the voltage set by
PWM 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 PWM
instruction to give the capacitor a fresh charge.
Just as it takes time to discharge a capacitor, it also takes time to charge it
in the first place. The PWM command lets you specify the charging time
in terms of PWM cycles. The period of each cycle is shown in
Table 5.65.
So, on the BS2, to charge a capacitor for 5ms, you would specify 5 cycles in
the PWM instruction.
How do you determine how long to charge a capacitor? Use this rule-of-
thumb formula: Charge time = 4 * R * C. For instance, Figure 5.27 uses a
10k (10 x 10
3
ohm) resistor and a 1 µF (1 x 10
-6
F) capacitor:
Charge time = 4 * 10 x 10
3
* 1 x 10
-6
= 40 x 10
-3
seconds, or 40 ms.
Since, on the BS2, each cycle is approximately a millisecond, it would take
at least 40 cycles to charge the capacitor. Assuming the circuit is
connected to pin 0, here’s the complete PWM instruction:
PWM 0, 100, 40 ' Put a 1.96V charge on capacitor.
After outputting the PWM 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
P0
Analog Voltage
Vss
0.1 uF
+
Figure 5.27: Example PWM Filter
Circuit.
D
ETERMINING THE APPROPRIATE
CYCLE TIME FOR YOUR CIRCUIT
.
F
ILTERING THE
PWM
SIGNAL
.
Summary of Contents for BASIC Stamp 2e
Page 1: ...BASIC Stamp Programming Manual Version 2 0c...
Page 34: ...Quick Start Guide Page 32 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 340: ...ASCII Chart Page 338 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 342: ...Reserved Words Page 340 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 346: ...Conversion Formatters Page 344 BASIC Stamp Programming Manual 2 0b www parallaxinc com...