From b683f992e9d1f31c3d3c54fb00dddb5ee8414c32 Mon Sep 17 00:00:00 2001 From: Bruce Lucas Date: Sat, 30 Dec 2023 09:24:45 -0500 Subject: [PATCH] Correct PWM frequency prescale computation Assuming this datasheet https://www.digikey.be/htmldatasheets/production/1640697/0/0/1/pca9685-datasheet.html#pf19 is correct, the prescale value computation is off by one. Prior to this change a PWM frequency of about 2 kHz could be set, and would be reported. After this change a maximum PWM frequency of 1526 Hz is accepted and reported, consistent with the datasheet. --- adafruit_pca9685.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_pca9685.py b/adafruit_pca9685.py index 1aca414..8e9b211 100755 --- a/adafruit_pca9685.py +++ b/adafruit_pca9685.py @@ -166,11 +166,11 @@ def frequency(self) -> float: raise ValueError( "The device pre_scale register (0xFE) was not read or returned a value < 3" ) - return self.reference_clock_speed / 4096 / prescale_result + return self.reference_clock_speed / 4096 / (prescale_result + 1) @frequency.setter def frequency(self, freq: float) -> None: - prescale = int(self.reference_clock_speed / 4096.0 / freq + 0.5) + prescale = int(self.reference_clock_speed / 4096.0 / freq + 0.5) - 1 if prescale < 3: raise ValueError("PCA9685 cannot output at the given frequency") old_mode = self.mode1_reg # Mode 1