Skip to content

Commit

Permalink
add channelCount (#13)
Browse files Browse the repository at this point in the history
* add channelCount, documentation, update examples
  • Loading branch information
RobTillaart authored Jan 6, 2022
1 parent bb58e85 commit b8cd667
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 20 deletions.
14 changes: 8 additions & 6 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.3.3
// VERSION: 0.3.4
// PURPOSE: Arduino library for I2C PCA9685 16 channel PWM
// URL: https://github.com/RobTillaart/PCA9685_RT
//
Expand All @@ -17,6 +17,7 @@
// 0.3.1 2021-01-05 Arduino-CI + unit test
// 0.3.2 2021-01-14 WireN support
// 0.3.3 2021-12-23 update library.json, license, readme, minor edits
// 0.3.4 2022-01-03 add channelCount()


#include "PCA9685.h"
Expand Down Expand Up @@ -145,7 +146,7 @@ uint8_t PCA9685::readMode(uint8_t reg)
void PCA9685::setPWM(uint8_t channel, uint16_t onTime, uint16_t offTime)
{
_error = PCA9685_OK;
if (channel > 15)
if (channel >= _channelCount)
{
_error = PCA9685_ERR_CHANNEL;
return;
Expand All @@ -167,7 +168,7 @@ void PCA9685::setPWM(uint8_t channel, uint16_t offTime)
void PCA9685::getPWM(uint8_t channel, uint16_t* onTime, uint16_t* offTime)
{
_error = PCA9685_OK;
if (channel > 15)
if (channel >= _channelCount)
{
_error = PCA9685_ERR_CHANNEL;
return;
Expand All @@ -193,8 +194,9 @@ void PCA9685::setFrequency(uint16_t freq, int offset)
{
_error = PCA9685_OK;
_freq = freq;
if (_freq < 24) _freq = 24; // page 25 datasheet
if (_freq > 1526) _freq = 1526;
// limits frequency see page 25 datasheet
if (_freq < PCA9685_MIN_FREQ) _freq = PCA9685_MIN_FREQ;
if (_freq > PCA9685_MAX_FREQ) _freq = PCA9685_MAX_FREQ;
// removed float operation for speed
// faster but equal accurate
// uint8_t scaler = round(25e6 / (_freq * 4096)) - 1;
Expand Down Expand Up @@ -226,7 +228,7 @@ int PCA9685::getFrequency(bool cache)
void PCA9685::digitalWrite(uint8_t channel, uint8_t mode)
{
_error = PCA9685_OK;
if (channel > 15)
if (channel >= _channelCount)
{
_error = PCA9685_ERR_CHANNEL;
return;
Expand Down
13 changes: 10 additions & 3 deletions PCA9685.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once
//
// FILE: PCA9685.H
// FILE: PCA9685.h
// AUTHOR: Rob Tillaart
// DATE: 24-apr-2016
// VERSION: 0.3.3
// VERSION: 0.3.4
// PURPOSE: Arduino library for I2C PCA9685 16 channel PWM
// URL: https://github.com/RobTillaart/PCA9685_RT

Expand All @@ -12,7 +12,7 @@
#include "Wire.h"


#define PCA9685_LIB_VERSION (F("0.3.3"))
#define PCA9685_LIB_VERSION (F("0.3.4"))

// ERROR CODES
#define PCA9685_OK 0x00
Expand All @@ -21,6 +21,10 @@
#define PCA9685_ERR_MODE 0xFD
#define PCA9685_ERR_I2C 0xFC

// get/setFrequency()
#define PCA9685_MIN_FREQ 24
#define PCA9685_MAX_FREQ 1526


class PCA9685
{
Expand All @@ -34,6 +38,8 @@ class PCA9685
void reset();
bool isConnected();

uint8_t channelCount() { return _channelCount; };

// reg = 1, 2 check datasheet for values
void writeMode(uint8_t reg, uint8_t value);
uint8_t readMode(uint8_t reg);
Expand Down Expand Up @@ -78,6 +84,7 @@ class PCA9685
uint8_t _address;
int _error;
int _freq = 200; // default PWM frequency - P25 datasheet
uint8_t _channelCount = 16;

TwoWire* _wire;
};
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Lower frequencies do better than higher frequencies.
Library does not support multiple Wire instances (yet).
- **void reset()** resets the library to start up conditions.
- **bool isConnected()** checks if address is available on I2C bus.
- **uint8_t channelCount()** returns the number of channels = 16.


### Mode
Expand Down
2 changes: 1 addition & 1 deletion examples/PCA9685_allOFF_test/PCA9685_allOFF_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void setup()
Serial.println();

pinMode(PIN, INPUT_PULLUP);
for (int channel = 0; channel < 16; channel++)
for (int channel = 0; channel < PCA.channelCount(); channel++)
{
PCA.setPWM(channel, 0, 1000);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// to see the frequency of the PWM


#include "Arduino.h"
#include "Wire.h"
#include "PCA9685.h"


Expand Down
2 changes: 2 additions & 0 deletions examples/PCA9685_maxPWM_test/PCA9685_maxPWM_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// to see the frequency of the PWM


#include "Arduino.h"
#include "Wire.h"
#include "PCA9685.h"


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// Note: the higher the frequency, the more inaccurate the real frequency,


#include "Arduino.h"
#include "Wire.h"
#include "PCA9685.h"


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// to see the frequency of the PWM


#include "Arduino.h"
#include "Wire.h"
#include "PCA9685.h"


Expand All @@ -35,8 +37,8 @@ void setup()
pinMode(IRQ_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(IRQ_PIN), irq, RISING); // CHANGE

PCA.setFrequency(24);
PCA.setPWM(15, 0, 4095); // gives 2 changes per interval
PCA.setFrequency(PCA9685_MIN_FREQ); // == 24
PCA.setPWM(15, 0, 4095); // gives 2 changes per interval

Serial.println("\nSET\tGET\tGET%\tIRQ\tIRQ%");
}
Expand Down Expand Up @@ -73,7 +75,7 @@ void loop()
Serial.print("\n");

freq += 4;
if (freq >= 1526) freq = 24;
if (freq >= 1526) freq = 24; // page 25 datasheet; PCA9685_MAX_FREQ, PCA9685_MIN_FREQ
PCA.setFrequency(freq);
PCA.setPWM(15, 0, 4095);

Expand Down
8 changes: 5 additions & 3 deletions examples/PCA9685_test01/PCA9685_test01.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// PUPROSE: test PCA9685 library


#include "Arduino.h"
#include "Wire.h"
#include "PCA9685.h"


Expand Down Expand Up @@ -36,7 +38,7 @@ void testSetON()
Serial.print(millis());
Serial.print("\t");
Serial.println("Test - setHIGH");
for (uint8_t channel = 0; channel < 16; channel++)
for (uint8_t channel = 0; channel < ledArray.channelCount(); channel++)
{
ledArray.digitalWrite(channel, HIGH);
}
Expand All @@ -48,7 +50,7 @@ void testSetOFF()
Serial.print(millis());
Serial.print("\t");
Serial.println("Test - setLOW");
for (uint8_t channel = 0; channel < 16; channel++)
for (uint8_t channel = 0; channel < ledArray.channelCount(); channel++)
{
ledArray.digitalWrite(channel, LOW);
}
Expand All @@ -60,7 +62,7 @@ void testPWMMode()
Serial.print(millis());
Serial.print("\t");
Serial.println("Test - setPwm getPWM");
for (uint16_t channel = 0; channel < 16; channel++)
for (uint16_t channel = 0; channel < ledArray.channelCount(); channel++)
{
// every next line ~twice as much time
ledArray.setPWM(channel, channel * 127, channel * 255);
Expand Down
7 changes: 5 additions & 2 deletions examples/PCA9685_test02/PCA9685_test02.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
// PUPROSE: test PCA9685 library


#include "Arduino.h"
#include "Wire.h"
#include "PCA9685.h"


PCA9685 ledArray(0x40);


Expand Down Expand Up @@ -39,7 +42,7 @@ void testDigitalWrite(uint8_t mode)
Serial.print(millis());
Serial.print("\t");
Serial.println(__FUNCTION__);
for (int channel = 0; channel < 16; channel++)
for (int channel = 0; channel < ledArray.channelCount(); channel++)
{
ledArray.digitalWrite(channel, mode);
delay(100);
Expand Down Expand Up @@ -73,7 +76,7 @@ void testPWMMode()
Serial.print(millis());
Serial.print("\t");
Serial.println(__FUNCTION__);
for (uint16_t channel = 1; channel < 16; channel++)
for (uint16_t channel = 1; channel < ledArray.channelCount(); channel++)
{
// every next line ~twice as much time
ledArray.setPWM(channel, channel * 127, channel * 255);
Expand Down
3 changes: 3 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ lastError KEYWORD2

# Constants (LITERAL1)
PCA9685_LIB_VERSION LITERAL1

PCA9685_OK LITERAL1
PCA9685_ERROR LITERAL1
PCA9685_ERR_CHANNEL LITERAL1
PCA9685_ERR_MODE LITERAL1
PCA9685_ERR_I2C LITERAL1

PCA9685_MIN_FREQ LITERAL1
PCA9685_MAX_FREQ LITERAL1

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.3.3",
"version": "0.3.4",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
Expand Down
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.3.3
version=0.3.4
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
2 changes: 2 additions & 0 deletions test/unit_test_001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

#include <ArduinoUnitTests.h>

#include "Arduino.h"
#include "Wire.h"
#include "PCA9685.h"


Expand Down

0 comments on commit b8cd667

Please sign in to comment.