mirror of
https://github.com/xreef/PCF8574_library.git
synced 2024-08-30 18:12:18 +00:00
New tested 2.0 version
This commit is contained in:
parent
fac5ad5c8e
commit
2509754e5e
182
PCF8574.cpp
182
PCF8574.cpp
@ -169,17 +169,16 @@ void PCF8574::begin(){
|
||||
if (writeMode>0 || readMode>0){
|
||||
DEBUG_PRINTLN("Set write mode");
|
||||
_wire->beginTransmission(_address);
|
||||
DEBUG_PRINT(" ");
|
||||
DEBUG_PRINT("usedPin pin ");
|
||||
|
||||
|
||||
byte usedPin = writeMode | readModePullUp;
|
||||
DEBUG_PRINTLN( writeMode, BIN);
|
||||
DEBUG_PRINTLN( readModePullUp, BIN);
|
||||
DEBUG_PRINTLN( readModePullDown, BIN);
|
||||
DEBUG_PRINTLN( ~usedPin, BIN);
|
||||
DEBUG_PRINT("resetInitial pin ");
|
||||
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.");
|
||||
_wire->endTransmission();
|
||||
@ -187,9 +186,12 @@ void PCF8574::begin(){
|
||||
|
||||
// If using interrupt set interrupt value to pin
|
||||
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)");
|
||||
::pinMode(_interruptPin, INPUT_PULLUP);
|
||||
attachInterrupt(digitalPinToInterrupt(_interruptPin), (*_interruptFunction), FALLING );
|
||||
attachInterrupt(digitalPinToInterrupt(_interruptPin), (*_interruptFunction), CHANGE );
|
||||
}
|
||||
|
||||
// inizialize last read
|
||||
@ -199,9 +201,10 @@ void PCF8574::begin(){
|
||||
/**
|
||||
* Set if fin is OUTPUT or INPUT
|
||||
* @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(pin);
|
||||
DEBUG_PRINT(" as ");
|
||||
@ -209,15 +212,23 @@ void PCF8574::pinMode(uint8_t pin, uint8_t mode){
|
||||
|
||||
if (mode == OUTPUT){
|
||||
writeMode = writeMode | bit(pin);
|
||||
if (output_start==HIGH) {
|
||||
writeModeUp = writeModeUp | bit(pin);
|
||||
}
|
||||
|
||||
readMode = readMode & ~bit(pin);
|
||||
readModePullDown = readModePullDown & ~bit(pin);
|
||||
readModePullUp = readModePullUp & ~bit(pin);
|
||||
|
||||
DEBUG_PRINT("writeMode: ");
|
||||
DEBUG_PRINT("W: ");
|
||||
DEBUG_PRINT(writeMode, BIN);
|
||||
DEBUG_PRINT("readMode ALL: ");
|
||||
DEBUG_PRINTLN(readMode, 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 if (mode == INPUT){
|
||||
writeMode = writeMode & ~bit(pin);
|
||||
@ -226,10 +237,15 @@ void PCF8574::pinMode(uint8_t pin, uint8_t mode){
|
||||
readModePullDown = readModePullDown | bit(pin);
|
||||
readModePullUp = readModePullUp & ~bit(pin);
|
||||
|
||||
DEBUG_PRINT("writeMode: ");
|
||||
DEBUG_PRINT("W: ");
|
||||
DEBUG_PRINT(writeMode, BIN);
|
||||
DEBUG_PRINT("readMode ALL: ");
|
||||
DEBUG_PRINTLN(readMode, 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 if (mode == INPUT_PULLUP){
|
||||
writeMode = writeMode & ~bit(pin);
|
||||
|
||||
@ -237,17 +253,19 @@ void PCF8574::pinMode(uint8_t pin, uint8_t mode){
|
||||
readModePullDown = readModePullDown & ~bit(pin);
|
||||
readModePullUp = readModePullUp | bit(pin);
|
||||
|
||||
DEBUG_PRINT("writeMode: ");
|
||||
DEBUG_PRINT("W: ");
|
||||
DEBUG_PRINT(writeMode, BIN);
|
||||
DEBUG_PRINT("readMode ALL: ");
|
||||
DEBUG_PRINTLN(readMode, 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{
|
||||
DEBUG_PRINTLN("Mode non supported by PCF8574")
|
||||
}
|
||||
DEBUG_PRINT("Write mode: ");
|
||||
DEBUG_PRINTLN(writeMode, BIN);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
@ -263,7 +281,7 @@ void PCF8574::readBuffer(bool force){
|
||||
byte iInput = _wire->read();// Read a byte
|
||||
if ((iInput & readModePullDown)>0 and (~iInput & readModePullUp)>0){
|
||||
// if ((iInput & readMode)>0){
|
||||
byteBuffered = byteBuffered | (byte)iInput;
|
||||
byteBuffered = (byteBuffered & ~readMode) | (byte)iInput;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -284,14 +302,9 @@ void PCF8574::readBuffer(bool force){
|
||||
DEBUG_PRINTLN("Data ready");
|
||||
byte iInput = _wire->read();// Read a byte
|
||||
|
||||
if ((iInput & readModePullDown)>0 and (~iInput & readModePullUp)>0){
|
||||
// if ((iInput & readMode)>0){
|
||||
DEBUG_PRINT("Input ");
|
||||
DEBUG_PRINTLN((byte)iInput, BIN);
|
||||
|
||||
byteBuffered = byteBuffered | (byte)iInput;
|
||||
DEBUG_PRINT("byteBuffered ");
|
||||
DEBUG_PRINTLN(byteBuffered, BIN);
|
||||
if ((readModePullDown & iInput)>0 or (readModePullUp & ~iInput)>0){
|
||||
DEBUG_PRINT(" -------- CHANGE --------- ");
|
||||
byteBuffered = (byteBuffered & ~readMode) | (byte)iInput;
|
||||
}
|
||||
}
|
||||
|
||||
@ -307,11 +320,11 @@ void PCF8574::readBuffer(bool force){
|
||||
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 ((byteBuffered & readModePullDown)>0 and (~byteBuffered & readModePullUp)>0){
|
||||
byteBuffered = ~readMode & byteBuffered;
|
||||
//if ((byteBuffered & readModePullDown)>0 and (~byteBuffered & readModePullUp)>0){
|
||||
byteBuffered = (resetInitial & readMode) | (byteBuffered & ~readMode); //~readMode & byteBuffered;
|
||||
DEBUG_PRINT("Buffer hight value readed set readed ");
|
||||
DEBUG_PRINTLN(byteBuffered, BIN);
|
||||
}
|
||||
//}
|
||||
DEBUG_PRINT("Return value ");
|
||||
return digitalInput;
|
||||
};
|
||||
@ -330,14 +343,10 @@ void PCF8574::readBuffer(bool force){
|
||||
DEBUG_PRINTLN("Data ready");
|
||||
byte iInput = _wire->read();// Read a byte
|
||||
|
||||
if ((iInput & readModePullDown)>0 and (~iInput & readModePullUp)>0){
|
||||
// if ((iInput & readMode)>0){
|
||||
DEBUG_PRINT("Input ");
|
||||
DEBUG_PRINTLN((byte)iInput, BIN);
|
||||
if ((readModePullDown & iInput)>0 or (readModePullUp & ~iInput)>0){
|
||||
DEBUG_PRINT(" -------- CHANGE --------- ");
|
||||
byteBuffered = (byteBuffered & ~readMode) | (byte)iInput;
|
||||
|
||||
byteBuffered = byteBuffered | (byte)iInput;
|
||||
DEBUG_PRINT("byteBuffered ");
|
||||
DEBUG_PRINTLN(byteBuffered, BIN);
|
||||
}
|
||||
}
|
||||
|
||||
@ -346,12 +355,11 @@ void PCF8574::readBuffer(bool force){
|
||||
|
||||
byte byteRead = byteBuffered;
|
||||
|
||||
if ((byteBuffered & readModePullDown)>0 and (~byteBuffered & readModePullUp)>0){
|
||||
// if ((readMode & byteBuffered)>0){
|
||||
byteBuffered = ~readMode & byteBuffered;
|
||||
//if ((byteBuffered & readModePullDown)>0 and (~byteBuffered & readModePullUp)>0){
|
||||
byteBuffered = (resetInitial & readMode) | (byteBuffered & ~readMode); //~readMode & byteBuffered;
|
||||
DEBUG_PRINT("Buffer hight value readed set readed ");
|
||||
DEBUG_PRINTLN(byteBuffered, BIN);
|
||||
}
|
||||
//}
|
||||
DEBUG_PRINT("Return value ");
|
||||
return byteRead;
|
||||
};
|
||||
@ -364,56 +372,62 @@ void PCF8574::readBuffer(bool force){
|
||||
* @return
|
||||
*/
|
||||
uint8_t PCF8574::digitalRead(uint8_t pin){
|
||||
uint8_t value = (bit(pin) & readModePullDown)?LOW:HIGH;
|
||||
uint8_t value = (bit(pin) & readModePullUp)?HIGH:LOW;
|
||||
DEBUG_PRINT("Read pin ");
|
||||
DEBUG_PRINTLN(pin);
|
||||
DEBUG_PRINT (pin);
|
||||
// Check if pin already HIGH than read and prevent reread of i2c
|
||||
if (((bit(pin) & readModePullDown & byteBuffered)>0) and (bit(pin) & readModePullUp & ~byteBuffered) ){
|
||||
DEBUG_PRINTLN("Pin already set");
|
||||
if ((bit(pin) & readModePullDown & byteBuffered)>0) {
|
||||
value = HIGH;
|
||||
}else{
|
||||
value = LOW;
|
||||
}
|
||||
// DEBUG_PRINTLN("----------------------------------")
|
||||
// DEBUG_PRINT("readModePullUp ");
|
||||
// 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*/){
|
||||
DEBUG_PRINTLN("Read from buffer");
|
||||
DEBUG_PRINT(" ...Read from buffer... ");
|
||||
_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
|
||||
{
|
||||
DEBUG_PRINTLN("Data ready");
|
||||
DEBUG_PRINTLN(" Data ready");
|
||||
byte iInput = _wire->read();// Read a byte
|
||||
DEBUG_PRINT("Input ");
|
||||
DEBUG_PRINT((byte)iInput, BIN);
|
||||
|
||||
if ((iInput & readModePullDown)>0 and (~iInput & readModePullUp)>0){
|
||||
// if ((iInput & readMode)>0){
|
||||
DEBUG_PRINT("Input ");
|
||||
DEBUG_PRINTLN((byte)iInput, BIN);
|
||||
|
||||
byteBuffered = byteBuffered | (byte)iInput;
|
||||
DEBUG_PRINT("byteBuffered ");
|
||||
DEBUG_PRINTLN(byteBuffered, BIN);
|
||||
|
||||
if ((readModePullDown & iInput)>0 or (readModePullUp & ~iInput)>0){
|
||||
DEBUG_PRINT(" -------- CHANGE --------- ");
|
||||
byteBuffered = (byteBuffered & ~readMode) | (byte)iInput;
|
||||
if ((bit(pin) & byteBuffered)>0){
|
||||
value = HIGH;
|
||||
}else{
|
||||
value = LOW;
|
||||
}
|
||||
// value = (bit(pin) & byteBuffered);
|
||||
}
|
||||
}
|
||||
}
|
||||
DEBUG_PRINT("Buffer value ");
|
||||
DEBUG_PRINTLN(byteBuffered, BIN);
|
||||
DEBUG_PRINT(" ..Buffer value ");
|
||||
DEBUG_PRINT(byteBuffered, BIN);
|
||||
// If HIGH set to low to read buffer only one time
|
||||
if ((bit(pin) & readModePullDown) and value==HIGH){
|
||||
byteBuffered = ~bit(pin) & byteBuffered;
|
||||
DEBUG_PRINT("Buffer hight value readed set readed ");
|
||||
DEBUG_PRINTLN(byteBuffered, BIN);
|
||||
byteBuffered = bit(pin) ^ byteBuffered;
|
||||
DEBUG_PRINT(" ...Buffer hight value readed set readed ");
|
||||
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_PRINTLN(byteBuffered, BIN);
|
||||
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);
|
||||
return value;
|
||||
};
|
||||
@ -426,10 +440,17 @@ 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
|
||||
DEBUG_PRINT("Value ");
|
||||
DEBUG_PRINT(value);
|
||||
DEBUG_PRINT(" Write data pre ");
|
||||
DEBUG_PRINT(writeByteBuffered, BIN);
|
||||
|
||||
if (value==HIGH){
|
||||
writeByteBuffered = writeByteBuffered | bit(pin);
|
||||
byteBuffered = writeByteBuffered | bit(pin);
|
||||
}else{
|
||||
writeByteBuffered = writeByteBuffered & ~bit(pin);
|
||||
byteBuffered = writeByteBuffered & ~bit(pin);
|
||||
}
|
||||
DEBUG_PRINT("Write data ");
|
||||
DEBUG_PRINT(writeByteBuffered, BIN);
|
||||
@ -438,10 +459,15 @@ void PCF8574::digitalWrite(uint8_t pin, uint8_t value){
|
||||
DEBUG_PRINT(" bin value ");
|
||||
DEBUG_PRINT(bit(pin), BIN);
|
||||
DEBUG_PRINT(" value ");
|
||||
DEBUG_PRINTLN(value);
|
||||
DEBUG_PRINT(value);
|
||||
|
||||
writeByteBuffered = writeByteBuffered & writeMode;
|
||||
_wire->write(writeByteBuffered);
|
||||
// writeByteBuffered = writeByteBuffered & (~writeMode & byteBuffered);
|
||||
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.");
|
||||
|
||||
_wire->endTransmission();
|
||||
|
13
PCF8574.h
13
PCF8574.h
@ -45,6 +45,9 @@
|
||||
// Uncomment for low memory usage this prevent use of complex DigitalInput structure and free 7byte of 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 DEBUG_PRINTER Serial
|
||||
|
||||
@ -57,7 +60,11 @@
|
||||
#define DEBUG_PRINTLN(...) {}
|
||||
#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 P1 B00000010
|
||||
@ -101,7 +108,7 @@ public:
|
||||
#endif
|
||||
|
||||
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);
|
||||
uint8_t digitalRead(uint8_t pin);
|
||||
@ -146,10 +153,12 @@ private:
|
||||
void (*_interruptFunction)(){};
|
||||
|
||||
byte writeMode = B00000000;
|
||||
byte writeModeUp = B00000000;
|
||||
byte readMode = B00000000;
|
||||
byte readModePullUp = B00000000;
|
||||
byte readModePullDown = B00000000;
|
||||
byte byteBuffered = B00000000;
|
||||
byte resetInitial = B00000000;
|
||||
unsigned long lastReadMillis = 0;
|
||||
|
||||
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/).
|
||||
|
||||
### 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).
|
||||
|
||||
Tutorial:
|
||||
|
||||
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.
|
||||
|
||||
# Reef complete PCF8574 PCF8574AP digital input and output expander with i2c bus.
|
||||
I try to simplify the use of this IC, with a minimal set of operation.
|
||||
|
||||
PCF8574P address map 0x20-0x27
|
||||
PCF8574AP address map 0x38-0x3f
|
||||
|
||||
Constructor:
|
||||
you must pas the address of i2c (to check the adress use this guide [I2cScanner](https://playground.arduino.cc/Main/I2cScanner))
|
||||
```cpp
|
||||
PCF8574(uint8_t address);
|
||||
```
|
||||
for esp8266 if you want specify SDA e SCL pin use this:
|
||||
|
||||
```cpp
|
||||
PCF8574(uint8_t address, uint8_t sda, uint8_t scl);
|
||||
```
|
||||
You must set input/output mode:
|
||||
```cpp
|
||||
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:
|
||||
|
||||
![PCF8574 schema](https://github.com/xreef/PCF8574_library/blob/master/resources/PCF8574-pins.gif)
|
||||
|
||||
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();
|
||||
Serial.print(di.p0);
|
||||
Serial.print(" - ");
|
||||
Serial.print(di.p1);
|
||||
Serial.print(" - ");
|
||||
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
|
||||
// #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
|
||||
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
|
||||
p0 = ((di & bit(0))>0)?HIGH:LOW;
|
||||
p1 = ((di & bit(1))>0)?HIGH:LOW;
|
||||
p2 = ((di & bit(2))>0)?HIGH:LOW;
|
||||
p3 = ((di & bit(3))>0)?HIGH:LOW;
|
||||
p4 = ((di & bit(4))>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
|
||||
int p1Digital = PCF8574.digitalRead(P1); // read P1
|
||||
```
|
||||
|
||||
If you want write a digital value you must do:
|
||||
```cpp
|
||||
PCF8574.digitalWrite(P1, HIGH);
|
||||
```
|
||||
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
|
||||
// Function interrupt
|
||||
void keyPressedOnPCF8574();
|
||||
|
||||
// Set i2c address
|
||||
PCF8574 pcf8574(0x39, ARDUINO_UNO_INTERRUPT_PIN, keyPressedOnPCF8574);
|
||||
```
|
||||
Remember you can't use Serial or Wire on interrupt function.
|
||||
|
||||
The better way is to set only a variable to read on loop:
|
||||
```cpp
|
||||
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:
|
||||
![Breadboard](https://github.com/xreef/PCF8574_library/blob/master/resources/testReadWriteLedButton_bb.png)
|
||||
|
||||
### 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.
|
||||
|
||||
### Version 2.0
|
||||
|
||||
Library to use i2c analog IC with arduino and esp8266. Can read and write digital value with only 2 wire (perfect for ESP-01).
|
||||
|
||||
Tutorial:
|
||||
|
||||
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.
|
||||
|
||||
# Reef complete PCF8574 PCF8574AP digital input and output expander with i2c bus.
|
||||
I try to simplify the use of this IC, with a minimal set of operation.
|
||||
|
||||
PCF8574P address map 0x20-0x27
|
||||
PCF8574AP address map 0x38-0x3f
|
||||
|
||||
Constructor:
|
||||
you must pas the address of i2c (to check the adress use this guide [I2cScanner](https://playground.arduino.cc/Main/I2cScanner))
|
||||
```cpp
|
||||
PCF8574(uint8_t address);
|
||||
```
|
||||
for esp8266 if you want specify SDA e SCL pin use this:
|
||||
|
||||
```cpp
|
||||
PCF8574(uint8_t address, uint8_t sda, uint8_t scl);
|
||||
```
|
||||
You must set input/output mode:
|
||||
```cpp
|
||||
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:
|
||||
|
||||
![PCF8574 schema](https://github.com/xreef/PCF8574_library/blob/master/resources/PCF8574-pins.gif)
|
||||
|
||||
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();
|
||||
Serial.print(di.p0);
|
||||
Serial.print(" - ");
|
||||
Serial.print(di.p1);
|
||||
Serial.print(" - ");
|
||||
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
|
||||
// #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
|
||||
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
|
||||
p0 = ((di & bit(0))>0)?HIGH:LOW;
|
||||
p1 = ((di & bit(1))>0)?HIGH:LOW;
|
||||
p2 = ((di & bit(2))>0)?HIGH:LOW;
|
||||
p3 = ((di & bit(3))>0)?HIGH:LOW;
|
||||
p4 = ((di & bit(4))>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
|
||||
int p1Digital = PCF8574.digitalRead(P1); // read P1
|
||||
```
|
||||
|
||||
If you want write a digital value you must do:
|
||||
```cpp
|
||||
PCF8574.digitalWrite(P1, HIGH);
|
||||
```
|
||||
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
|
||||
// Function interrupt
|
||||
void keyPressedOnPCF8574();
|
||||
|
||||
// Set i2c address
|
||||
PCF8574 pcf8574(0x39, ARDUINO_UNO_INTERRUPT_PIN, keyPressedOnPCF8574);
|
||||
```
|
||||
Remember you can't use Serial or Wire on interrupt function.
|
||||
|
||||
The better way is to set only a variable to read on loop:
|
||||
```cpp
|
||||
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:
|
||||
![Breadboard](https://github.com/xreef/PCF8574_library/blob/master/resources/testReadWriteLedButton_bb.png)
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=PCF8574 library
|
||||
version=0.9.0
|
||||
version=2.0.0
|
||||
author=Reef
|
||||
maintainer=Renzo Mischianti <renzo.mischianti@gmail.com>
|
||||
sentence=Arduino/ESP8266 library for PCF8574
|
||||
|
Loading…
Reference in New Issue
Block a user