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
.
Содержание BASIC Stamp 2e
Страница 1: ...BASIC Stamp Programming Manual Version 2 0c...
Страница 30: ...Introduction to the BASIC Stamps Page 28 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 34: ...Quick Start Guide Page 32 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 90: ...BUTTON BASIC Stamp Command Reference Page 88 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 118: ...END BASIC Stamp Command Reference Page 116 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 128: ...FREQOUT BASIC Stamp Command Reference Page 126 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 196: ...NAP BASIC Stamp Command Reference Page 194 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 206: ...OWIN BASIC Stamp Command Reference Page 204 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 214: ...OWOUT BASIC Stamp Command Reference Page 212 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 216: ...PAUSE BASIC Stamp Command Reference Page 214 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 226: ...POLLMODE BASIC Stamp Command Reference Page 224 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 232: ...POLLOUT BASIC Stamp Command Reference Page 230 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 236: ...POLLRUN BASIC Stamp Command Reference Page 234 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 240: ...POLLWAIT BASIC Stamp Command Reference Page 238 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 262: ...RCTIME BASIC Stamp Command Reference Page 260 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 274: ...RUN BASIC Stamp Command Reference Page 272 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 310: ...SEROUT BASIC Stamp Command Reference Page 308 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 324: ...STOP BASIC Stamp Command Reference Page 322 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 340: ...ASCII Chart Page 338 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 342: ...Reserved Words Page 340 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 346: ...Conversion Formatters Page 344 BASIC Stamp Programming Manual 2 0b www parallaxinc com...