Hi. I'm working on using a PCF8575 port expander to read a keypad 8x8 matrix.
This board has two bytes of values, first byte for pins 0 to 7, and the second byte for pins from 8 to 15.
In the setup function, I set the first byte as 0xFF and the second one as 0x00.
If I jump any pin from pins 8 to 15 (that are all low), to any pin from 0 to 7 it returns a 0 in the proper 8-bit BIN value in the first byte. For instance, if I short pins P12 and P02, I get: 11011111 00000000.
Now, if I change the value of the second byte to 0xF7 it means that not jumping any pin, I get a read of 11111111 11101111. In this case, in order for the first byte to react, I can only do it by jumping any pin from pin P13. Any other pins from 8 to 15 will do nothing.
So far so good, that's the basic principle of a matrix, you turn one column low (in this case columns would be byte 2, aka pins from 8 to 15) at a time, and scan each row separatedly.
So I have a couple of questions to proceed from here:
- How can I do a for function that jumps from 11111110, to 11111101, to 11111011, etc?
- the method to read and write these boards, since they consist of two bytes, is to run the Wire.write() function twice, as you can see, in the setup function (code below). For the matrix function I'm working on, I need to be able to write a value the first byte, then read the second byte, then write again the first byte, and so on so forth. How would you suggest I do this?
Test code for now reads what pins from 0 to 7 are jumped:
#include "Wire.h"
#define address 0x20
void setup() {
Serial.begin(115200);
Wire.begin();
Wire.beginTransmission(address);
Wire.write(0xFF);
Wire.write(0x00);
Wire.endTransmission();
}
void loop() {
byte read_value;
if (Wire.requestFrom(address, 2) == 2) {
read_value = (Wire.read());
for (int i = 0; i < 8; i++) {
Serial.print(bitRead(read_value, i));
}
Serial.print("\t");
read_value = (Wire.read());
for (int i = 0; i < 8; i++) {
Serial.print(bitRead(read_value, i));
}
Serial.println();
delay(500);
}
}

