arduino-libraries / Servo

Servo Library for Arduino
http://arduino.cc/
GNU Lesser General Public License v2.1
233 stars 251 forks source link

Selectable timer prescaler #118

Open SuperTankMan opened 8 months ago

SuperTankMan commented 8 months ago

Hi

I noticed that the servo library uses 8 as the prescaler to the timer from the system clock in the code. This then allows Servo.writemicroseconds() function to request pulses width to be in microseconds(uSec) which means the best resolution you can get from a servo is to the nearest 1 uSec.

I would like to raise a feature request to select the prescaler to either 8, 4, 2 or 1 which would then automatically allow best resolution you get from a Servo.writeMicroseconds() to 1uSec, 0.5uSec, 0.25uSec and 0.125uSec respectively.

This obviously means that: 1) When prescaler is 8 (default) the Servo.writeMicroseconds() will take values approx 1000 to 2000 where 1500 is the 90 degrees angle and each increment in Servo.writeMicroseconds() mean an increase in pulse width of 1uSec and Servo.write() means an increase in 1deg for each increment. This is the current default setting.

2) When prescaler is 4 the Servo.writeMicroseconds() will take values approx 2000 to 4000 where 3000 is the 90 degrees angle and each increment in writeMicroseconds() mean an increase in pulse width of 0.5uSec and Servo.write() means an increase in 0.5deg for each increment.

3) When prescaler is 2 the servo.writeMicroseconds() will take values approx 4000 to 8000 where 6000 is the 90 degrees angle and each increment in writeMicroseconds() mean an increase in pulse width of 0.25uSec and Servo.write() means an increase in 0.25deg for each increment.

4) When prescaler is 1 the servo.writeMicroseconds() will take values approx 8000 to 16000 where 12000 is the 90 degrees angle and each increment in writeMicroseconds() mean an increase in pulse width of 0.125uSec and Servo.write() means an increase in 0.125deg for each increment.

Once the prescaler is selected by for example: uint8_t Servo.attach(int pin, int prescaler); // Same as attach(int pin) but with additional prescaler selection It may be that the prescaler can not be selected for each servo but all servos have will have to use the same prescaler.

Any values used in Servo.writeMicroseconds() and Servo.write() will then be automatically be scaled to correct values depending on the prescaler.

Please someone add/raise an additional feature request for this or similar function. I would not mind doing this but I have very little experience in Atmega328 hardware and registers and internal working of the Servo library. perhaps someone can guide me but I think by the time they guide me someone may be able to implement this feature....