I need to pass a 16-bit long address (for an external EEPROM) to a function (twi_writeTo
) in a buffer.
I am currently doing this
byte buffer[2];
buffer[0] = (byte)(eeaddress >> 8);
buffer[1] = (byte)(eeaddress & 0xFF);
where eeaddress
is an unsigned int
with the address.
I then do
int8_t ret = twi_writeTo(eeprom_i2c_addr, buffer, 2, 1);
Before I tried
int8_t ret = twi_writeTo(eeprom_i2c_addr, (uint8_t*)&eeaddress, 2, 1);
but that returns garbage. Probably the byte order is swapped. Is that the case? And if so, is there a better way than copying the two bytes to a buffer?
buffer[0]
tobuffer[1]
and vice versa, and check if that fixes your problem.(uint8_t*)&eeaddress
andbuffer
is mutually exclusive, so I don't understand your comment.buffer[]
bytes over and see if that creates the same garbage that(uint8_t*)&eeaddress
does. If so, then yes it is an endian problem.it returns garbage
? Please give some examples with hex numbers.