Hi, I was wondering if anyone could help me out. I am using the PCF8575 from SparkFun to learn I2C. I have successfully gotten to the point Where I can send info to the chip to make LED’s light up on the pins I desire. However reading from it has become quite frustrating.
I have the below code currently and it does not work at all. It is obvious that I do not understand how this chip communicates. In the DATA SHEET it says that you have to write all ones to the chip first to select inputs, but When I tried that it did not act like i thought it should. I than tried to access only pins 10-17 to read from and use pins 0 -7 as the outputs to light LED’s. Again with no luck. Could you please help me by either correcting my code below, or be writing me a short read script so that I can dissect it, and understand what to do. I am currently looking to read a switch on one register, and than light an LED on the opposite register. Yout time is much appreciated.
#include <Wire.h>
byte address = 0x20; // address of PCF8575 with A0-A2 connected to GND "B01000000"
byte input;
byte c;
byte d;
void setup()
{
Wire.begin(); // join i2c bus
}
//control the pins
void loop(){
byte a = B11000000; //controls pins 0 - 7
byte b = B00110011; //controls pins 10-17
// by changing the 1's and 0's above you can control which pins are on and off.
// Remember the chip can sink more than it can source So the 1's are off and the 0's are on.
input = Wire.requestFrom(0x20,2);
if(input == 2) {
c = Wire.receive();
d = Wire.receive();
}
if(d == B00010000) {
Wire.beginTransmission(address); // send the address and the write cmnd
Wire.send(a);
Wire.endTransmission();
} // pack the first byte
else {
Wire.beginTransmission(address); // send the address and the write cmnd
Wire.send(b); // pack the second byte
Wire.endTransmission();
} // send the data
}