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

Commits on Oct 29, 2019

  1. Simplify and improve the writeMicroseconds() calculations

    Checked the calculations by hand
    
    Use Datasheet 7.3.5 Equation 1 for frequency from prescale
    https://cdn-shop.adafruit.com/datasheets/PCA9685.pdf
    Frequency = (ClockFrequency/(PreScale+1))*(1/BitRate)
    and from the rest of the calculation
    us per period = PulseLength/Frequency
    us per bit = us per period/BitRate
     we can combine to get
    us per bit = (PulseLength/((ClockFrequency/(PreScale+1))*(1/BitRate)))*(1/BitRate)
    
    This can be simplified to (PulseLength (PreScale + 1))/ClockFrequency 
    this formula was implemented in proposed code
    
    checked by setting chip frequency to 60Hz
    read prescale = 105
    us per bit = 4.069 with 25Mhz (a PWM frequency of about 57.3hz)
    Choosing the FREQUENCY_CALIBRATED constant listed at 104.3% higher or 26.075MHz
    we get 
    us per bit = 4.1035 or a PWM frequency of about 60.056Hz much closer to the set 60Hz 
    
    Overall this change makes less calculation steps, and uses fewer variables. The change should be faster and more accurate to the intended results.
    photodude committed Oct 29, 2019
    Configuration menu
    Copy the full SHA
    d7e0ec9 View commit details
    Browse the repository at this point in the history
  2. Wrap print statement in #ifdef ENABLE_DEBUG_OUTPUT

    debug statements should be Wrapped in `#ifdef ENABLE_DEBUG_OUTPUT`
    photodude committed Oct 29, 2019
    Configuration menu
    Copy the full SHA
    942f457 View commit details
    Browse the repository at this point in the history
  3. Change prescale from double to uint16_t

    Prescale values are integers 3 to 255 with a maximum of 255+1 for the calculations.
    photodude committed Oct 29, 2019
    Configuration menu
    Copy the full SHA
    33ec0da View commit details
    Browse the repository at this point in the history