One technique I've seen for PWM:
(for 8 bit resolution)
0 to 255 corresponds to 0 to 100% output.
Add your pwm to a byte at each time interval that the loop is ran
If you get an overflow, turn output on, else turn output off.
So, starting from zero and using 50% (128) let's step through a couple of loops.
(in no specific computer language)
byte = 0; pwm = 128
LOOP:
byte = byte + pwm
if overflow the out_pin = 1 else out_pin = 0
...
...
goto LOOP
First pass, byte = 128, output 0
second pass, byte = 0, overflow = 1, output 1
third pass, byte = 128, overflow = 0, output 0
and so on.
The error is the difference between the commanded position and the actual position. |