Go Down

Topic: I2c and big numbers (Read 4403 times) previous topic - next topic

dudevlad

Can i send over i2c a number bigger than 255 ? If i send from an arduino a number like 874 i don`t receive it exactly, i receive it in 2 digit format. Must i convert to chars the digits and send them?

nickgammon

You can send up to 16 bytes at once using the standard Wire library.

Quote
Must i convert to chars the digits and send them?


I'm not sure what you mean by that. Can you give example code?
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Grumpy_Mike

Quote
i receive it in 2 digit format.

How are you receiving it?
You are not splitting the number you send at the moment are you?
On the receive end you specify the number of bytes you want to receive.
There is an expectation at both ends how numbers will be transferred, the I2C is not a general purpose interface it is designed for transferring register values. Do you want it to transfer strings?

PaulS

You can send integer values using Wire.send() as integers.
Code: [Select]
int bigVal = 852;
Wire.send((byte *)&bigVal, sizeof(int));


On the receiving end, you need to receive the same number of bytes, and reassemble them in the proper order, using bit shifting.

The art of getting good answers lies in asking good questions.

dudevlad

I`ve succeeded by sending each digit at a time with x/100,x/10%10 and x%10.

nickgammon

Try doing that with negative or floating-point numbers. Paul's suggestion would be more reliable.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Photomankc

I2C is about sending bytes.  It does not care if they are values all to themselves or parts of larger values, it moves bytes over the wire to and from devices with certain addresses.  I am sending all kinds of stuff over one right now: longs, words, bytes, positive and negative.  When you go to send or read the data *YOU* have to impose the meaning and order back into them and both the sender and reciever must agree about what type the number actually is.

It goes something like this:
To send an unsigned long of $FFEECCDD, you send FF, then send EE, then send CC, then send DD.  The other side needs to recieve those and store them into the right sized variable and then shift the last value it got over and add in the next value.  After all the bytes are recieved it has $FFEECCDD and both sides have the number 4,293,840,093 as long as they both agree that it was an unsigned long.

Go Up