mirror of
https://github.com/xreef/PCF8574_library.git
synced 2024-08-30 18:12:18 +00:00
Esp32 support for second i2c channel
This commit is contained in:
parent
b0f9079737
commit
9a39b47339
137
PCF8574.cpp
137
PCF8574.cpp
@ -1,3 +1,30 @@
|
||||
/*
|
||||
* PCF8574 GPIO Port Expand
|
||||
* https://www.mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "PCF8574.h"
|
||||
#include "Wire.h"
|
||||
|
||||
@ -6,6 +33,8 @@
|
||||
* @param address: i2c address
|
||||
*/
|
||||
PCF8574::PCF8574(uint8_t address){
|
||||
_wire = &Wire;
|
||||
|
||||
_address = address;
|
||||
};
|
||||
|
||||
@ -16,6 +45,8 @@ PCF8574::PCF8574(uint8_t address){
|
||||
* @param interruptFunction: function to call when interrupt raised
|
||||
*/
|
||||
PCF8574::PCF8574(uint8_t address, uint8_t interruptPin, void (*interruptFunction)() ){
|
||||
_wire = &Wire;
|
||||
|
||||
_address = address;
|
||||
_interruptPin = interruptPin;
|
||||
_interruptFunction = interruptFunction;
|
||||
@ -30,6 +61,8 @@ PCF8574::PCF8574(uint8_t address, uint8_t interruptPin, void (*interruptFunctio
|
||||
* @param scl: scl pin
|
||||
*/
|
||||
PCF8574::PCF8574(uint8_t address, uint8_t sda, uint8_t scl){
|
||||
_wire = &Wire;
|
||||
|
||||
_address = address;
|
||||
_sda = sda;
|
||||
_scl = scl;
|
||||
@ -44,6 +77,70 @@ PCF8574::PCF8574(uint8_t address, uint8_t interruptPin, void (*interruptFunctio
|
||||
* @param interruptFunction: function to call when interrupt raised
|
||||
*/
|
||||
PCF8574::PCF8574(uint8_t address, uint8_t sda, uint8_t scl, uint8_t interruptPin, void (*interruptFunction)() ){
|
||||
_wire = &Wire;
|
||||
|
||||
_address = address;
|
||||
_sda = sda;
|
||||
_scl = scl;
|
||||
|
||||
_interruptPin = interruptPin;
|
||||
_interruptFunction = interruptFunction;
|
||||
|
||||
_usingInterrupt = true;
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef ESP32
|
||||
/**
|
||||
* Constructor
|
||||
* @param address: i2c address
|
||||
*/
|
||||
PCF8574::PCF8574(TwoWire *pWire, uint8_t address){
|
||||
_wire = pWire;
|
||||
|
||||
_address = address;
|
||||
};
|
||||
|
||||
/**
|
||||
* Construcor
|
||||
* @param address: i2c address
|
||||
* @param interruptPin: pin to set interrupt
|
||||
* @param interruptFunction: function to call when interrupt raised
|
||||
*/
|
||||
PCF8574::PCF8574(TwoWire *pWire, uint8_t address, uint8_t interruptPin, void (*interruptFunction)() ){
|
||||
_wire = pWire;
|
||||
|
||||
_address = address;
|
||||
_interruptPin = interruptPin;
|
||||
_interruptFunction = interruptFunction;
|
||||
_usingInterrupt = true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param address: i2c address
|
||||
* @param sda: sda pin
|
||||
* @param scl: scl pin
|
||||
*/
|
||||
PCF8574::PCF8574(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl){
|
||||
_wire = pWire;
|
||||
|
||||
_address = address;
|
||||
_sda = sda;
|
||||
_scl = scl;
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param address: i2c address
|
||||
* @param sda: sda pin
|
||||
* @param scl: scl pin
|
||||
* @param interruptPin: pin to set interrupt
|
||||
* @param interruptFunction: function to call when interrupt raised
|
||||
*/
|
||||
PCF8574::PCF8574(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl, uint8_t interruptPin, void (*interruptFunction)() ){
|
||||
_wire = pWire;
|
||||
|
||||
_address = address;
|
||||
_sda = sda;
|
||||
_scl = scl;
|
||||
@ -60,18 +157,18 @@ PCF8574::PCF8574(uint8_t address, uint8_t interruptPin, void (*interruptFunctio
|
||||
*/
|
||||
void PCF8574::begin(){
|
||||
#ifndef __AVR
|
||||
Wire.begin(_sda, _scl);
|
||||
_wire->begin(_sda, _scl);
|
||||
#else
|
||||
// Default pin for AVR some problem on software emulation
|
||||
// #define SCL_PIN _scl
|
||||
// #define SDA_PIN _sda
|
||||
Wire.begin();
|
||||
_wire->begin();
|
||||
#endif
|
||||
|
||||
// Che if there are pins to set low
|
||||
if (writeMode>0 || readMode>0){
|
||||
DEBUG_PRINTLN("Set write mode");
|
||||
Wire.beginTransmission(_address);
|
||||
_wire->beginTransmission(_address);
|
||||
DEBUG_PRINT(" ");
|
||||
DEBUG_PRINT("usedPin pin ");
|
||||
|
||||
@ -79,10 +176,10 @@ void PCF8574::begin(){
|
||||
byte usedPin = writeMode | readMode;
|
||||
DEBUG_PRINTLN( ~usedPin, BIN);
|
||||
|
||||
Wire.write(~usedPin);
|
||||
_wire->write(~usedPin);
|
||||
|
||||
DEBUG_PRINTLN("Start end trasmission if stop here check pullup resistor.");
|
||||
Wire.endTransmission();
|
||||
_wire->endTransmission();
|
||||
}
|
||||
|
||||
// If using interrupt set interrupt value to pin
|
||||
@ -137,11 +234,11 @@ void PCF8574::pinMode(uint8_t pin, uint8_t mode){
|
||||
*/
|
||||
void PCF8574::readBuffer(bool force){
|
||||
if (millis() > PCF8574::lastReadMillis+READ_ELAPSED_TIME || _usingInterrupt || force){
|
||||
Wire.requestFrom(_address,(uint8_t)1);// Begin transmission to PCF8574 with the buttons
|
||||
_wire->requestFrom(_address,(uint8_t)1);// Begin transmission to PCF8574 with the buttons
|
||||
lastReadMillis = millis();
|
||||
if(Wire.available()) // If bytes are available to be recieved
|
||||
if(_wire->available()) // If bytes are available to be recieved
|
||||
{
|
||||
byte iInput = Wire.read();// Read a byte
|
||||
byte iInput = _wire->read();// Read a byte
|
||||
if ((iInput & readMode)>0){
|
||||
byteBuffered = byteBuffered | (byte)iInput;
|
||||
}
|
||||
@ -157,12 +254,12 @@ void PCF8574::readBuffer(bool force){
|
||||
*/
|
||||
PCF8574::DigitalInput PCF8574::digitalReadAll(void){
|
||||
DEBUG_PRINTLN("Read from buffer");
|
||||
Wire.requestFrom(_address,(uint8_t)1);// Begin transmission to PCF8574 with the buttons
|
||||
_wire->requestFrom(_address,(uint8_t)1);// Begin transmission to PCF8574 with the buttons
|
||||
lastReadMillis = millis();
|
||||
if(Wire.available()) // If bytes are available to be recieved
|
||||
if(_wire->available()) // If bytes are available to be recieved
|
||||
{
|
||||
DEBUG_PRINTLN("Data ready");
|
||||
byte iInput = Wire.read();// Read a byte
|
||||
byte iInput = _wire->read();// Read a byte
|
||||
|
||||
if ((iInput & readMode)>0){
|
||||
DEBUG_PRINT("Input ");
|
||||
@ -202,12 +299,12 @@ void PCF8574::readBuffer(bool force){
|
||||
*/
|
||||
byte PCF8574::digitalReadAll(void){
|
||||
DEBUG_PRINTLN("Read from buffer");
|
||||
Wire.requestFrom(_address,(uint8_t)1);// Begin transmission to PCF8574 with the buttons
|
||||
_wire->requestFrom(_address,(uint8_t)1);// Begin transmission to PCF8574 with the buttons
|
||||
lastReadMillis = millis();
|
||||
if(Wire.available()) // If bytes are available to be recieved
|
||||
if(_wire->available()) // If bytes are available to be recieved
|
||||
{
|
||||
DEBUG_PRINTLN("Data ready");
|
||||
byte iInput = Wire.read();// Read a byte
|
||||
byte iInput = _wire->read();// Read a byte
|
||||
|
||||
if ((iInput & readMode)>0){
|
||||
DEBUG_PRINT("Input ");
|
||||
@ -250,12 +347,12 @@ uint8_t PCF8574::digitalRead(uint8_t pin){
|
||||
value = HIGH;
|
||||
}else if ((/*(bit(pin) & byteBuffered)<=0 && */millis() > PCF8574::lastReadMillis+READ_ELAPSED_TIME) /*|| _usingInterrupt*/){
|
||||
DEBUG_PRINTLN("Read from buffer");
|
||||
Wire.requestFrom(_address,(uint8_t)1);// Begin transmission to PCF8574 with the buttons
|
||||
_wire->requestFrom(_address,(uint8_t)1);// Begin transmission to PCF8574 with the buttons
|
||||
lastReadMillis = millis();
|
||||
if(Wire.available()) // If bytes are available to be recieved
|
||||
if(_wire->available()) // If bytes are available to be recieved
|
||||
{
|
||||
DEBUG_PRINTLN("Data ready");
|
||||
byte iInput = Wire.read();// Read a byte
|
||||
byte iInput = _wire->read();// Read a byte
|
||||
|
||||
if ((iInput & readMode)>0){
|
||||
DEBUG_PRINT("Input ");
|
||||
@ -291,7 +388,7 @@ uint8_t PCF8574::digitalRead(uint8_t pin){
|
||||
*/
|
||||
void PCF8574::digitalWrite(uint8_t pin, uint8_t value){
|
||||
DEBUG_PRINTLN("Begin trasmission");
|
||||
Wire.beginTransmission(_address); //Begin the transmission to PCF8574
|
||||
_wire->beginTransmission(_address); //Begin the transmission to PCF8574
|
||||
if (value==HIGH){
|
||||
writeByteBuffered = writeByteBuffered | bit(pin);
|
||||
}else{
|
||||
@ -307,10 +404,10 @@ void PCF8574::digitalWrite(uint8_t pin, uint8_t value){
|
||||
DEBUG_PRINTLN(value);
|
||||
|
||||
writeByteBuffered = writeByteBuffered & writeMode;
|
||||
Wire.write(writeByteBuffered);
|
||||
_wire->write(writeByteBuffered);
|
||||
DEBUG_PRINTLN("Start end trasmission if stop here check pullup resistor.");
|
||||
|
||||
Wire.endTransmission();
|
||||
_wire->endTransmission();
|
||||
};
|
||||
|
||||
|
||||
|
44
PCF8574.h
44
PCF8574.h
@ -1,7 +1,28 @@
|
||||
/** \mainpage PCF8574 library
|
||||
/*
|
||||
* PCF8574 GPIO Port Expand
|
||||
* https://www.mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/
|
||||
*
|
||||
* MIT license
|
||||
* written by Renzo Mischianti
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef PCF8574_h
|
||||
@ -60,10 +81,21 @@ class PCF8574 {
|
||||
public:
|
||||
|
||||
PCF8574(uint8_t address);
|
||||
PCF8574(uint8_t address, uint8_t sda, uint8_t scl);
|
||||
|
||||
PCF8574(uint8_t address, uint8_t interruptPin, void (*interruptFunction)() );
|
||||
|
||||
#ifndef __AVR
|
||||
PCF8574(uint8_t address, uint8_t sda, uint8_t scl);
|
||||
PCF8574(uint8_t address, uint8_t sda, uint8_t scl, uint8_t interruptPin, void (*interruptFunction)());
|
||||
#endif
|
||||
|
||||
#ifdef ESP32
|
||||
///// changes for second i2c bus
|
||||
PCF8574(TwoWire *pWire, uint8_t address);
|
||||
PCF8574(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl);
|
||||
|
||||
PCF8574(TwoWire *pWire, uint8_t address, uint8_t interruptPin, void (*interruptFunction)() );
|
||||
PCF8574(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl, uint8_t interruptPin, void (*interruptFunction)());
|
||||
#endif
|
||||
|
||||
void begin();
|
||||
void pinMode(uint8_t pin, uint8_t mode);
|
||||
@ -94,6 +126,8 @@ private:
|
||||
uint8_t _sda = SDA;
|
||||
uint8_t _scl = SCL;
|
||||
|
||||
TwoWire *_wire;
|
||||
|
||||
bool _usingInterrupt = false;
|
||||
uint8_t _interruptPin = 2;
|
||||
void (*_interruptFunction)(){};
|
||||
|
@ -1,3 +1,10 @@
|
||||
/*
|
||||
Blink led on PIN0
|
||||
by Mischianti Renzo <http://www.mischianti.org>
|
||||
|
||||
https://www.mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/
|
||||
*/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "PCF8574.h"
|
||||
|
||||
|
@ -1,3 +1,10 @@
|
||||
/*
|
||||
KeyPressed on PIN1
|
||||
by Mischianti Renzo <http://www.mischianti.org>
|
||||
|
||||
https://www.mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/
|
||||
*/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "PCF8574.h"
|
||||
|
||||
|
@ -1,3 +1,10 @@
|
||||
/*
|
||||
KeyPressed async
|
||||
by Mischianti Renzo <http://www.mischianti.org>
|
||||
|
||||
https://www.mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/
|
||||
*/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "PCF8574.h"
|
||||
|
||||
|
@ -1,3 +1,10 @@
|
||||
/*
|
||||
KeyPressed with interrupt
|
||||
by Mischianti Renzo <http://www.mischianti.org>
|
||||
|
||||
https://www.mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/
|
||||
*/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "PCF8574.h"
|
||||
|
||||
|
66
examples/ledEsp32OnTheSecondI2C/ledEsp32OnTheSecondI2C.ino
Normal file
66
examples/ledEsp32OnTheSecondI2C/ledEsp32OnTheSecondI2C.ino
Normal file
@ -0,0 +1,66 @@
|
||||
#include "Arduino.h"
|
||||
/*
|
||||
* PCF8574 GPIO Port Expand
|
||||
* Blink all led
|
||||
* by Mischianti Renzo <http://www.mischianti.org>
|
||||
*
|
||||
* https://www.mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/
|
||||
*
|
||||
*
|
||||
* PCF8574 ----- Esp32
|
||||
* A0 ----- GRD
|
||||
* A1 ----- GRD
|
||||
* A2 ----- GRD
|
||||
* VSS ----- GRD
|
||||
* VDD ----- 5V/3.3V
|
||||
* SDA ----- 21
|
||||
* SCL ----- 22
|
||||
*
|
||||
* P0 ----------------- LED0
|
||||
* P1 ----------------- LED1
|
||||
* P2 ----------------- LED2
|
||||
* P3 ----------------- LED3
|
||||
* P4 ----------------- LED4
|
||||
* P5 ----------------- LED5
|
||||
* P6 ----------------- LED6
|
||||
* P7 ----------------- LED7
|
||||
*
|
||||
*/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "PCF8574.h" // https://github.com/xreef/PCF8574_library
|
||||
|
||||
// Instantiate Wire for generic use at 400kHz
|
||||
TwoWire I2Cone = TwoWire(0);
|
||||
// Instantiate Wire for generic use at 100kHz
|
||||
TwoWire I2Ctwo = TwoWire(1);
|
||||
|
||||
// Set i2c address
|
||||
PCF8574 pcf8574(&I2Ctwo, 0x20);
|
||||
// PCF8574 pcf8574(&I2Ctwo, 0x20, 21, 22);
|
||||
// PCF8574(TwoWire *pWire, uint8_t address, uint8_t interruptPin, void (*interruptFunction)() );
|
||||
// PCF8574(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl, uint8_t interruptPin, void (*interruptFunction)());
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(112560);
|
||||
|
||||
I2Cone.begin(16,17,400000); // SDA pin 16, SCL pin 17, 400kHz frequency
|
||||
|
||||
// Set pinMode to OUTPUT
|
||||
for(int i=0;i<8;i++) {
|
||||
pcf8574.pinMode(i, OUTPUT);
|
||||
}
|
||||
pcf8574.begin();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
static int pin = 0;
|
||||
pcf8574.digitalWrite(pin, HIGH);
|
||||
delay(400);
|
||||
pcf8574.digitalWrite(pin, LOW);
|
||||
delay(400);
|
||||
pin++;
|
||||
if (pin > 7) pin = 0;
|
||||
}
|
@ -1,3 +1,10 @@
|
||||
/*
|
||||
Read all data after interrupt
|
||||
by Mischianti Renzo <http://www.mischianti.org>
|
||||
|
||||
https://www.mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/
|
||||
*/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "PCF8574.h"
|
||||
|
||||
|
@ -1,3 +1,10 @@
|
||||
/*
|
||||
KeyPressed with interrupt in LOW_MEMORY mode
|
||||
by Mischianti Renzo <http://www.mischianti.org>
|
||||
|
||||
https://www.mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/
|
||||
*/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "PCF8574.h"
|
||||
|
||||
|
3020
resources/PCB/PCB3.flat
Normal file
3020
resources/PCB/PCB3.flat
Normal file
File diff suppressed because one or more lines are too long
Binary file not shown.
BIN
resources/PCB_PCF8574_04.fzz
Normal file
BIN
resources/PCB_PCF8574_04.fzz
Normal file
Binary file not shown.
BIN
resources/PCB_PCF8574_04_pcb.png
Normal file
BIN
resources/PCB_PCF8574_04_pcb.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 192 KiB |
Loading…
Reference in New Issue
Block a user