
To output square waves of variable length and pulse widths, one can use the PWM command.
PWM requires three arguments:
To make this clear: If you want to generate square waves with a frequency of 1µs, call PWM with 1 and 1 as second and third argument. Unfortunately, the pulse width for an 1µs wave is not adjustable because this is the highest possible frequency. To generate waves that have a period of exactly 1s, but a very small pulse of 1µs, call PWM with 199998 as second and 2 as third argument, and so on...
Pin 5, 6, 7, 9, 10, 11 can be used as PWM outputs simultaneously. Since all outputs share a common timer, the frequency of each signal must be equal to the the others. To follow this rule, simply make sure that the sum of the last two arguments of the PWM command is the same for all outputs.
The following example slowly moves a hobbyist RC servo (Compatible to a S03NXF Std. Servo) from one end to the other and vice versa:
outmode -2
lab:
print "+"
for p = 1900 to 4500 step 10
pwm 7, 40000, p
sleep 50
next
print "-"
for p = 4500 to 1900 step -10
pwm 7, 40000, p
sleep 50
next
goto lab
Please note: The PWM command immediately changes pin configuration for the selected ports. No explicit initialization is required.