Positron Compiler Documentation

PWM

Positron8 · Revision 4.0.6.5 · PDF page 181

PWM Pin, Duty, Cycles

Output pulse-width-modulation on a pin, then return the pin to input state.

Pin is a Port.Pin constant that specifies the I/O pin to use. Duty is a variable, constant (0-255), or expression, which specifies the analogue level desired (0-5 volts). Cycles is a variable or constant (0-255) which specifies the number of cycles to output. Larger capacitors require multiple cycles to fully charge. Cycle time is dependant on Xtal frequency. If a 4MHz crystal is used, then cycle takes approx 5 ms. If a 20MHz crystal is used, then cycle takes approx 1 ms.

PWM can be used to generate analogue voltages (0-5V) through a pin connected to a resistor and capacitor to ground; the resistor-capacitor junction is the analogue output (see circuit). Since the capacitor gradually discharges, PWM should be executed periodically to refresh the

Original diagram or table from Positron8, PDF page 181
Original figure / layout · PDF page 181
Text in this figure or table

To I/O Pin

10uF Output

analogue voltage.

PWM emits a burst of 1s and 0s whose ratio is proportional to the duty 10k

value you specify. If duty is 0, then the pin is continuously low (0); if

duty is 255, then the pin is continuously high. For values in between,Analogue
the proportion is duty/255. For example, if duty is 100, the ratio of 1s toVoltage

0s is 100/255 = 0.392, approximately 39 percent.

When such a burst is used to charge a capacitor arranged, the voltage across the capacitor is equal to:-

(duty / 255) * 5.

So if duty is 100, the capacitor voltage is

(100 / 255) * 5 = 1.96 volts.

Original diagram or table from Positron8, PDF page 181
Original figure / layout · PDF page 181
Text in this figure or table

This voltage will drop as the capaci- 9 Volts 78L05 9 Volts

Regulated 5 Voltstor discharges through whatever load
InIN OUT
GNDR1it is driving. The rate of discharge is

IC2 14 4.7k IC1

VDD 13 proportional to the current drawn by

RB7

412
MCLRRB6

11

RB5 the load; more current = faster dis-

10

4MHzRB4
98
C6CrystalRB3IC3charge. You can reduce this effect in
1682
.1ufOSC1RB2R2-
710- 5 Volts
RB110kLMC662software by refreshing the capaci-
63Out
RB0+
C1C2
PIC16F844
10uf0.1uf3tor's charge with frequent use of the
RA4LED
142
OSC2RA3
C3C41C5PWM command, or you can buffer

RA2

181uf
22pf22pfRA1
17the output using an op-amp to
RA0R3

VSS 470 5 greatly reduce the need for frequent 0v PWM cycles.

See also : HPWM, PulseOut, Servo.

View original PDF page 181Open page in PDFOriginal PDF page 181