Servo PWM - LDmicro/LDmicro GitHub Wiki
Discussion 1: SG90
Discussion 2: Can't set pwm
SG90.pdf
The SG90 servo requires a time-based pulse with a period of 20 ms (50 Hz) and
the workspace is from 1 ms (-90 degrees) to 2 ms (+90 degrees).
There are two complexity:
- PWM frequency. It may be too slow.
- PWM resolution. Workspace is 2 ms - 1 ms = 1 ms. The short workspace reduces PWM resolution at 20 times (20 ms / 1 ms).
1. AVR
-
The AVR MCU can generate only a few PWM frequencies depending on Fclk, the selected resolution (TOP+1) and the prescaler.
LDmicro will choose the closest achievable frequency; if the error is large (>5%) then it will warn you.
You can calculate or mesure the actual PWM frequency or set the knowingly small (large) PWM frequency to get a warning message with achievable frequencies.
Thus, we can accept a PWM frequency of 61 Hz or
we can use an external generator of 50x1024x256=13107200 Hz instead of 16 MHz and
get hard 50 Hz PWM frequency. -
Depending on the timer resolution (8 or 16 bits), the PWM will have 8 bit or 8/9/10 bit resolution.
Timers 0 and 2 are usually 8 bit.
Timers 1,3,4,5 are usually 16 bit.
Use PWM based on timers 1,3,4 or 5 to give a high resolution.
Use 10-bit PWM resolution to get
61 Hz PWM frequency with
62 motion steps from -90 to 90 degrees
with 2,88 degrees per step.
-90 degrees, 1 ms, duty 61
+90 degrees, 2 ms, duty 124
0 degrees, 1.5 ms, duty 93
LDmicro v4.3.6 tested in Proteus.
servo pwm.zip
2. PIC
To get a PWM frequency of 50 Hz, you must set the MCU crystal frequency less than 819 kHz.
0 degrees, 1.5 ms, duty 38
servo pwm PIC16F887.zip
Any questions to [email protected]
Best regards.
Ihor Nehrutsa.