diff --git a/PCA9685.cpp b/PCA9685.cpp index 16c1a8b..919a3e1 100644 --- a/PCA9685.cpp +++ b/PCA9685.cpp @@ -2,7 +2,7 @@ // FILE: PCA9685.cpp // AUTHOR: Rob Tillaart // DATE: 24-apr-2016 -// VERSION: 0.2.1 +// VERSION: 0.2.2 // PURPOSE: Arduino library for I2C PCA9685 16 channel PWM // URL: https://github.com/RobTillaart/PCA9685_RT // @@ -11,6 +11,7 @@ // 0.1.1 2019-01-30 testing && fixing // 0.2.0 2020-05-25 refactor; ESP32 begin(sda,scl) // 0.2.1 2020-06-19 fix library.json +// 0.2.2 2020-09-21 fix #1 + add getFrequency() #include #include "PCA9685.h" @@ -152,12 +153,13 @@ void PCA9685::getPWM(uint8_t channel, uint16_t* onTime, uint16_t* offTime) // set update frequency for all channels void PCA9685::setFrequency(uint16_t freq) { - if (freq < 24) freq = 24; - if (freq > 1526) freq = 1526; + _freq = freq; + if (_freq < 24) _freq = 24; // page 25 datasheet + if (_freq > 1526) _freq = 1526; // removed float operation for speed // faster but equal accurate // uint8_t scaler = round(25e6 / (freq * 4096)) - 1; - uint8_t scaler = 28828 / (freq * 8) - 1; + uint8_t scaler = 48828 / (_freq * 8) - 1; writeReg(PCA9685_PRE_SCALE, scaler); } diff --git a/PCA9685.h b/PCA9685.h index 93bf83d..c058900 100644 --- a/PCA9685.h +++ b/PCA9685.h @@ -3,7 +3,7 @@ // FILE: PCA9685.H // AUTHOR: Rob Tillaart // DATE: 24-apr-2016 -// VERSION: 0.2.1 +// VERSION: 0.2.2 // PURPOSE: Arduino library for I2C PCA9685 16 channel PWM // URL: https://github.com/RobTillaart/PCA9685_RT // @@ -13,7 +13,7 @@ #include "Arduino.h" -#define PCA9685_LIB_VERSION "0.2.1" +#define PCA9685_LIB_VERSION "0.2.2" // ERROR CODES #define PCA9685_OK 0x00 @@ -50,6 +50,7 @@ class PCA9685 // set update frequency for all channels // freq = 24 - 1526 Hz void setFrequency(uint16_t freq); + int getFrequency() { return _freq; }; // set channel HIGH or LOW (effectively no PWM) void digitalWrite(uint8_t channel, uint8_t mode); @@ -69,6 +70,7 @@ class PCA9685 uint8_t _address; int _error; + int _freq = 200; // default PWM frequency - P25 datasheet }; // -- END OF FILE -- diff --git a/README.md b/README.md index 302fa78..9333d06 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,10 @@ channels do not need to start at the same moment with HIGH. **setFrequency(freq)** set the update speed of the channels. This is for all channels at once. +The frequency is constrained to be between 24 and 1526 Hz, + +**getFrequency()** get the current update frequency of the channels. +This is same for all channels. **digitalWrite(channel, mode)** mode = HIGH or LOW, just use the PCA9685 as a digitalpin. diff --git a/examples/PCA9685_test02/PCA9685_test02.ino b/examples/PCA9685_test02/PCA9685_test02.ino index 6d0ce6a..5d366fc 100644 --- a/examples/PCA9685_test02/PCA9685_test02.ino +++ b/examples/PCA9685_test02/PCA9685_test02.ino @@ -2,7 +2,7 @@ // FILE: PCA9685_test02.ino // AUTHOR: Rob Tillaart // DATE: 24-APR-2016 -// VERSION: 0.1.0 +// VERSION: 0.1.1 // PUPROSE: test PCA9685 library // @@ -93,13 +93,24 @@ void testFrequency() Serial.println(__FUNCTION__); ledArray.setPWM(0, 1000, 3000); - for (uint16_t freq = 24; freq < 2000; freq *= 2) + for (uint16_t freq = 12; freq < 2000; freq *= 2) { - Serial.println(freq); + Serial.print(freq); ledArray.setFrequency(freq); + // if freq is out of range => report + if (ledArray.getFrequency() != freq) + { + Serial.print("\tconstrained to : "); + Serial.println(ledArray.getFrequency()); + } + else + { + Serial.println("\tOK"); + } delay(2000); } ledArray.setOFF(0); + Serial.println(); } @@ -107,3 +118,5 @@ void loop() { //testPWM(0); } + +// -- END OF FILE -- diff --git a/library.json b/library.json index 03565c4..caad882 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/PCA9685_RT.git" }, - "version": "0.2.1", + "version": "0.2.2", "frameworks": "arduino", "platforms": "*" } diff --git a/library.properties b/library.properties index b82f588..5e746dd 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=PCA9685_RT -version=0.2.1 +version=0.2.2 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for I2C PCA9685 16 channel PWM