1

I want to read a register 0x012A from the device I2C address 0x0A, the value stored in the register is two bytes, If the register address is one byte i can use Wire.requestFrom and give the register address and how many bytes i want to read since here register address itself 2 bytes i'am bit confused . can any one help in this regard.

1
  • 1
    Have a look at github.com/rodan/ds3231/blob/master/ds3231.cpp#L409-L413 . As far as I can see. You begin a transmition to the device; you write your two registry address bytes; then close the transmission. After that you use requestFrom to read any number of bytes (which happens to be two as well, in your case). – Gerben Oct 28 '20 at 14:16
3

i can use Wire.requestFrom and give the register address and how many bytes i want to read

So you give it 2 to ask for 2 bytes.

Then you combine them. How you do that depends on what the bytes represent. If the first is the upper byte and the second the lower byte of a 16 bit value then you would:

uint8_t b1 = Wire.read();
uint8_t b2 = Wire.read();

uint16_t val = (b1 << 8) | b2;

If it's the other way around then just swap the variables in the combining section.

However if the values are some other arrangement (I saw something recently that had two registers with a 12 bit value with overlapping bits) you will have to combine it in the way that is required for that specific arrangement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.