mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.2.0 PCA9553
This commit is contained in:
parent
67d3e8735f
commit
4ea091d924
@ -6,6 +6,24 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
|
||||
## [0.2.0] - 2023-07-17
|
||||
- refactor interface (breaking)
|
||||
- for compatibility with PCA9552, PCA9551
|
||||
- add **uint8_t getAddress()**
|
||||
- add **void digitalWrite(led, value)**
|
||||
- add **uint8_t digitalRead(led)**
|
||||
- add **reset()**
|
||||
- rename setLEDSource to **setOutputMode()**
|
||||
- rename channelCount to **outputCount()**
|
||||
- add #defines for modi
|
||||
- add some error handling
|
||||
- add examples
|
||||
- update readme.md
|
||||
- internal refactor
|
||||
|
||||
|
||||
----
|
||||
|
||||
## [0.1.0] - 2023-07-16
|
||||
- initial version
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
// FILE: PCA9553.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2023-07-16
|
||||
// VERSION: 0.1.0
|
||||
// VERSION: 0.2.0
|
||||
// PURPOSE: Arduino library for for I2C PCA9553 4 channel PWM
|
||||
// URL: https://github.com/RobTillaart/PCA9553
|
||||
|
||||
@ -18,6 +18,8 @@ PCA9553::PCA9553(const uint8_t deviceAddress, TwoWire *wire)
|
||||
{
|
||||
_address = deviceAddress;
|
||||
_wire = wire;
|
||||
_outputCount = 4;
|
||||
_error = PCA9553_OK;
|
||||
}
|
||||
|
||||
|
||||
@ -53,9 +55,30 @@ bool PCA9553::isConnected()
|
||||
}
|
||||
|
||||
|
||||
uint8_t PCA9553::channelCount()
|
||||
uint8_t PCA9553::reset()
|
||||
{
|
||||
return 4;
|
||||
// not most efficient
|
||||
setPrescaler(0, 0); // 44 Hz
|
||||
setPrescaler(1, 0); // 44 Hz
|
||||
setPWM(0, 128); // 50%
|
||||
setPWM(1, 128); // 50%
|
||||
for (int pin = 0; pin < _outputCount; pin++)
|
||||
{
|
||||
setOutputMode(pin, 0); // LOW
|
||||
}
|
||||
return PCA9553_OK;
|
||||
}
|
||||
|
||||
|
||||
uint8_t PCA9553::getAddress()
|
||||
{
|
||||
return _address;
|
||||
}
|
||||
|
||||
|
||||
uint8_t PCA9553::outputCount()
|
||||
{
|
||||
return _outputCount;
|
||||
}
|
||||
|
||||
|
||||
@ -69,31 +92,42 @@ uint8_t PCA9553::getInput()
|
||||
}
|
||||
|
||||
|
||||
void PCA9553::pinMode(uint8_t pin, uint8_t mode)
|
||||
{
|
||||
if (mode != OUTPUT) setOutputMode(pin, PCA9553_MODE_HIGH);
|
||||
}
|
||||
|
||||
|
||||
void PCA9553::digitalWrite(uint8_t pin, uint8_t val)
|
||||
{
|
||||
if (val == LOW) setOutputMode(pin, PCA9553_MODE_LOW);
|
||||
else setOutputMode(pin, PCA9553_MODE_HIGH);
|
||||
}
|
||||
|
||||
|
||||
uint8_t PCA9553::digitalRead(uint8_t pin)
|
||||
{
|
||||
uint8_t value = readReg(PCA9553_INPUT);
|
||||
if ((value >> pin) & 0x01) return HIGH;
|
||||
return LOW;
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
//
|
||||
// PRESCALERS
|
||||
//
|
||||
void PCA9553::setPrescaler0(uint8_t psc)
|
||||
void PCA9553::setPrescaler(uint8_t gen, uint8_t psc)
|
||||
{
|
||||
writeReg(PCA9553_PSC0, psc);
|
||||
if (gen == 0) writeReg(PCA9553_PSC0, psc);
|
||||
else writeReg(PCA9553_PSC1, psc);
|
||||
}
|
||||
|
||||
|
||||
uint8_t PCA9553::getPrescaler0()
|
||||
uint8_t PCA9553::getPrescaler(uint8_t gen)
|
||||
{
|
||||
return readReg(PCA9553_PSC0);
|
||||
}
|
||||
|
||||
|
||||
void PCA9553::setPrescaler1(uint8_t psc)
|
||||
{
|
||||
writeReg(PCA9553_PSC1, psc);
|
||||
}
|
||||
|
||||
|
||||
uint8_t PCA9553::getPrescaler1()
|
||||
{
|
||||
return readReg(PCA9553_PSC1);
|
||||
if (gen == 0) return readReg(PCA9553_PSC0);
|
||||
else return readReg(PCA9553_PSC1);
|
||||
}
|
||||
|
||||
|
||||
@ -101,59 +135,70 @@ uint8_t PCA9553::getPrescaler1()
|
||||
//
|
||||
// PWM
|
||||
//
|
||||
void PCA9553::setPWM0(uint8_t pwm)
|
||||
void PCA9553::setPWM(uint8_t gen, uint8_t pwm)
|
||||
{
|
||||
writeReg(PCA9553_PWM0, pwm);
|
||||
if (gen == 0) writeReg(PCA9553_PWM0, pwm);
|
||||
else writeReg(PCA9553_PWM1, pwm);
|
||||
}
|
||||
|
||||
|
||||
uint8_t PCA9553::getPWM0()
|
||||
uint8_t PCA9553::getPWM(uint8_t gen)
|
||||
{
|
||||
return readReg(PCA9553_PWM0);
|
||||
}
|
||||
|
||||
|
||||
void PCA9553::setPWM1(uint8_t pwm)
|
||||
{
|
||||
writeReg(PCA9553_PWM1, pwm);
|
||||
}
|
||||
|
||||
|
||||
uint8_t PCA9553::getPWM1()
|
||||
{
|
||||
return readReg(PCA9553_PWM1);
|
||||
if (gen == 0) return readReg(PCA9553_PWM0);
|
||||
else return readReg(PCA9553_PWM1);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
//
|
||||
// LED SOURCE SELECTOR
|
||||
// OUTPUT MODE - MUX SELECTION
|
||||
//
|
||||
bool PCA9553::setLEDSource(uint8_t led, uint8_t source)
|
||||
uint8_t PCA9553::setOutputMode(uint8_t pin, uint8_t mode)
|
||||
{
|
||||
if (led > 3) return false;
|
||||
if (source > 3) return false;
|
||||
if (pin >= _outputCount)
|
||||
{
|
||||
_error = PCA9553_ERR_CHAN;
|
||||
return _error;
|
||||
}
|
||||
if (mode > 3)
|
||||
{
|
||||
_error = PCA9553_ERR_MODE;
|
||||
return _error;
|
||||
}
|
||||
|
||||
uint8_t val = source << (led * 2);
|
||||
uint8_t ledSelect = readReg(PCA9553_LS0);
|
||||
ledSelect &= ~(0x03 << (led * 2));
|
||||
ledSelect |= val;
|
||||
uint8_t reg = PCA9553_LS0;
|
||||
uint8_t ledSelect = readReg(reg);
|
||||
ledSelect &= ~(0x03 << (pin * 2));
|
||||
ledSelect |= (mode << (pin * 2));
|
||||
|
||||
writeReg(PCA9553_LS0, ledSelect);
|
||||
return true;
|
||||
return writeReg(reg, ledSelect);
|
||||
}
|
||||
|
||||
|
||||
uint8_t PCA9553::getLEDSource(uint8_t led)
|
||||
uint8_t PCA9553::getOutputMode(uint8_t pin)
|
||||
{
|
||||
if (led > 3) return PCA9553_ERROR;
|
||||
if (pin >= _outputCount)
|
||||
{
|
||||
_error = PCA9553_ERR_CHAN;
|
||||
return _error;
|
||||
}
|
||||
|
||||
uint8_t ledSelect = readReg(PCA9553_LS0);
|
||||
uint8_t source = (ledSelect >> (led * 2)) & 0x03;
|
||||
return source;
|
||||
uint8_t reg = PCA9553_LS0;
|
||||
uint8_t ledSelect = readReg(reg);
|
||||
uint8_t mode = (ledSelect >> (pin * 2)) & 0x03;
|
||||
return mode;
|
||||
}
|
||||
|
||||
|
||||
uint8_t PCA9553::getLastError()
|
||||
{
|
||||
uint8_t e = _error;
|
||||
_error = PCA9553_OK;
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
//
|
||||
// PRIVATE
|
||||
@ -166,7 +211,7 @@ uint8_t PCA9553::writeReg(uint8_t reg, uint8_t value)
|
||||
_error = _wire->endTransmission();
|
||||
|
||||
if (_error == 0) _error = PCA9553_OK;
|
||||
else _error = PCA9553_ERROR;
|
||||
else _error = PCA9553_ERR_I2C;
|
||||
return _error;
|
||||
}
|
||||
|
||||
@ -176,10 +221,15 @@ uint8_t PCA9553::readReg(uint8_t reg)
|
||||
_wire->beginTransmission(_address);
|
||||
_wire->write(reg);
|
||||
_error = _wire->endTransmission();
|
||||
|
||||
if (_error == 0) _error = PCA9553_OK;
|
||||
else
|
||||
{
|
||||
_error = PCA9553_ERR_I2C;
|
||||
return 0;
|
||||
}
|
||||
if (_wire->requestFrom(_address, (uint8_t)1) != 1)
|
||||
{
|
||||
_error = PCA9553_ERROR;
|
||||
_error = PCA9553_ERR_I2C;
|
||||
return 0;
|
||||
}
|
||||
_error = PCA9553_OK;
|
||||
|
@ -3,7 +3,7 @@
|
||||
// FILE: PCA9553.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2023-07-16
|
||||
// VERSION: 0.1.0
|
||||
// VERSION: 0.2.0
|
||||
// PUPROSE: Arduino library for for I2C PCA9553 4 channel PWM
|
||||
// URL: https://github.com/RobTillaart/PCA9553
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
#include "Wire.h"
|
||||
|
||||
|
||||
#define PCA9553_LIB_VERSION (F("0.1.0"))
|
||||
#define PCA9553_LIB_VERSION (F("0.2.0"))
|
||||
|
||||
|
||||
// REGISTERS
|
||||
@ -24,11 +24,14 @@
|
||||
#define PCA9553_LS0 0x05
|
||||
|
||||
|
||||
// LED SOURCE MASKS
|
||||
// TODO
|
||||
// MUX OUTPUT MODES
|
||||
#define PCA9553_MODE_LOW 0
|
||||
#define PCA9553_MODE_HIGH 1
|
||||
#define PCA9553_MODE_PWM0 2
|
||||
#define PCA9553_MODE_PWM1 3
|
||||
|
||||
|
||||
// ERROR CODES
|
||||
// ERROR CODES (not all used yet)
|
||||
#define PCA9553_OK 0x00
|
||||
#define PCA9553_ERROR 0xFF
|
||||
#define PCA9553_ERR_WRITE 0xFE
|
||||
@ -52,39 +55,38 @@ public:
|
||||
#endif
|
||||
bool begin();
|
||||
bool isConnected();
|
||||
uint8_t reset();
|
||||
|
||||
uint8_t channelCount();
|
||||
uint8_t getAddress();
|
||||
uint8_t outputCount();
|
||||
|
||||
// input register, only lower 4 bits
|
||||
// GPIO
|
||||
uint8_t getInput();
|
||||
|
||||
void pinMode(uint8_t pin, uint8_t mode);
|
||||
void digitalWrite(uint8_t pin, uint8_t value);
|
||||
uint8_t digitalRead(uint8_t pin);
|
||||
|
||||
// PRESCALERS
|
||||
void setPrescaler0(uint8_t psc = 255);
|
||||
uint8_t getPrescaler0();
|
||||
|
||||
void setPrescaler1(uint8_t psc = 255);
|
||||
uint8_t getPrescaler1();
|
||||
|
||||
void setPrescaler(uint8_t generator, uint8_t prescaler = 0);
|
||||
uint8_t getPrescaler(uint8_t generator);
|
||||
|
||||
// PWM
|
||||
void setPWM0(uint8_t pwm = 128);
|
||||
uint8_t getPWM0();
|
||||
|
||||
void setPWM1(uint8_t pwm = 128);
|
||||
uint8_t getPWM1();
|
||||
void setPWM(uint8_t generator, uint8_t pwm = 128);
|
||||
uint8_t getPWM(uint8_t generator);
|
||||
|
||||
|
||||
// LED SOURCE SELECTOR
|
||||
// led = 0..3
|
||||
// OUTPUT MODE - MUX SELECTION
|
||||
// pin: 0..3
|
||||
// mode:
|
||||
// 0 = output is set LOW (LED on)
|
||||
// 1 = output is set high-impedance (LED off; default)
|
||||
// 2 = output blinks at PWM0 rate
|
||||
// 3 = output blinks at PWM1 rate
|
||||
uint8_t setOutputMode(uint8_t pin, uint8_t mode);
|
||||
uint8_t getOutputMode(uint8_t pin);
|
||||
|
||||
// source:
|
||||
// 00 = output is set LOW (LED on)
|
||||
// 01 = output is set high-impedance (LED off; default)
|
||||
// 10 = output blinks at PWM0 rate
|
||||
// 11 = output blinks at PWM1 rate
|
||||
bool setLEDSource(uint8_t led, uint8_t source);
|
||||
uint8_t getLEDSource(uint8_t led);
|
||||
// ERROR
|
||||
uint8_t getLastError();
|
||||
|
||||
|
||||
private:
|
||||
@ -92,7 +94,9 @@ private:
|
||||
uint8_t readReg(uint8_t reg);
|
||||
|
||||
uint8_t _address;
|
||||
uint8_t _outputCount;
|
||||
uint8_t _error;
|
||||
|
||||
TwoWire* _wire;
|
||||
};
|
||||
|
||||
|
@ -13,7 +13,7 @@ Arduino library for PCA9553 I2C 8 bit PWM LED driver, 4 channel.
|
||||
|
||||
## Description
|
||||
|
||||
This library is to control the I2C PCA9553 PWM extender.
|
||||
This experimental library is to control the I2C PCA9553 PWM extender.
|
||||
This device has two possible hardcoded I2C addresses 0x62 and 0x63,
|
||||
see table below.
|
||||
If you need to connect more PCA9553 devices to a single I2C bus you
|
||||
@ -47,11 +47,16 @@ all zeroes, causing the bits to be set HIGH (LED off).
|
||||
|
||||
#### Related
|
||||
|
||||
- https://github.com/RobTillaart/PCA9634 (8 channel)
|
||||
- https://github.com/RobTillaart/PCA9553 (16 channel)
|
||||
- https://github.com/RobTillaart/PCA9685_RT (16 channel)
|
||||
- https://github.com/RobTillaart/PCA9551 (8 channel)
|
||||
- https://github.com/RobTillaart/PCA9552 (16 channel)
|
||||
- https://github.com/RobTillaart/PCA9553 (4 channel)
|
||||
|
||||
Follow up series
|
||||
- https://github.com/RobTillaart/PCA9634 (8 channel)
|
||||
- https://github.com/RobTillaart/PCA9635 (16 channel)
|
||||
- https://github.com/RobTillaart/PCA9685_RT (16 channel)
|
||||
|
||||
|
||||
|
||||
## Interface
|
||||
|
||||
@ -69,21 +74,27 @@ Returns true if device address is available on I2C bus.
|
||||
- **bool begin(int sda, int scl)**
|
||||
idem, ESP32 ESP8266 only.
|
||||
- **bool isConnected()** checks if address is available on I2C bus.
|
||||
- **uint8_t channelCount()** returns the number of channels = 4.
|
||||
- **uint8_t getAddress()** returns I2C address.
|
||||
- **uint8_t outputCount()** returns the number of channels = 4.
|
||||
- **uint8_t reset()**
|
||||
|
||||
|
||||
#### Input
|
||||
#### GPIO
|
||||
|
||||
- **getInput()** read current output levels.
|
||||
Only the lower 4 bits are used.
|
||||
- **uint8_t getInput()** read all current output levels.
|
||||
- **void pinMode(uint8_t pin, uint8_t mode)** set output pin to INPUT or OUTPUT.
|
||||
- **void digitalWrite(uint8_t pin, uint8_t value)** set output pin HIGH or LOW.
|
||||
- **uint8_t digitalRead(uint8_t pin)** read current state of output pin.
|
||||
|
||||
|
||||
#### Prescalers Frequency
|
||||
#### Prescaler Frequency
|
||||
|
||||
- **void setPrescaler0(uint8_t psc = 255)** set prescaler 0, default 255.
|
||||
- **uint8_t getPrescaler0()** get set value.
|
||||
- **void setPrescaler1(uint8_t psc = 255)** set prescaler 1, default 255.
|
||||
- **uint8_t getPrescaler1()** get set value.
|
||||
Get and set the pre-scaler of the PWM generator.
|
||||
|
||||
- **void setPrescaler(uint8_t gen, uint8_t psc = 255)** set pre-scaler for generator, default 255.
|
||||
- **uint8_t getPrescaler(uint8_t gen)** get the set value.
|
||||
|
||||
gen = 0 or 1
|
||||
|
||||
The main oscillator frequency can be divided by a pre-scaler.
|
||||
The period of ```BLINK = (PSC + 1) / 44```
|
||||
@ -91,7 +102,7 @@ This gives the output a blink range of 0.172 Hz to 44 Hz.
|
||||
|
||||
Some "magic" pre-scalers. (to be confirmed).
|
||||
|
||||
| psc | period | frequency |
|
||||
| psc | Period | Frequency |
|
||||
|:-----:|:--------:|:-----------:|
|
||||
| 0 | 0.0227 | 44.00 Hz |
|
||||
| 1 | 0.0455 | 22.00 Hz |
|
||||
@ -108,16 +119,19 @@ Some "magic" pre-scalers. (to be confirmed).
|
||||
|
||||
#### PWM
|
||||
|
||||
- **void setPWM0(uint8_t pwm = 128)** set PWM0, default 128 == 50%.
|
||||
- **uint8_t getPWM0()** return set PWM.
|
||||
- **void setPWM1(uint8_t pwm = 128)** set PWM0, default 128 == 50%.
|
||||
- **uint8_t getPWM1()** return set PWM.
|
||||
Get and set the duty cycle of the PWM generator.
|
||||
|
||||
- **void setPWM(uint8_t gen, uint8_t psc = 128)** set PWM for generator,
|
||||
default value = 128.
|
||||
- **uint8_t getPWM(uint8_t gen)** get the set value.
|
||||
|
||||
gen = 0 or 1
|
||||
|
||||
The duty cycle of ```BLINK = (256 - PWM) / 256```
|
||||
|
||||
| pwm | duty cycle |
|
||||
| pwm | Duty Cycle |
|
||||
|:-----:|:------------:|
|
||||
| 0 } 0% |
|
||||
| 0 | 0% |
|
||||
| 64 | 25% |
|
||||
| 128 | 50% |
|
||||
| 192 | 75% |
|
||||
@ -126,23 +140,35 @@ The duty cycle of ```BLINK = (256 - PWM) / 256```
|
||||
Note: one might need a Gamma brightness correction - https://github.com/RobTillaart/GAMMA
|
||||
|
||||
|
||||
#### LED source selector
|
||||
#### Output Mode
|
||||
|
||||
- **bool setLEDSource(uint8_t led, uint8_t source)** set the source
|
||||
of the selected led.
|
||||
- led == 0..3, source == 0..3, see table below
|
||||
- returns false if parameter is out of range.
|
||||
- **uint8_t getLEDSource(uint8_t led)** returns current setting.
|
||||
- led == 0..3
|
||||
- return source, see table below.
|
||||
- returns 0xFF if led parameter out of range.
|
||||
- **uint8_t setOutputMode(uint8_t pin, uint8_t mode)** set the mode for
|
||||
the selected output pin to one of 4 modi operandi.
|
||||
See table below.
|
||||
- pin == 0..3, mode == 0..3, see table below.
|
||||
- returns 0 if OK
|
||||
- returns error code if parameter is out of range.
|
||||
- **uint8_t getOutputMode(uint8_t led)** returns current setting.
|
||||
- pin == 0..3
|
||||
- returns mode, see table below.
|
||||
- returns error code if parameter is out of range.
|
||||
|
||||
| source | output |
|
||||
|:--------:|:---------------------|
|
||||
| 00 | is set LOW (LED on)
|
||||
| 01 | is set high-impedance (LED off; default)
|
||||
| 10 | blinks at PWM0 rate
|
||||
| 11 | blinks at PWM1 rate
|
||||
| Define | Value | Output pin |
|
||||
|:--------------------|:-------:|:---------------------|
|
||||
| PCA9553_MODE_LOW | 0 | is set LOW (LED on)
|
||||
| PCA9553_MODE_HIGH | 1 | is set high-impedance (LED off; default)
|
||||
| PCA9553_MODE_PWM0 | 2 | blinks at PWM0 rate
|
||||
| PCA9553_MODE_PWM1 | 3 | blinks at PWM1 rate
|
||||
|
||||
|
||||
#### Power On Reset
|
||||
|
||||
The PCA9553 will keep its settings as long as it is powered on.
|
||||
This means it can start with an previous configuration when uploading
|
||||
two different sketches short after each other.
|
||||
|
||||
To handle this the library has a **reset()** function which sets
|
||||
the device in the Power On state.
|
||||
|
||||
|
||||
#### Error codes
|
||||
@ -154,45 +180,35 @@ These are kept similar to PCA9635 et al error codes.
|
||||
| PCA9553_OK | 0x00 | Everything went well
|
||||
| PCA9553_ERROR | 0xFF | Generic error
|
||||
| PCA9553_ERR_WRITE | 0xFE |
|
||||
| PCA9553_ERR_CHAN | 0xFD |
|
||||
| PCA9553_ERR_MODE | 0xFC |
|
||||
| PCA9553_ERR_CHAN | 0xFD | output pin out of range / channel error
|
||||
| PCA9553_ERR_MODE | 0xFC | mode parameter out of range.
|
||||
| PCA9553_ERR_REG | 0xFB |
|
||||
| PCA9553_ERR_I2C | 0xFA |
|
||||
|
||||
To be elaborated in the source code.
|
||||
|
||||
|
||||
## Future
|
||||
|
||||
#### Must
|
||||
|
||||
- improve documentation
|
||||
- test test test
|
||||
- test with hardware
|
||||
|
||||
#### Should
|
||||
|
||||
- **reset()** power on reset...
|
||||
- GPIO modi pins
|
||||
- **pinMode()**
|
||||
- **digitalWrite()**
|
||||
- **digitalRead()**
|
||||
- improve error handling
|
||||
- return values, where etc.
|
||||
- **setLEDSource(src0, src1, src2, src3)** one call
|
||||
- defines for sources
|
||||
- getAddress()
|
||||
- test
|
||||
- reset function
|
||||
|
||||
|
||||
#### Could
|
||||
|
||||
|
||||
#### Wont (on request)
|
||||
|
||||
- no usage of autoincrement register
|
||||
- percent interface for PWM
|
||||
- time interface for prescaler
|
||||
- default setup in begin (what how)
|
||||
- alternative interface calls
|
||||
- **setPWM(pwm0, pwm1)**
|
||||
- **setPrescaler(psc0, psc1)**
|
||||
- **setChannel(channel, psc0, pwm)**
|
||||
- **setPWM(channel, pwm);
|
||||
- **setPrescaler(channel, psc1)**
|
||||
|
||||
|
||||
|
48
libraries/PCA9553/examples/PCA9553_PWM/PCA9553_PWM.ino
Normal file
48
libraries/PCA9553/examples/PCA9553_PWM/PCA9553_PWM.ino
Normal file
@ -0,0 +1,48 @@
|
||||
//
|
||||
// FILE: PCA9553_PWM.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2023-07-18
|
||||
// PURPOSE: test PCA9553 library
|
||||
// URL: https://github.com/RobTillaart/PCA9553
|
||||
//
|
||||
// Connect LEDs from pin 0 and pin 1 with a resistor to 5V
|
||||
// See datasheet
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "Wire.h"
|
||||
#include "PCA9553.h"
|
||||
|
||||
|
||||
PCA9553 leds(0x62);
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.print("PCA9553_LIB_VERSION: ");
|
||||
Serial.println(PCA9553_LIB_VERSION);
|
||||
Serial.println();
|
||||
|
||||
if (leds.begin() == false)
|
||||
{
|
||||
Serial.println("Could not connect.");
|
||||
while(1);
|
||||
}
|
||||
|
||||
leds.setOutputMode(0, PCA9553_MODE_PWM0);
|
||||
leds.setOutputMode(1, PCA9553_MODE_PWM1);
|
||||
|
||||
leds.setPrescaler(0, 43); // 1 Hz
|
||||
leds.setPWM(0, 128); // 50% duty cycle
|
||||
leds.setPrescaler(1, 21); // 2 Hz
|
||||
leds.setPWM(1, 32); // 12% duty cycle
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
53
libraries/PCA9553/examples/PCA9553_blink/PCA9553_blink.ino
Normal file
53
libraries/PCA9553/examples/PCA9553_blink/PCA9553_blink.ino
Normal file
@ -0,0 +1,53 @@
|
||||
//
|
||||
// FILE: PCA9553_blink.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2023-07-18
|
||||
// PURPOSE: test PCA9553 library
|
||||
// URL: https://github.com/RobTillaart/PCA9553
|
||||
//
|
||||
// Connect LEDs from pin 0 and pin 1 with a resistor to 5V
|
||||
// See datasheet
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "Wire.h"
|
||||
#include "PCA9553.h"
|
||||
|
||||
|
||||
PCA9553 leds(0x62);
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.print("PCA9553_LIB_VERSION: ");
|
||||
Serial.println(PCA9553_LIB_VERSION);
|
||||
Serial.println();
|
||||
|
||||
if (leds.begin() == false)
|
||||
{
|
||||
Serial.println("Could not connect.");
|
||||
while(1);
|
||||
}
|
||||
|
||||
// GPIO
|
||||
// not mandatory, just to make it explicit
|
||||
leds.pinMode(0, OUTPUT);
|
||||
leds.pinMode(1, OUTPUT);
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
leds.digitalWrite(0, LOW);
|
||||
delay(500);
|
||||
leds.digitalWrite(1, LOW);
|
||||
delay(500);
|
||||
leds.digitalWrite(0,HIGH);
|
||||
delay(500);
|
||||
leds.digitalWrite(1,HIGH);
|
||||
delay(500);
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
@ -0,0 +1,53 @@
|
||||
//
|
||||
// FILE: PCA9553_digitalRead.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2023-07-18
|
||||
// PURPOSE: test PCA9553 library
|
||||
// URL: https://github.com/RobTillaart/PCA9553
|
||||
//
|
||||
// Connect pin 0 to 4 to either +5V or to GND
|
||||
// to read LOW/HIGH values
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "Wire.h"
|
||||
#include "PCA9553.h"
|
||||
|
||||
|
||||
PCA9553 leds(0x62);
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.print("PCA9553_LIB_VERSION: ");
|
||||
Serial.println(PCA9553_LIB_VERSION);
|
||||
Serial.println();
|
||||
|
||||
if (leds.begin() == false)
|
||||
{
|
||||
Serial.println("Could not connect.");
|
||||
while (1);
|
||||
}
|
||||
|
||||
for (int pin = 0; pin < leds.outputCount(); pin++)
|
||||
{
|
||||
leds.pinMode(pin, INPUT);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
for (int pin = 0; pin < leds.outputCount(); pin++)
|
||||
{
|
||||
int x = leds.digitalRead(pin);
|
||||
Serial.print(x);
|
||||
Serial.print('\t');
|
||||
}
|
||||
Serial.println();
|
||||
delay(100);
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
@ -23,23 +23,23 @@ void setup()
|
||||
|
||||
if (leds.begin() == false)
|
||||
{
|
||||
Serial.println("COuld not connect.");
|
||||
Serial.println("Could not connect.");
|
||||
while(1);
|
||||
}
|
||||
|
||||
// default
|
||||
leds.setPrescaler0(255);
|
||||
leds.setPWM0(128);
|
||||
leds.setPrescaler(0, 255);
|
||||
leds.setPWM(0, 128);
|
||||
|
||||
// different
|
||||
leds.setPrescaler1(113);
|
||||
leds.setPWM1(32);
|
||||
leds.setPrescaler(1, 113);
|
||||
leds.setPWM(1, 32);
|
||||
|
||||
// all channels a different source
|
||||
leds.setLEDSource(0, 0);
|
||||
leds.setLEDSource(1, 1);
|
||||
leds.setLEDSource(2, 2);
|
||||
leds.setLEDSource(3, 3);
|
||||
// all output pins in a different mode
|
||||
leds.setOutputMode(0, PCA9553_MODE_LOW);
|
||||
leds.setOutputMode(1, PCA9553_MODE_HIGH);
|
||||
leds.setOutputMode(2, PCA9553_MODE_PWM0);
|
||||
leds.setOutputMode(3, PCA9553_MODE_PWM1);
|
||||
|
||||
Serial.println("done...");
|
||||
}
|
||||
|
48
libraries/PCA9553/examples/PCA9553_test02/PCA9553_test02.ino
Normal file
48
libraries/PCA9553/examples/PCA9553_test02/PCA9553_test02.ino
Normal file
@ -0,0 +1,48 @@
|
||||
//
|
||||
// FILE: PCA9553_test02.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2023-07-17
|
||||
// PURPOSE: test PCA9553 library
|
||||
// URL: https://github.com/RobTillaart/PCA9553
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "Wire.h"
|
||||
#include "PCA9553.h"
|
||||
|
||||
|
||||
PCA9553 leds(0x62);
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.print("PCA9553_LIB_VERSION: ");
|
||||
Serial.println(PCA9553_LIB_VERSION);
|
||||
Serial.println();
|
||||
|
||||
if (leds.begin() == false)
|
||||
{
|
||||
Serial.println("Could not connect.");
|
||||
while(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
// random blink
|
||||
leds.digitalWrite(0, random(2));
|
||||
|
||||
// steady blink
|
||||
leds.digitalWrite(1, !leds.digitalRead(1));
|
||||
|
||||
// output 2 follows 3
|
||||
leds.digitalWrite(2, leds.digitalRead(3));
|
||||
leds.digitalWrite(3, random(2));
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
@ -0,0 +1,119 @@
|
||||
//
|
||||
// FILE: PCA9553_test_registers.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: test PCA9553 device registers readback
|
||||
// URL: https://github.com/RobTillaart/PCA9553
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "Wire.h"
|
||||
#include "PCA9553.h"
|
||||
|
||||
|
||||
PCA9553 leds(0x62);
|
||||
|
||||
|
||||
void test_GPIO()
|
||||
{
|
||||
Serial.println(__FUNCTION__);
|
||||
for (int i = 0; i < leds.outputCount(); i++)
|
||||
{
|
||||
leds.digitalWrite(i, LOW);
|
||||
Serial.print(i);
|
||||
Serial.print("\t");
|
||||
Serial.print(leds.digitalRead(i));
|
||||
Serial.print("\t");
|
||||
leds.digitalWrite(i, HIGH);
|
||||
Serial.print(leds.digitalRead(i));
|
||||
Serial.print("\n");
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
void test_prescaler()
|
||||
{
|
||||
Serial.println(__FUNCTION__);
|
||||
for (uint8_t val = 0; val < 255; val += 5)
|
||||
{
|
||||
leds.setPrescaler(0, val);
|
||||
leds.setPrescaler(1, val);
|
||||
Serial.print(leds.getPrescaler(0));
|
||||
Serial.print("\t");
|
||||
Serial.println(leds.getPrescaler(1));
|
||||
Serial.print("\n");
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
void test_PWM()
|
||||
{
|
||||
Serial.println(__FUNCTION__);
|
||||
for (uint8_t val = 0; val < 255; val += 5)
|
||||
{
|
||||
leds.setPWM(0, val);
|
||||
leds.setPWM(1, val);
|
||||
Serial.print(leds.getPWM(0));
|
||||
Serial.print("\t");
|
||||
Serial.println(leds.getPWM(1));
|
||||
Serial.print("\n");
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void test_source()
|
||||
{
|
||||
Serial.println(__FUNCTION__);
|
||||
for (uint8_t val = 0; val < 4; val++)
|
||||
{
|
||||
leds.setOutputMode(0, val);
|
||||
leds.setOutputMode(1, val);
|
||||
leds.setOutputMode(2, val);
|
||||
leds.setOutputMode(3, val);
|
||||
|
||||
Serial.print(leds.getOutputMode(0));
|
||||
Serial.print("\t");
|
||||
Serial.print(leds.getOutputMode(1));
|
||||
Serial.print("\t");
|
||||
Serial.print(leds.getOutputMode(2));
|
||||
Serial.print("\t");
|
||||
Serial.print(leds.getOutputMode(3));
|
||||
Serial.print("\n");
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.print("PCA9553_LIB_VERSION: ");
|
||||
Serial.println(PCA9553_LIB_VERSION);
|
||||
Serial.println();
|
||||
|
||||
if (leds.begin() == false)
|
||||
{
|
||||
Serial.println("Could not connect.");
|
||||
while(1);
|
||||
}
|
||||
|
||||
Serial.println(leds.getAddress(), HEX);
|
||||
Serial.println(leds.outputCount());
|
||||
Serial.println();
|
||||
|
||||
test_GPIO();
|
||||
test_prescaler();
|
||||
test_PWM();
|
||||
test_source();
|
||||
|
||||
Serial.println("\ndone...");
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
@ -7,30 +7,37 @@ PCA9553 KEYWORD1
|
||||
|
||||
# Methods and Functions (KEYWORD2)
|
||||
begin KEYWORD2
|
||||
configure KEYWORD2
|
||||
|
||||
isConnected KEYWORD2
|
||||
channelCount KEYWORD2
|
||||
getAddress KEYWORD2
|
||||
outputCount KEYWORD2
|
||||
reset KEYWORD2
|
||||
|
||||
getInput KEYWORD2
|
||||
pinMode KEYWORD2
|
||||
digitalWrite KEYWORD2
|
||||
digitalRead KEYWORD2
|
||||
|
||||
setPrescaler0 KEYWORD2
|
||||
getPrescaler0 KEYWORD2
|
||||
setPrescaler1 KEYWORD2
|
||||
getPrescaler1 KEYWORD2
|
||||
setPrescaler KEYWORD2
|
||||
getPrescaler KEYWORD2
|
||||
|
||||
setPWM0 KEYWORD2
|
||||
getPWM0 KEYWORD2
|
||||
setPWM1 KEYWORD2
|
||||
getPWM1 KEYWORD2
|
||||
setPWM KEYWORD2
|
||||
getPWM KEYWORD2
|
||||
|
||||
setLEDSource KEYWORD2
|
||||
getLEDSource KEYWORD2
|
||||
setOutputMode KEYWORD2
|
||||
getOutputMode KEYWORD2
|
||||
|
||||
|
||||
#
|
||||
# Constants ( LITERAL1)
|
||||
#
|
||||
PCA9553_LIB_VERSION LITERAL1
|
||||
|
||||
PCA9553_OK LITERAL1
|
||||
PCA9553_ERROR LITERAL1
|
||||
|
||||
PCA9553_ERR_WRITE LITERAL1
|
||||
PCA9553_ERR_CHAN LITERAL1
|
||||
PCA9553_ERR_MODE LITERAL1
|
||||
PCA9553_ERR_REG LITERAL1
|
||||
PCA9553_ERR_I2C LITERAL1
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "PCA9553",
|
||||
"keywords": "I2C,PCA9635,PWM",
|
||||
"keywords": "I2C,PCA9553,PWM",
|
||||
"description": "Arduino library for PCA9553 I2C LED driver, 4 channel",
|
||||
"authors":
|
||||
[
|
||||
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/PCA9553.git"
|
||||
},
|
||||
"version": "0.1.0",
|
||||
"version": "0.2.0",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=PCA9553
|
||||
version=0.1.0
|
||||
version=0.2.0
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Arduino library for PCA9553 I2C LED driver 4 channel
|
||||
|
@ -56,6 +56,12 @@ unittest(test_constants)
|
||||
assertEqual(PCA9553_PWM1 , 0x04);
|
||||
assertEqual(PCA9553_LS0 , 0x05);
|
||||
|
||||
fprintf(stderr, "\noutput modi");
|
||||
assertEqual(PCA9553_MODE_LOW , 0x00);
|
||||
assertEqual(PCA9553_MODE_HIGH , 0x01);
|
||||
assertEqual(PCA9553_MODE_PWM0 , 0x02);
|
||||
assertEqual(PCA9553_MODE_PWM1 , 0x03);
|
||||
|
||||
fprintf(stderr, "\nerrorcodes");
|
||||
assertEqual(PCA9553_OK , 0x00);
|
||||
assertEqual(PCA9553_ERROR , 0xFF);
|
||||
@ -71,7 +77,8 @@ unittest(test_constructor)
|
||||
{
|
||||
PCA9553 pca(0x62);
|
||||
|
||||
assertEqual(4, pca.channelCount());
|
||||
assertEqual(4, pca.outputCount());
|
||||
assertEqual(0x62, pca.getAddress());
|
||||
}
|
||||
|
||||
// need mock up for more tests.
|
||||
|
Loading…
x
Reference in New Issue
Block a user