0

Leading zeros problem
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Leading zeros problem

by BadOsc on Tue Dec 04, 2018 1:23 pm

I need to write to an I2C bus

The problem is that i really need to send 0x000017 with this command to a device

<<bus_device.write(bytes([A_DEVICE_REGISTER]), stop=True)>>

When i set A_DEVICE_REGISTER to 0x000017 0x17 is send.
That's not what is needed for the device i want to send it to, the leading zero's must be send also

Anyone any idea how to solve this

Warm regards

BadOsc
 
Posts: 2
Joined: Tue Dec 04, 2018 4:19 am

Re: Leading zeros problem

by adafruit_support_bill on Tue Dec 04, 2018 1:28 pm

It is not clear which library you are using for i2c communication. Using the "write" function from the "Wire" library, you can specify the number of bytes to write:
https://www.arduino.cc/en/Reference/WireWrite

Another method is to write the two bytes explicitly - as in this example from our MCP9808 library:
Code: Select all | TOGGLE FULL SIZE
void Adafruit_MCP9808::write16(uint8_t reg, uint16_t value) {
    Wire.beginTransmission(_i2caddr);
    Wire.write((uint8_t)reg);
    Wire.write(value >> 8);
    Wire.write(value & 0xFF);
    Wire.endTransmission();
}

adafruit_support_bill
 
Posts: 74337
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.