but I have some questions:
(my questions as comments)
#define DIP_ADDRESS (0x4 << 3 | 0x0) // how should I understand that << and |
Wire.send(0xff & dir); // the same question with &
Wire.send(dir >> 8); // the same question with >>
//What make that three lines?
and one more question here:
#define REGISTER_CONFIG (6) // has REGISTER_CONFIG a value 6?
Wire.send(REGISTER_CONFIG); // why am I sending 6?
To write to a register, send the chip the register address followed by the desired data.
The full code is :
#define REGISTER_CONFIG (6)
void gpio_dir(int address, int dir) {
// Send config register address
Wire.beginTransmission(address);
Wire.send(REGISTER_CONFIG);
// Connect to device and send two bytes
Wire.send(0xff & dir); // low byte
Wire.send(dir >> 8); // high byte
Wire.endTransmission();
}
You're using a PCA9555, 16bit I/O expander, so by looking at the datasheet pf the chip, you see that
The Configuration registers (registers 6 and 7) configure the directions of the I/O pins. If a bit in this register is
set to 1, the corresponding port pin is enabled as an input with a high-impedance output driver. If a bit in this
register is cleared to 0, the corresponding port pin is enabled as an output.
Register 6 is the configuration for 8 I/O pins, register 7 for the 8 olthers pins.
So, you are sending the number 6 to the chip to tell it that you want to access the register number 6 (REGISTER_CONFIG = 6).
In the code from the page, the variable dir is an integer used to tell which pins will be an input & which ones will be outputs.
On the page you linked
Register 6 is the configuration for 8 I/O pins, register 7 for the 8 olthers pins.
So, you are sending the number 6 to the chip to tell it that you want to access the register number 6 (REGISTER_CONFIG = 6).
Now I understand...
but have one more question..
I've found a datasheet: HTTP 301 This page has been moved
"... This register is an input-only port. It reflects the incoming logic levels
of the pins,..." (site 6)
if I understand I can not set all 16 pins as output or input?
Is there a I/O I2C chip where I can set all 16 pins how I want?