Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify and improve the writeMicroseconds() calculations #66

Merged
merged 3 commits into from
Nov 3, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions Adafruit_PWMServoDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,24 +296,14 @@ void Adafruit_PWMServoDriver::writeMicroseconds(uint8_t num, uint16_t Microsecon
double pulselength;
pulselength = 1000000; // 1,000,000 us per second

// Read prescale and convert to frequency
// Read prescale
double prescale = Adafruit_PWMServoDriver::readPrescale();
prescale += 1;
uint32_t freq = 25000000; // Chip frequency is 25MHz
freq /= prescale;
freq /= 4096; // 12 bits of resolution

#ifdef ENABLE_DEBUG_OUTPUT
Serial.print(freq); Serial.println(" Calculated PCA9685 chip PWM Frequency");
#endif

pulselength /= freq; // us per period from PCA9685 chip PWM Frequency using prescale reverse frequency calc
Serial.print(prescale); Serial.println(" PCA9685 chip prescale");
photodude marked this conversation as resolved.
Show resolved Hide resolved

#ifdef ENABLE_DEBUG_OUTPUT
Serial.print(pulselength); Serial.println(" us per period");
#endif

pulselength /= 4096; // 12 bits of resolution
// Calculate the pulse for PWM based on Equation 1 from the datasheet section 7.3.5
prescale += 1;
pulselength *= prescale;
pulselength /= FREQUENCY_CALIBRATED;

#ifdef ENABLE_DEBUG_OUTPUT
Serial.print(pulselength); Serial.println(" us per bit");
Expand Down