Skip to content

Commit e73f40c

Browse files
authored
Fix frequency calculation from prescale
Use Datasheet 7.3.5 Equation 1 to fix frequency calculation from prescale https://cdn-shop.adafruit.com/datasheets/PCA9685.pdf checked by setting chip frequency to 60Hz read prescale = 105 hand calculation returned 57.58 computation in C++ shows same 57.58 value difference between input 60 Hz and reverse calculated frequency of 57.58 is due to limitations of the chip and the 252 possible PWM frequencies. This difference is to be expected.
1 parent 2631ff3 commit e73f40c

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

Adafruit_PWMServoDriver.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,10 @@ void Adafruit_PWMServoDriver::writeMicroseconds(uint8_t num, uint16_t Microsecon
299299
// Read prescale and convert to frequency
300300
double prescale = Adafruit_PWMServoDriver::readPrescale();
301301
prescale += 1;
302-
// Rounding to nearest number is equal to adding 0,5 and floor to nearest number
303-
prescale *= 4096;
304-
prescale -= 2048;
305-
uint32_t freq = FREQUENCY_CALIBRATED;
306-
freq *= prescale; // Calculated PCA9685 chip PWM Frequency
307-
freq /= 0.9; // Correct for overshoot in the frequency setting
308-
302+
uint32_t freq = 25000000; // Chip frequency is 25MHz
303+
freq /= prescale;
304+
freq /= 4096; // 12 bits of resolution
305+
309306
#ifdef ENABLE_DEBUG_OUTPUT
310307
Serial.print(freq); Serial.println(" Calculated PCA9685 chip PWM Frequency");
311308
#endif

0 commit comments

Comments
 (0)