i2c proggramming through c

Discussion in 'C Programming' started by vinod d, Nov 23, 2006.

  1. vinod d

    vinod d Guest

    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.
     
    vinod d, Nov 23, 2006
    #1
    1. Advertisements

  2. vinod d

    jacob navia Guest

    vinod d a écrit :
    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.
     
    jacob navia, Nov 23, 2006
    #2
    1. Advertisements

  3. 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.
     
    Keith Thompson, Nov 23, 2006
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.