@@ -120,19 +120,23 @@ void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
120
120
Serial.print (" Attempting to set freq " );
121
121
Serial.println (freq);
122
122
#endif
123
-
124
- freq *= 0.9 ; // Correct for overshoot in the frequency setting (see issue #11).
125
- float prescaleval = 25000000 ;
123
+ // Range output modulation frequency is dependant on oscillator
124
+ if (freq < 1 ) freq = 1 ;
125
+ if (freq > 3500 ) freq = 3500 ; // Datasheet limit is 3052=50MHz/(4*4096)
126
+ /*
127
+ freq *= 0.9; // Correct for overshoot in the frequency setting (see issue #11)
128
+ float prescaleval = FREQUENCY_OSCILLATOR;
129
+ */
130
+ uint32_t prescaleval = FREQUENCY_CALIBRATED;
131
+ prescaleval /= freq; // required output modulation frequency
132
+ // rounding to nearest number is equal to adding 0,5 and floor to nearest number
133
+ prescaleval += 2048 ;
126
134
prescaleval /= 4096 ;
127
- prescaleval /= freq;
128
135
prescaleval -= 1 ;
136
+ if (prescaleval < PCA9685_PRESCALE_MIN) prescaleval = PCA9685_PRESCALE_MIN;
137
+ if (prescaleval > PCA9685_PRESCALE_MAX) prescaleval = PCA9685_PRESCALE_MAX;
138
+ uint8_t prescale = (uint8_t ) prescaleval;
129
139
130
- #ifdef ENABLE_DEBUG_OUTPUT
131
- Serial.print (" Estimated pre-scale: " );
132
- Serial.println (prescaleval);
133
- #endif
134
-
135
- uint8_t prescale = floor (prescaleval + 0.5 );
136
140
#ifdef ENABLE_DEBUG_OUTPUT
137
141
Serial.print (" Final pre-scale: " );
138
142
Serial.println (prescale);
@@ -178,6 +182,15 @@ void Adafruit_PWMServoDriver::setOutputMode(bool totempole) {
178
182
#endif
179
183
}
180
184
185
+ /* !
186
+ * @brief Reads set Prescale from PCA9685
187
+ * @param void
188
+ */
189
+ uint8_t Adafruit_PWMServoDriver::readPrescale (void )
190
+ {
191
+ return read8 (PCA9685_PRESCALE);
192
+ }
193
+
181
194
/* !
182
195
* @brief Gets the PWM output of one of the PCA9685 pins
183
196
* @param num One of the PWM output pins, from 0 to 15
0 commit comments