how to write data directly to registers?

hey,
how how can I write data directly to registers?
the only command I know is

   Wire.beginTransmission(addr);
   Wire.write(databyte);
   Wire.endTransmission();
  • but no register there to write to.

How can I do something like

   i2cwrite(int devaddr, int regaddr, int ndatabytes, char *data)

?

Perhaps in the future. I think the Arduino Due with the ARM M3 chip can do such things.
The requestFrom() has it already, but not documented.
https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/libraries/Wire/Wire.cpp
There is no undocumented i2cwrite that does it all yet. Write your own function.

Perhaps I misunderstood your question ?
The Wire.write() is used for the register address and for the data. The amount of bytes depends on how many bytes the register address is and how many bytes are written.

it's for writing directly 1 byte, e.g. to a devaddr 0x61, regaddr 7, and another byte e.g to regaddr 11 or 15.

ArthurD:
it’s for writing directly 1 byte, e.g. to a devaddr 0x61, regaddr 7, and another byte e.g to regaddr 11 or 15.

bool i2cwrite(byte devaddr, int regaddr, byte ndatabytes, char *data){
Wire.beginTransmission(devaddr);
Wire.write(HIGH(regaddr)); // this assumes the device expects MSB,LSB
Wire.write(LOW(regaddr));
if(ndatabytes>29) ndatabytes=29; // Wire's internal transmit buffer is 32 bytes
for(byte i=0;i<ndatabytes;i++){
  Wire.write(data[i]);
  }
return (Wire.endTransmission(true)==0);
}

ArthurD:
thank you Sir, but do you have that a little less snooty?
Finally Arduino is not for pro's but for beginners!

I appreciate your input very much, but I never read that in ANY Arduino tutorial or Playground ressource...

why is that so?

You had never read that Arduino's can do I2C communications using the WIRE.h library?

(You identified the correct library, and the correct function from the library to use? But did not realize you needed to use them?)

Or that you can use C++ to write functions?

I'm sorry I miss understood you.

Chuck.

this part was what I never read about anywhere - nowhere an example was given how to address a register by which code for writing! - :

Wire.write(HIGH(regaddr)); // this assumes the device expects MSB,LSB
Wire.write(LOW(regaddr));
if(ndatabytes>29) ndatabytes=29; // Wire's internal transmit buffer is 32 bytes

Nothing about HIGH(regaddr) and LOW(regaddr) and MSB or LSB first or last or other way round or if anyhow, and how and where and when to pass that to whatever.
The REST is simple of course then.

Is there something in the water tonight? Give it up with the insults. Posts removed, insults redacted.

  • Moderator

The I2c protocol doesn’t define message structures down to the "register " level. Those are specific to the chip you are talking to, and thus not appropriate for documentation at the arduino level…

aha, thank you, that's indeed new to me.
would it be possible then perhaps to provide some specific example source codes (no libs!) how to access e.g.,
PCF8574 (edited, has no registers)
PCF8591
MCP23017
to write to (and perhaps to read from if it's non-standard too) ?

Some of the popular chips have have additional libraries provided by Shield Vendors, for example for the MCP23017 there is GitHub - adafruit/Adafruit-MCP23017-Arduino-Library (it even has the readRegister/writeRegister functions you were looking for.)
I didn't see anything for the 8591, but googling "PCF8591 Arduino library" turns up a couple of "tutorials."

Here's a post about I2C and the MCP23017.

thank you very much! Now I already understand a little more about the topic, e.g.
Wire.beginTransmission (0x20); // expander has I2C address 0x20
Wire.send (0x00); // register 0 is the I/O direction register for Port A
Wire.send (0x00); // 0x00 for all pins to output mode, 0xFF for all pins to input mode
Wire.endTransmission ();

Wire.beginTransmission (0x20); // expander has I2C address 0x20
Wire.send (0x12); // register 0x12 is the I/O port "A"
Wire.send (0x56); // what to put on that port
Wire.endTransmission ();

...
Wire.requestFrom (0x20, (byte) 1);
byte data = Wire.receive ();

I did'n understand so far that there is no general system to do this but each device would need it's own commands.

Thank you very much for your replies!

BTW,

which are the MCP23017 registers of port B to write/read?

edit:
found it out, at the first link by westfw!

I/O Port "B" direction register == 0x01 (A == 0x00)
I/O port "B" data register == 0x13 (A == 0x12)

thanks again!