2

Some back ground information; I have to interface an EEprom which has I2C interface. I want to save an array of floats in the memory and read it back. I want to make it as fast as possible. Currently I have the following solution for it which works perfectly fine.

float a[5];
unsigned char* p = (unsigned char *)a;
for ( i = 2; i < 22; i++)
{
    data [ i ] = p [ i -2 ];
}                                                           

 twi_master_trans(
if ( twi_master_trans ( EEprom_address  , data  , 22 ,NULL , 0)  == false )
{
    put_falsh_string("TWI major error1 ");
}

I am pointing at each element to an alternative location in memory. Why not I just give the starting point of my main array. I thought that should be done like this:

twi_master_trans ( EEprom_address  , (unsigned char *)a, 22 ,NULL , 0)

I don't understand why it does not work. Could you explain why? or what am I not understanding?

this is the definition of twi_master_trans:

   bool twi_master_trans(
        unsigned char slave_addr,
        unsigned char *tx_data,
        unsigned char tx_count,
        unsigned char *rx_data,
        unsigned char rx_count)

I know that my question is not very clear but I can't explain it any better. but I give it a try.

The argument for a function is pointer to unsigned char, I have an array of floats which has to be sent there. A pointer to starting byte in the float array is supposed to be what we pass to the function.

Thank You, I am a noob, sorry if it is a dumb question.

5
  • First of all, the array a is probably 20 bytes (float is usually four bytes, times 5), so passing 22 as the array length would cause the function to read out of bounds. Secondly, note how the loop skips the first two bytes in the data array? There is probably some other data there. Commented Jun 29, 2014 at 8:54
  • Well, actually that is the address of memory location. But anyway you solved the problem. I am not sending my data to the right address. Commented Jun 29, 2014 at 8:57
  • How do I stop other people from wasting time on my question? Commented Jun 29, 2014 at 8:58
  • Should I remove the question? Commented Jun 29, 2014 at 8:59
  • 4
    If you're sure that your question won't benefit anybody else then yes that's what you should do. Commented Jun 29, 2014 at 9:01

2 Answers 2

1

In the first snippet, the float data starts at byte 2 of the char array. In the second snippet, it starts at byte 0.

It looks like the missing two bytes are the key to why one works and the other doesn't.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Actually that is the exact problem.
How do I make sure other people do not waste their time on this?
0

you can use sprintf() to convert your data in float to an string and then send it to that function with char argument as you mentioned. this may not be that fast but it would definitely solve the problem.

for faster solution you can study floating point precision i.e. the way float are stored and you can break float in 4 packets of 8 bits and then send it to that function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.