0
\$\begingroup\$

I'm using Xc8 compiler and pic18f.Im communicating with a dspic30f ;coded in assembly and receiving a 4 byte variable via i2c.

Task: I have to receive the 4 byte packet store,modify it and send back to the dspic controller. Since I'm using xc8 compiler,I trust I can store the packet as float only which consumes 3 or 4 bytes of space.But the compiler converts the 3-4 bytes in to IEEE float format of which I have only limited understanding.

Eg:If dspic sends a 4 byte value 0x0001ffff and expects pic 18f to modify and send the value as 0x001f00ff. As the pic18 used xc8 compiler;I trust the only way is to store the 4 bytes as float.But I have no idea how to resend the modified float value in to the desired hexadecimal format?.

I would appreciate any valuable inputs or suggestions.Meanwhile I'll give an attempt to understand the float format in C compilers. Any alternatives that can possibly make this happen will be deeply appreciated.

Thank you for your time.

Regards

Rookie91

\$\endgroup\$
  • \$\begingroup\$ What do you exactly want to communicate between them? Where do floats enter the picture, why not just use unsigned int (or unsigned long int, if your compiler has 16-bit ints)? \$\endgroup\$ – Timo Jan 15 '16 at 14:04
  • \$\begingroup\$ Timo floats enter picture because I need to store the Incoming four bytes.This value will be incremented or decremented and the updates value must be sent back to the dspic.I thought of representing the four bytes as float but I trust it's a bad idea. \$\endgroup\$ – Rookie91 Jan 15 '16 at 16:14
3
\$\begingroup\$

Float is definitely not the way to do it. I'm not sure what maths you need to do on the received value, your example seems to imply just swapping two bytes around. If that is the case then just receive the value into a 4 byte array (unsigned char val_array[4]) and swap the bytes around. If you need to do complex maths on the value then storing it as a long would be better.

\$\endgroup\$
  • \$\begingroup\$ SteveG thank you very much.I need to store the incoming four bytes and update the value either increasing or decreasing it.The maximum and minimum value that I need to update till is in range of 4bytes.So if I need to update the receiving Value and send back it to the dspic;how should I do it?I trust I'm clear now on this.I don't need to just swap the bytes but I have to update them and resend those in the correct format. \$\endgroup\$ – Rookie91 Jan 15 '16 at 16:19
  • \$\begingroup\$ OK, I think what you're asking is how to send and receive a long integer via I2C. The first thing to realise is that I2C can only send a sequence of bytes, so the 'long' has to be broken down into 4 bytes when its sent. On receive the long has to be reconstructed from the 4 bytes received. Rather than explain how to do this here, can I point you at another forum which has a few examples of how to do it. Try this one reddit.com/r/arduino/comments/19529p/… \$\endgroup\$ – Steve G Jan 15 '16 at 16:45

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.