Parallax BASIC Stamp 2e Programming Manual page 250

Table of Contents

Advertisement

PWM - BASIC Stamp Command Reference
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.
P0
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
3
10k (10 x 10
ohm) resistor and a 1 µF (1 x 10
Charge time = 4 * 10 x 10
Since, on the BS2, each cycle is approximately a millisecond, it would take
at least 40 cycles to charge the capacitor.
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
Page 248 • BASIC Stamp Programming Manual 2.0b • www.parallaxinc.com
+
0.1 uF
Vss
-6
F) capacitor:
3
-6
-3
* 1 x 10
= 40 x 10
seconds, or 40 ms.
Analog Voltage
Assuming the circuit is
F
PWM
ILTERING THE
SIGNAL
Figure 5.27: Example PWM Filter
Circuit.
D
ETERMINING THE APPROPRIATE
CYCLE TIME FOR YOUR CIRCUIT
.
.

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the BASIC Stamp 2e and is the answer not in the manual?

Table of Contents