mirror of
https://github.com/xreef/PCF8574_library.git
synced 2024-08-30 18:12:18 +00:00
UPDATE 2.0 VERSION
This commit is contained in:
commit
f8d02a8b34
191
PCF8574.cpp
191
PCF8574.cpp
@ -169,14 +169,16 @@ void PCF8574::begin(){
|
|||||||
if (writeMode>0 || readMode>0){
|
if (writeMode>0 || readMode>0){
|
||||||
DEBUG_PRINTLN("Set write mode");
|
DEBUG_PRINTLN("Set write mode");
|
||||||
_wire->beginTransmission(_address);
|
_wire->beginTransmission(_address);
|
||||||
DEBUG_PRINT(" ");
|
|
||||||
DEBUG_PRINT("usedPin pin ");
|
|
||||||
|
|
||||||
|
|
||||||
byte usedPin = writeMode | readMode;
|
DEBUG_PRINT("resetInitial pin ");
|
||||||
DEBUG_PRINTLN( ~usedPin, BIN);
|
resetInitial = writeModeUp | readModePullUp;
|
||||||
|
DEBUG_PRINTLN( resetInitial, BIN);
|
||||||
|
|
||||||
_wire->write(~usedPin);
|
_wire->write(resetInitial);
|
||||||
|
|
||||||
|
byteBuffered = resetInitial;
|
||||||
|
writeByteBuffered = writeModeUp;
|
||||||
|
|
||||||
DEBUG_PRINTLN("Start end trasmission if stop here check pullup resistor.");
|
DEBUG_PRINTLN("Start end trasmission if stop here check pullup resistor.");
|
||||||
_wire->endTransmission();
|
_wire->endTransmission();
|
||||||
@ -184,9 +186,12 @@ void PCF8574::begin(){
|
|||||||
|
|
||||||
// If using interrupt set interrupt value to pin
|
// If using interrupt set interrupt value to pin
|
||||||
if (_usingInterrupt){
|
if (_usingInterrupt){
|
||||||
|
// DEBUG_PRINTLN("Using interrupt pin (not all pin is interrupted)");
|
||||||
|
// ::pinMode(_interruptPin, INPUT_PULLUP);
|
||||||
|
// attachInterrupt(digitalPinToInterrupt(_interruptPin), (*_interruptFunction), FALLING );
|
||||||
DEBUG_PRINTLN("Using interrupt pin (not all pin is interrupted)");
|
DEBUG_PRINTLN("Using interrupt pin (not all pin is interrupted)");
|
||||||
::pinMode(_interruptPin, INPUT_PULLUP);
|
::pinMode(_interruptPin, INPUT_PULLUP);
|
||||||
attachInterrupt(digitalPinToInterrupt(_interruptPin), (*_interruptFunction), FALLING );
|
attachInterrupt(digitalPinToInterrupt(_interruptPin), (*_interruptFunction), CHANGE );
|
||||||
}
|
}
|
||||||
|
|
||||||
// inizialize last read
|
// inizialize last read
|
||||||
@ -196,9 +201,10 @@ void PCF8574::begin(){
|
|||||||
/**
|
/**
|
||||||
* Set if fin is OUTPUT or INPUT
|
* Set if fin is OUTPUT or INPUT
|
||||||
* @param pin: pin to set
|
* @param pin: pin to set
|
||||||
* @param mode: mode, supported only INPUT or OUTPUT (to semplify)
|
* @param mode: mode, supported only INPUT or OUTPUT (to simplify)
|
||||||
|
* @param output_start: output_start, for OUTPUT we can set initial value
|
||||||
*/
|
*/
|
||||||
void PCF8574::pinMode(uint8_t pin, uint8_t mode){
|
void PCF8574::pinMode(uint8_t pin, uint8_t mode, uint8_t output_start){
|
||||||
DEBUG_PRINT("Set pin ");
|
DEBUG_PRINT("Set pin ");
|
||||||
DEBUG_PRINT(pin);
|
DEBUG_PRINT(pin);
|
||||||
DEBUG_PRINT(" as ");
|
DEBUG_PRINT(" as ");
|
||||||
@ -206,26 +212,60 @@ void PCF8574::pinMode(uint8_t pin, uint8_t mode){
|
|||||||
|
|
||||||
if (mode == OUTPUT){
|
if (mode == OUTPUT){
|
||||||
writeMode = writeMode | bit(pin);
|
writeMode = writeMode | bit(pin);
|
||||||
|
if (output_start==HIGH) {
|
||||||
|
writeModeUp = writeModeUp | bit(pin);
|
||||||
|
}
|
||||||
|
|
||||||
readMode = readMode & ~bit(pin);
|
readMode = readMode & ~bit(pin);
|
||||||
DEBUG_PRINT("writeMode: ");
|
readModePullDown = readModePullDown & ~bit(pin);
|
||||||
|
readModePullUp = readModePullUp & ~bit(pin);
|
||||||
|
|
||||||
|
DEBUG_PRINT("W: ");
|
||||||
DEBUG_PRINT(writeMode, BIN);
|
DEBUG_PRINT(writeMode, BIN);
|
||||||
DEBUG_PRINT("readMode: ");
|
DEBUG_PRINT(" R ALL: ");
|
||||||
DEBUG_PRINTLN(readMode, BIN);
|
DEBUG_PRINT(readMode, BIN);
|
||||||
|
|
||||||
|
DEBUG_PRINT(" R Down: ");
|
||||||
|
DEBUG_PRINT(readModePullDown, BIN);
|
||||||
|
DEBUG_PRINT("R Up: ");
|
||||||
|
DEBUG_PRINTLN(readModePullUp, BIN);
|
||||||
|
|
||||||
}else if (mode == INPUT){
|
}else if (mode == INPUT){
|
||||||
writeMode = writeMode & ~bit(pin);
|
writeMode = writeMode & ~bit(pin);
|
||||||
readMode = readMode | bit(pin);
|
|
||||||
DEBUG_PRINT("writeMode: ");
|
readMode = readMode | bit(pin);
|
||||||
|
readModePullDown = readModePullDown | bit(pin);
|
||||||
|
readModePullUp = readModePullUp & ~bit(pin);
|
||||||
|
|
||||||
|
DEBUG_PRINT("W: ");
|
||||||
DEBUG_PRINT(writeMode, BIN);
|
DEBUG_PRINT(writeMode, BIN);
|
||||||
DEBUG_PRINT("readMode: ");
|
DEBUG_PRINT(" R ALL: ");
|
||||||
DEBUG_PRINTLN(readMode, BIN);
|
DEBUG_PRINT(readMode, BIN);
|
||||||
|
|
||||||
|
DEBUG_PRINT(" R Down: ");
|
||||||
|
DEBUG_PRINT(readModePullDown, BIN);
|
||||||
|
DEBUG_PRINT("R Up: ");
|
||||||
|
DEBUG_PRINTLN(readModePullUp, BIN);
|
||||||
|
}else if (mode == INPUT_PULLUP){
|
||||||
|
writeMode = writeMode & ~bit(pin);
|
||||||
|
|
||||||
|
readMode = readMode | bit(pin);
|
||||||
|
readModePullDown = readModePullDown & ~bit(pin);
|
||||||
|
readModePullUp = readModePullUp | bit(pin);
|
||||||
|
|
||||||
|
DEBUG_PRINT("W: ");
|
||||||
|
DEBUG_PRINT(writeMode, BIN);
|
||||||
|
DEBUG_PRINT(" R ALL: ");
|
||||||
|
DEBUG_PRINT(readMode, BIN);
|
||||||
|
|
||||||
|
DEBUG_PRINT(" R Down: ");
|
||||||
|
DEBUG_PRINT(readModePullDown, BIN);
|
||||||
|
DEBUG_PRINT("R Up: ");
|
||||||
|
DEBUG_PRINTLN(readModePullUp, BIN);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
DEBUG_PRINTLN("Mode non supported by PCF8574")
|
DEBUG_PRINTLN("Mode non supported by PCF8574")
|
||||||
}
|
}
|
||||||
DEBUG_PRINT("Write mode: ");
|
|
||||||
DEBUG_PRINTLN(writeMode, BIN);
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -239,8 +279,9 @@ void PCF8574::readBuffer(bool force){
|
|||||||
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){
|
if ((iInput & readModePullDown)>0 and (~iInput & readModePullUp)>0){
|
||||||
byteBuffered = byteBuffered | (byte)iInput;
|
// if ((iInput & readMode)>0){
|
||||||
|
byteBuffered = (byteBuffered & ~readMode) | (byte)iInput;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -261,13 +302,9 @@ void PCF8574::readBuffer(bool force){
|
|||||||
DEBUG_PRINTLN("Data ready");
|
DEBUG_PRINTLN("Data ready");
|
||||||
byte iInput = _wire->read();// Read a byte
|
byte iInput = _wire->read();// Read a byte
|
||||||
|
|
||||||
if ((iInput & readMode)>0){
|
if ((readModePullDown & iInput)>0 or (readModePullUp & ~iInput)>0){
|
||||||
DEBUG_PRINT("Input ");
|
DEBUG_PRINT(" -------- CHANGE --------- ");
|
||||||
DEBUG_PRINTLN((byte)iInput, BIN);
|
byteBuffered = (byteBuffered & ~readMode) | (byte)iInput;
|
||||||
|
|
||||||
byteBuffered = byteBuffered | (byte)iInput;
|
|
||||||
DEBUG_PRINT("byteBuffered ");
|
|
||||||
DEBUG_PRINTLN(byteBuffered, BIN);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -283,11 +320,11 @@ void PCF8574::readBuffer(bool force){
|
|||||||
if ((bit(6) & readMode)>0) digitalInput.p6 = ((byteBuffered & bit(6))>0)?HIGH:LOW;
|
if ((bit(6) & readMode)>0) digitalInput.p6 = ((byteBuffered & bit(6))>0)?HIGH:LOW;
|
||||||
if ((bit(7) & readMode)>0) digitalInput.p7 = ((byteBuffered & bit(7))>0)?HIGH:LOW;
|
if ((bit(7) & readMode)>0) digitalInput.p7 = ((byteBuffered & bit(7))>0)?HIGH:LOW;
|
||||||
|
|
||||||
if ((readMode & byteBuffered)>0){
|
//if ((byteBuffered & readModePullDown)>0 and (~byteBuffered & readModePullUp)>0){
|
||||||
byteBuffered = ~readMode & byteBuffered;
|
byteBuffered = (resetInitial & readMode) | (byteBuffered & ~readMode); //~readMode & byteBuffered;
|
||||||
DEBUG_PRINT("Buffer hight value readed set readed ");
|
DEBUG_PRINT("Buffer hight value readed set readed ");
|
||||||
DEBUG_PRINTLN(byteBuffered, BIN);
|
DEBUG_PRINTLN(byteBuffered, BIN);
|
||||||
}
|
//}
|
||||||
DEBUG_PRINT("Return value ");
|
DEBUG_PRINT("Return value ");
|
||||||
return digitalInput;
|
return digitalInput;
|
||||||
};
|
};
|
||||||
@ -306,13 +343,10 @@ void PCF8574::readBuffer(bool force){
|
|||||||
DEBUG_PRINTLN("Data ready");
|
DEBUG_PRINTLN("Data ready");
|
||||||
byte iInput = _wire->read();// Read a byte
|
byte iInput = _wire->read();// Read a byte
|
||||||
|
|
||||||
if ((iInput & readMode)>0){
|
if ((readModePullDown & iInput)>0 or (readModePullUp & ~iInput)>0){
|
||||||
DEBUG_PRINT("Input ");
|
DEBUG_PRINT(" -------- CHANGE --------- ");
|
||||||
DEBUG_PRINTLN((byte)iInput, BIN);
|
byteBuffered = (byteBuffered & ~readMode) | (byte)iInput;
|
||||||
|
|
||||||
byteBuffered = byteBuffered | (byte)iInput;
|
|
||||||
DEBUG_PRINT("byteBuffered ");
|
|
||||||
DEBUG_PRINTLN(byteBuffered, BIN);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -321,11 +355,11 @@ void PCF8574::readBuffer(bool force){
|
|||||||
|
|
||||||
byte byteRead = byteBuffered;
|
byte byteRead = byteBuffered;
|
||||||
|
|
||||||
if ((readMode & byteBuffered)>0){
|
//if ((byteBuffered & readModePullDown)>0 and (~byteBuffered & readModePullUp)>0){
|
||||||
byteBuffered = ~readMode & byteBuffered;
|
byteBuffered = (resetInitial & readMode) | (byteBuffered & ~readMode); //~readMode & byteBuffered;
|
||||||
DEBUG_PRINT("Buffer hight value readed set readed ");
|
DEBUG_PRINT("Buffer hight value readed set readed ");
|
||||||
DEBUG_PRINTLN(byteBuffered, BIN);
|
DEBUG_PRINTLN(byteBuffered, BIN);
|
||||||
}
|
//}
|
||||||
DEBUG_PRINT("Return value ");
|
DEBUG_PRINT("Return value ");
|
||||||
return byteRead;
|
return byteRead;
|
||||||
};
|
};
|
||||||
@ -338,45 +372,62 @@ void PCF8574::readBuffer(bool force){
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
uint8_t PCF8574::digitalRead(uint8_t pin){
|
uint8_t PCF8574::digitalRead(uint8_t pin){
|
||||||
uint8_t value = LOW;
|
uint8_t value = (bit(pin) & readModePullUp)?HIGH:LOW;
|
||||||
DEBUG_PRINT("Read pin ");
|
DEBUG_PRINT("Read pin ");
|
||||||
DEBUG_PRINTLN(pin);
|
DEBUG_PRINT (pin);
|
||||||
// Check if pin already HIGH than read and prevent reread of i2c
|
// Check if pin already HIGH than read and prevent reread of i2c
|
||||||
if ((bit(pin) & byteBuffered)>0){
|
// DEBUG_PRINTLN("----------------------------------")
|
||||||
DEBUG_PRINTLN("Pin already up");
|
// DEBUG_PRINT("readModePullUp ");
|
||||||
value = HIGH;
|
// DEBUG_PRINTLN(readModePullUp, BIN);
|
||||||
|
// DEBUG_PRINT("readModePullDown ");
|
||||||
|
// DEBUG_PRINTLN(readModePullDown, BIN);
|
||||||
|
// DEBUG_PRINT("byteBuffered ");
|
||||||
|
// DEBUG_PRINTLN(byteBuffered, BIN);
|
||||||
|
|
||||||
|
|
||||||
|
if (((bit(pin) & (readModePullDown & byteBuffered))>0) or (bit(pin) & (readModePullUp & ~byteBuffered))>0 ){
|
||||||
|
DEBUG_PRINTLN(" ...Pin already set");
|
||||||
|
if ((bit(pin) & byteBuffered)>0){
|
||||||
|
value = HIGH;
|
||||||
|
}else{
|
||||||
|
value = LOW;
|
||||||
|
}
|
||||||
}else if ((/*(bit(pin) & byteBuffered)<=0 && */millis() > PCF8574::lastReadMillis+READ_ELAPSED_TIME) /*|| _usingInterrupt*/){
|
}else if ((/*(bit(pin) & byteBuffered)<=0 && */millis() > PCF8574::lastReadMillis+READ_ELAPSED_TIME) /*|| _usingInterrupt*/){
|
||||||
DEBUG_PRINTLN("Read from buffer");
|
DEBUG_PRINT(" ...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();
|
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");
|
DEBUG_PRINTLN(" Data ready");
|
||||||
byte iInput = _wire->read();// Read a byte
|
byte iInput = _wire->read();// Read a byte
|
||||||
|
DEBUG_PRINT("Input ");
|
||||||
|
DEBUG_PRINT((byte)iInput, BIN);
|
||||||
|
|
||||||
if ((iInput & readMode)>0){
|
if ((readModePullDown & iInput)>0 or (readModePullUp & ~iInput)>0){
|
||||||
DEBUG_PRINT("Input ");
|
DEBUG_PRINT(" -------- CHANGE --------- ");
|
||||||
DEBUG_PRINTLN((byte)iInput, BIN);
|
byteBuffered = (byteBuffered & ~readMode) | (byte)iInput;
|
||||||
|
|
||||||
byteBuffered = byteBuffered | (byte)iInput;
|
|
||||||
DEBUG_PRINT("byteBuffered ");
|
|
||||||
DEBUG_PRINTLN(byteBuffered, BIN);
|
|
||||||
|
|
||||||
if ((bit(pin) & byteBuffered)>0){
|
if ((bit(pin) & byteBuffered)>0){
|
||||||
value = HIGH;
|
value = HIGH;
|
||||||
|
}else{
|
||||||
|
value = LOW;
|
||||||
}
|
}
|
||||||
|
// value = (bit(pin) & byteBuffered);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DEBUG_PRINT("Buffer value ");
|
DEBUG_PRINT(" ..Buffer value ");
|
||||||
DEBUG_PRINTLN(byteBuffered, BIN);
|
DEBUG_PRINT(byteBuffered, BIN);
|
||||||
// If HIGH set to low to read buffer only one time
|
// If HIGH set to low to read buffer only one time
|
||||||
if (value==HIGH){
|
if ((bit(pin) & readModePullDown) and value==HIGH){
|
||||||
byteBuffered = ~bit(pin) & byteBuffered;
|
byteBuffered = bit(pin) ^ byteBuffered;
|
||||||
DEBUG_PRINT("Buffer hight value readed set readed ");
|
DEBUG_PRINT(" ...Buffer hight value readed set readed ");
|
||||||
DEBUG_PRINTLN(byteBuffered, BIN);
|
DEBUG_PRINT (byteBuffered, BIN);
|
||||||
|
}else if ((bit(pin) & readModePullUp) and value==LOW){
|
||||||
|
byteBuffered = bit(pin) ^ byteBuffered;
|
||||||
|
DEBUG_PRINT(" ...Buffer low value readed set readed ");
|
||||||
|
DEBUG_PRINT(byteBuffered, BIN);
|
||||||
}
|
}
|
||||||
DEBUG_PRINT("Return value ");
|
DEBUG_PRINT(" ...Return value ");
|
||||||
DEBUG_PRINTLN(value);
|
DEBUG_PRINTLN(value);
|
||||||
return value;
|
return value;
|
||||||
};
|
};
|
||||||
@ -389,10 +440,17 @@ uint8_t PCF8574::digitalRead(uint8_t pin){
|
|||||||
void PCF8574::digitalWrite(uint8_t pin, uint8_t value){
|
void PCF8574::digitalWrite(uint8_t pin, uint8_t value){
|
||||||
DEBUG_PRINTLN("Begin trasmission");
|
DEBUG_PRINTLN("Begin trasmission");
|
||||||
_wire->beginTransmission(_address); //Begin the transmission to PCF8574
|
_wire->beginTransmission(_address); //Begin the transmission to PCF8574
|
||||||
|
DEBUG_PRINT("Value ");
|
||||||
|
DEBUG_PRINT(value);
|
||||||
|
DEBUG_PRINT(" Write data pre ");
|
||||||
|
DEBUG_PRINT(writeByteBuffered, BIN);
|
||||||
|
|
||||||
if (value==HIGH){
|
if (value==HIGH){
|
||||||
writeByteBuffered = writeByteBuffered | bit(pin);
|
writeByteBuffered = writeByteBuffered | bit(pin);
|
||||||
|
byteBuffered = writeByteBuffered | bit(pin);
|
||||||
}else{
|
}else{
|
||||||
writeByteBuffered = writeByteBuffered & ~bit(pin);
|
writeByteBuffered = writeByteBuffered & ~bit(pin);
|
||||||
|
byteBuffered = writeByteBuffered & ~bit(pin);
|
||||||
}
|
}
|
||||||
DEBUG_PRINT("Write data ");
|
DEBUG_PRINT("Write data ");
|
||||||
DEBUG_PRINT(writeByteBuffered, BIN);
|
DEBUG_PRINT(writeByteBuffered, BIN);
|
||||||
@ -401,10 +459,15 @@ void PCF8574::digitalWrite(uint8_t pin, uint8_t value){
|
|||||||
DEBUG_PRINT(" bin value ");
|
DEBUG_PRINT(" bin value ");
|
||||||
DEBUG_PRINT(bit(pin), BIN);
|
DEBUG_PRINT(bit(pin), BIN);
|
||||||
DEBUG_PRINT(" value ");
|
DEBUG_PRINT(" value ");
|
||||||
DEBUG_PRINTLN(value);
|
DEBUG_PRINT(value);
|
||||||
|
|
||||||
writeByteBuffered = writeByteBuffered & writeMode;
|
// writeByteBuffered = writeByteBuffered & (~writeMode & byteBuffered);
|
||||||
_wire->write(writeByteBuffered);
|
byteBuffered = (writeByteBuffered & writeMode) | (resetInitial & readMode);
|
||||||
|
DEBUG_PRINT(" byteBuffered ");
|
||||||
|
DEBUG_PRINTLN(byteBuffered, BIN);
|
||||||
|
|
||||||
|
_wire->write(byteBuffered);
|
||||||
|
// byteBuffered = (writeByteBuffered & writeMode) & (byteBuffered & readMode);
|
||||||
DEBUG_PRINTLN("Start end trasmission if stop here check pullup resistor.");
|
DEBUG_PRINTLN("Start end trasmission if stop here check pullup resistor.");
|
||||||
|
|
||||||
_wire->endTransmission();
|
_wire->endTransmission();
|
||||||
|
21
PCF8574.h
21
PCF8574.h
@ -45,6 +45,9 @@
|
|||||||
// Uncomment for low memory usage this prevent use of complex DigitalInput structure and free 7byte of memory
|
// Uncomment for low memory usage this prevent use of complex DigitalInput structure and free 7byte of memory
|
||||||
// #define PCF8574_LOW_MEMORY
|
// #define PCF8574_LOW_MEMORY
|
||||||
|
|
||||||
|
// Uncomment for low memory usage this prevent use of complex DigitalInput structure and free 7byte of memory
|
||||||
|
// #define PCF8574_LOW_LATENCY
|
||||||
|
|
||||||
// Define where debug output will be printed.
|
// Define where debug output will be printed.
|
||||||
#define DEBUG_PRINTER Serial
|
#define DEBUG_PRINTER Serial
|
||||||
|
|
||||||
@ -57,7 +60,11 @@
|
|||||||
#define DEBUG_PRINTLN(...) {}
|
#define DEBUG_PRINTLN(...) {}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define READ_ELAPSED_TIME 10
|
#ifdef PCF8574_LOW_LATENCY
|
||||||
|
#define READ_ELAPSED_TIME 1
|
||||||
|
#else
|
||||||
|
#define READ_ELAPSED_TIME 10
|
||||||
|
#endif
|
||||||
|
|
||||||
//#define P0 B00000001
|
//#define P0 B00000001
|
||||||
//#define P1 B00000010
|
//#define P1 B00000010
|
||||||
@ -101,7 +108,7 @@ public:
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
void begin();
|
void begin();
|
||||||
void pinMode(uint8_t pin, uint8_t mode);
|
void pinMode(uint8_t pin, uint8_t mode, uint8_t output_start = HIGH);
|
||||||
|
|
||||||
void readBuffer(bool force = true);
|
void readBuffer(bool force = true);
|
||||||
uint8_t digitalRead(uint8_t pin);
|
uint8_t digitalRead(uint8_t pin);
|
||||||
@ -145,9 +152,13 @@ private:
|
|||||||
uint8_t _interruptPin = 2;
|
uint8_t _interruptPin = 2;
|
||||||
void (*_interruptFunction)(){};
|
void (*_interruptFunction)(){};
|
||||||
|
|
||||||
byte writeMode = B00000000;
|
byte writeMode = B00000000;
|
||||||
byte readMode = B00000000;
|
byte writeModeUp = B00000000;
|
||||||
byte byteBuffered = B00000000;
|
byte readMode = B00000000;
|
||||||
|
byte readModePullUp = B00000000;
|
||||||
|
byte readModePullDown = B00000000;
|
||||||
|
byte byteBuffered = B00000000;
|
||||||
|
byte resetInitial = B00000000;
|
||||||
unsigned long lastReadMillis = 0;
|
unsigned long lastReadMillis = 0;
|
||||||
|
|
||||||
byte writeByteBuffered = B00000000;
|
byte writeByteBuffered = B00000000;
|
||||||
|
230
README.md
230
README.md
@ -1,114 +1,116 @@
|
|||||||
### Additional information and document update here on my site: [pcf8574 Article](https://www.mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/).
|
### Additional information and document update here on my site: [pcf8574 Article](https://www.mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/).
|
||||||
|
|
||||||
### If you need more pins [here](https://www.mischianti.org/2019/07/22/pcf8575-i2c-16-bit-digital-i-o-expander/) you can find pcf8575 16bit version of the IC.
|
### If you need more pins [here](https://www.mischianti.org/2019/07/22/pcf8575-i2c-16-bit-digital-i-o-expander/) you can find pcf8575 16bit version of the IC.
|
||||||
|
|
||||||
Library to use i2c analog IC with arduino and esp8266. Can read and write digital value with only 2 wire (perfect for ESP-01).
|
### Version 2.0
|
||||||
|
|
||||||
Tutorial:
|
Library to use i2c analog IC with arduino and esp8266. Can read and write digital value with only 2 wire (perfect for ESP-01).
|
||||||
|
|
||||||
To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder PCF8574. Check that the PCF8574 folder contains `PCF8574\\.cpp` and `PCF8574.h`. Place the DHT library folder your `<arduinosketchfolder>/libraries/` folder. You may need to create the libraries subfolder if its your first library. Restart the IDE.
|
Tutorial:
|
||||||
|
|
||||||
# Reef complete PCF8574 PCF8574AP digital input and output expander with i2c bus.
|
To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder PCF8574. Check that the PCF8574 folder contains `PCF8574\\.cpp` and `PCF8574.h`. Place the DHT library folder your `<arduinosketchfolder>/libraries/` folder. You may need to create the libraries subfolder if its your first library. Restart the IDE.
|
||||||
I try to simplify the use of this IC, with a minimal set of operation.
|
|
||||||
|
# Reef complete PCF8574 PCF8574AP digital input and output expander with i2c bus.
|
||||||
PCF8574P address map 0x20-0x27
|
I try to simplify the use of this IC, with a minimal set of operation.
|
||||||
PCF8574AP address map 0x38-0x3f
|
|
||||||
|
PCF8574P address map 0x20-0x27
|
||||||
Constructor:
|
PCF8574AP address map 0x38-0x3f
|
||||||
you must pas the address of i2c (to check the adress use this guide [I2cScanner](https://playground.arduino.cc/Main/I2cScanner))
|
|
||||||
```cpp
|
Constructor:
|
||||||
PCF8574(uint8_t address);
|
you must pas the address of i2c (to check the adress use this guide [I2cScanner](https://playground.arduino.cc/Main/I2cScanner))
|
||||||
```
|
```cpp
|
||||||
for esp8266 if you want specify SDA e SCL pin use this:
|
PCF8574(uint8_t address);
|
||||||
|
```
|
||||||
```cpp
|
for esp8266 if you want specify SDA e SCL pin use this:
|
||||||
PCF8574(uint8_t address, uint8_t sda, uint8_t scl);
|
|
||||||
```
|
```cpp
|
||||||
You must set input/output mode:
|
PCF8574(uint8_t address, uint8_t sda, uint8_t scl);
|
||||||
```cpp
|
```
|
||||||
pcf8574.pinMode(P0, OUTPUT);
|
You must set input/output mode:
|
||||||
pcf8574.pinMode(P1, INPUT);
|
```cpp
|
||||||
pcf8574.pinMode(P2, INPUT);
|
pcf8574.pinMode(P0, OUTPUT);
|
||||||
```
|
pcf8574.pinMode(P1, INPUT);
|
||||||
|
pcf8574.pinMode(P2, INPUT);
|
||||||
then IC as you can see in the image have 8 digital input/output:
|
```
|
||||||
|
|
||||||

|
then IC as you can see in the image have 8 digital input/output:
|
||||||
|
|
||||||
So to read all analog input in one trasmission you can do (even if I use a 10millis debounce time to prevent too much read from i2c):
|

|
||||||
```cpp
|
|
||||||
PCF8574::DigitalInput di = PCF8574.digitalReadAll();
|
So to read all analog input in one trasmission you can do (even if I use a 10millis debounce time to prevent too much read from i2c):
|
||||||
Serial.print(di.p0);
|
```cpp
|
||||||
Serial.print(" - ");
|
PCF8574::DigitalInput di = PCF8574.digitalReadAll();
|
||||||
Serial.print(di.p1);
|
Serial.print(di.p0);
|
||||||
Serial.print(" - ");
|
Serial.print(" - ");
|
||||||
Serial.print(di.p2);
|
Serial.print(di.p1);
|
||||||
Serial.print(" - ");
|
Serial.print(" - ");
|
||||||
Serial.println(di.p3);
|
Serial.print(di.p2);
|
||||||
```
|
Serial.print(" - ");
|
||||||
|
Serial.println(di.p3);
|
||||||
To follow a request (you can see It on [issue #5](https://github.com/xreef/PCF8574_library/issues/5)) I create a define variable to work with low memori device, if you decomment this line on .h file of the library:
|
```
|
||||||
|
|
||||||
```cpp
|
To follow a request (you can see It on [issue #5](https://github.com/xreef/PCF8574_library/issues/5)) I create a define variable to work with low memori device, if you decomment this line on .h file of the library:
|
||||||
// #define PCF8574_LOW_MEMORY
|
|
||||||
```
|
```cpp
|
||||||
|
// #define PCF8574_LOW_MEMORY
|
||||||
Enable low memory props and gain about 7byte of memory, and you must use the method to read all like so:
|
```
|
||||||
|
|
||||||
```cpp
|
Enable low memory props and gain about 7byte of memory, and you must use the method to read all like so:
|
||||||
byte di = pcf8574.digitalReadAll();
|
|
||||||
Serial.print("READ VALUE FROM PCF: ");
|
```cpp
|
||||||
Serial.println(di, BIN);
|
byte di = pcf8574.digitalReadAll();
|
||||||
```
|
Serial.print("READ VALUE FROM PCF: ");
|
||||||
|
Serial.println(di, BIN);
|
||||||
where di is a byte like 1110001, so you must do a bitwise operation to get the data, operation that I already do in the "normal" mode, here an example:
|
```
|
||||||
|
|
||||||
```cpp
|
where di is a byte like 1110001, so you must do a bitwise operation to get the data, operation that I already do in the "normal" mode, here an example:
|
||||||
p0 = ((di & bit(0))>0)?HIGH:LOW;
|
|
||||||
p1 = ((di & bit(1))>0)?HIGH:LOW;
|
```cpp
|
||||||
p2 = ((di & bit(2))>0)?HIGH:LOW;
|
p0 = ((di & bit(0))>0)?HIGH:LOW;
|
||||||
p3 = ((di & bit(3))>0)?HIGH:LOW;
|
p1 = ((di & bit(1))>0)?HIGH:LOW;
|
||||||
p4 = ((di & bit(4))>0)?HIGH:LOW;
|
p2 = ((di & bit(2))>0)?HIGH:LOW;
|
||||||
p5 = ((di & bit(5))>0)?HIGH:LOW;
|
p3 = ((di & bit(3))>0)?HIGH:LOW;
|
||||||
p6 = ((di & bit(6))>0)?HIGH:LOW;
|
p4 = ((di & bit(4))>0)?HIGH:LOW;
|
||||||
p7 = ((di & bit(7))>0)?HIGH:LOW;
|
p5 = ((di & bit(5))>0)?HIGH:LOW;
|
||||||
```
|
p6 = ((di & bit(6))>0)?HIGH:LOW;
|
||||||
|
p7 = ((di & bit(7))>0)?HIGH:LOW;
|
||||||
|
```
|
||||||
if you want read a single input:
|
|
||||||
|
|
||||||
```cpp
|
if you want read a single input:
|
||||||
int p1Digital = PCF8574.digitalRead(P1); // read P1
|
|
||||||
```
|
```cpp
|
||||||
|
int p1Digital = PCF8574.digitalRead(P1); // read P1
|
||||||
If you want write a digital value you must do:
|
```
|
||||||
```cpp
|
|
||||||
PCF8574.digitalWrite(P1, HIGH);
|
If you want write a digital value you must do:
|
||||||
```
|
```cpp
|
||||||
or:
|
PCF8574.digitalWrite(P1, HIGH);
|
||||||
```cpp
|
```
|
||||||
PCF8574.digitalWrite(P1, LOW);
|
or:
|
||||||
```
|
```cpp
|
||||||
|
PCF8574.digitalWrite(P1, LOW);
|
||||||
You can also use interrupt pin:
|
```
|
||||||
You must initialize the pin and the function to call when interrupt raised from PCF8574
|
|
||||||
```cpp
|
You can also use interrupt pin:
|
||||||
// Function interrupt
|
You must initialize the pin and the function to call when interrupt raised from PCF8574
|
||||||
void keyPressedOnPCF8574();
|
```cpp
|
||||||
|
// Function interrupt
|
||||||
// Set i2c address
|
void keyPressedOnPCF8574();
|
||||||
PCF8574 pcf8574(0x39, ARDUINO_UNO_INTERRUPT_PIN, keyPressedOnPCF8574);
|
|
||||||
```
|
// Set i2c address
|
||||||
Remember you can't use Serial or Wire on interrupt function.
|
PCF8574 pcf8574(0x39, ARDUINO_UNO_INTERRUPT_PIN, keyPressedOnPCF8574);
|
||||||
|
```
|
||||||
The better way is to set only a variable to read on loop:
|
Remember you can't use Serial or Wire on interrupt function.
|
||||||
```cpp
|
|
||||||
void keyPressedOnPCF8574(){
|
The better way is to set only a variable to read on loop:
|
||||||
// Interrupt called (No Serial no read no wire in this function, and DEBUG disabled on PCF library)
|
```cpp
|
||||||
keyPressed = true;
|
void keyPressedOnPCF8574(){
|
||||||
}
|
// Interrupt called (No Serial no read no wire in this function, and DEBUG disabled on PCF library)
|
||||||
```
|
keyPressed = true;
|
||||||
|
}
|
||||||
For the examples I use this wire schema on breadboard:
|
```
|
||||||

|
|
||||||
|
For the examples I use this wire schema on breadboard:
|
||||||
|

|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name=PCF8574 library
|
name=PCF8574 library
|
||||||
version=1.2.0
|
version=2.0.0
|
||||||
author=Reef
|
author=Reef
|
||||||
maintainer=Renzo Mischianti <renzo.mischianti@gmail.com>
|
maintainer=Renzo Mischianti <renzo.mischianti@gmail.com>
|
||||||
sentence=Arduino/ESP8266 library for PCF8574
|
sentence=Arduino/ESP8266 library for PCF8574
|
||||||
|
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user