i2c proggramming through c

V

vinod d

i m using the i2c protocol for my data transfer from as u know it is
synchronous on we can send one byte at a time. as i am having the 13
different variables of different data type. i have made the structure .
i wanted store it through i2c protocol. so how can i do it in better
way.
as i wanted to access the single byte from my created structure. for
i2c transfer.
 
J

jacob navia

vinod d a écrit :
i m using the i2c protocol for my data transfer from as u know it is
synchronous on we can send one byte at a time. as i am having the 13
different variables of different data type. i have made the structure .
i wanted store it through i2c protocol. so how can i do it in better
way.
as i wanted to access the single byte from my created structure. for
i2c transfer.
Suppose your structure is
struct mystruct { int i1;double d1;char name[12];};
(or something similar)

1) Define a data set:
struct mystruct MyData;
2) Fill it in with your values
3) define a pointer to unsigned char
unsigned char *ptr;
2) Make it point to the start of the structure:
ptr = (unsigned char *)(&MyData);
3) Send it 1 byte at a time:

for (i=0; i<sizeof(MyData);i++)
SendByte(*p++);
4) At the other end something must do the
contrary:

1) define an empty data set
2) receive the data, filling in the data set.
 
K

Keith Thompson

jacob navia said:
vinod d a écrit :
i m using the i2c protocol for my data transfer from as u know it is
synchronous on we can send one byte at a time. as i am having the 13
different variables of different data type. i have made the structure .
i wanted store it through i2c protocol. so how can i do it in better
way.
as i wanted to access the single byte from my created structure. for
i2c transfer.
Suppose your structure is
struct mystruct { int i1;double d1;char name[12];};
(or something similar)

1) Define a data set:
struct mystruct MyData;
2) Fill it in with your values
3) define a pointer to unsigned char
unsigned char *ptr;
2) Make it point to the start of the structure:
ptr = (unsigned char *)(&MyData);
3) Send it 1 byte at a time:

for (i=0; i<sizeof(MyData);i++)
SendByte(*p++);
4) At the other end something must do the
contrary:

1) define an empty data set
2) receive the data, filling in the data set.

That will work only of both ends use exactly the same layout for
"struct mystruct". That means the size of the fundamental types, the
floating-point format, and any padding must be identical. Basically,
you need the same code, compiled with the same compiler, on the same
platform, on both ends to do this safely. You're likely to be able to
get away with minor variations, but eventually it will break at the
most inconvenient possible moment.

If you want to transfer data safely from one system to another, you
need a representation that both systems can use. Plain text is one
good possibility, but there are other ways to break data down into
bytes.

I have no idea what the "i2c protocol" is; whatever it is, it's not
part of the C language. You're likely to get better information in
some system-specific newsgroup.

And please don't use silly abbreviations like "u" and "i m". This
isn't text messaging; take the time to spell out words.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top