Specifications
BASIC Stamp II
Page 294 • BASIC Stamp Programming Manual 1.8 • Parallax, Inc.
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 instruction lets you specify the charging
time in terms of PWM cycles. Each cycle is a period of approximately
1ms. So 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 I-12
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 40ms. Since 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 BS2 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 PWM.
PWM charges the capacitor; the load presented by your circuit dis-
charges it. How long the charge lasts (and therefore how often your
program should repeat the PWM instruction to refresh the charge) de-
pends on how much current the circuit draws, and how stable the volt-
age 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 I-12 can handle.
I/O pin
10k
1µF
Analog voltage
(0—5Vdc)
+
Figure I-12










