Skip to content

Commit

Permalink
fix #1 + add getFrequency()
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Sep 21, 2020
1 parent 7f95c92 commit fcf73f1
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
10 changes: 6 additions & 4 deletions PCA9685.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -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 <Wire.h>
#include "PCA9685.h"
Expand Down Expand Up @@ -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);
}

Expand Down
6 changes: 4 additions & 2 deletions PCA9685.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -69,6 +70,7 @@ class PCA9685

uint8_t _address;
int _error;
int _freq = 200; // default PWM frequency - P25 datasheet
};

// -- END OF FILE --
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 16 additions & 3 deletions examples/PCA9685_test02/PCA9685_test02.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
//

Expand Down Expand Up @@ -93,17 +93,30 @@ 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();
}


void loop()
{
//testPWM(0);
}

// -- END OF FILE --
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*"
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=PCA9685_RT
version=0.2.1
version=0.2.2
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for I2C PCA9685 16 channel PWM
Expand Down

0 comments on commit fcf73f1

Please sign in to comment.