0
\$\begingroup\$

I'm trying to put a variable that is initially not on the I2C bus, to the I2C bus. However, my variable is of type 'float' whereas the RXData/TXData variables on the I2C bus are of type uint_8t. Do I need to convert my 'float' variable to 'uint_8t' in order to transfer this variable throughout my I2C bus and if so how can I do it? This is what I have tried so far, where writeByte is my 'float' variable and pointer is the register address I want to modify. I'm using MSP432P401R and TI's driverlib library.

/* Initiate start and send first character */
I2C_masterSendMultiByteStart(EUSCI_B0_BASE,
(unsigned char)( ((writeByte>>8) & 0x0F) | ((pointer << 4) & 0x30) ) );

/* Finish transcation */
I2C_masterSendMultiByteFinish(EUSCI_B0_BASE,
(unsigned char)(writeByte & 0xFF));

But I can't seem to get it working. I'd appreciate any help. Thank you in advance and have a great day.

\$\endgroup\$
  • \$\begingroup\$ You send bytes via I2C. Your float is most probably 4 bytes. Use sizeof() operator and provide it as length argument to the function that sends data. You may also need to cast the pointer. Do you have any higher level API? What you have shown are some low level functions. You don't want to operate on them as this is tedious. I mean some wrapper library for TI's driverlib. \$\endgroup\$ – Al Bundy Jul 26 '19 at 7:01
  • \$\begingroup\$ consider explcitly 'encoding' to bytes, consider aliasing to bytes in a struct, does the I2C library have float write functions? Choose the way that causes least surprise to you when you read it. \$\endgroup\$ – Neil_UK Jul 26 '19 at 7:35
  • \$\begingroup\$ Thanks for the response guys but I don't think there is a wrapper library for TI's driverlib. I'm aware that my 'float' variable is 4 bytes but the functions that allow for transmitting/receiving data on the I2C bus in i2c.h library all take variables in 1 byte form. Example function calls are like this: extern void I2C_masterSendMultiByteStart(uint32_t moduleInstance,uint8_t txData),I2C_masterSendMultiByteNext(uint32_t moduleInstance,uint8_t txData), I2C_masterSendMultiByteFinish(uint32_t moduleInstance,uint8_t txData) \$\endgroup\$ – Bobby dickson Jul 26 '19 at 7:47
  • 1
    \$\begingroup\$ Use uint8_t pointer, take the address of your float and increase/decrease pointer depending on the architecture (I do not know this one). Or use union of float and array of 4 uint8_t. \$\endgroup\$ – Al Bundy Jul 26 '19 at 10:52

Your Answer

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

Browse other questions tagged or ask your own question.