CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

send an entire structure thru i2c

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ed Arnold



Joined: 02 Nov 2003
Posts: 18

View user's profile Send private message Visit poster's website

send an entire structure thru i2c
PostPosted: Sun Feb 29, 2004 9:54 pm     Reply with quote

I first searched for this subject on this forum, but got a bunch of similar related i2c or stucture topics. But still not the answers I an looking for. Well here goes. The following code should explain all, but what I want to do is send an entire structure to a slave PIC to print out a receipt. Can anyone help to fill in the blanks.

Code:


static struct
{
  int UID;
  int time[3];
  int QTY_A;
  int QTY_B;
  int KEY;
  int PLU;
}TRANS_QUE[16];

print_receipt()
{
 int addr=0x40;
  i2c_start();
  i2c_write(addr);
  i2c_write(1);      // print command
  i2c_start();
  i2c_write(addr);
  for(i=0;i<9;i++)    // send entire structure
     {
       i2c_write(TRANS_QUE[]);
     }
  i2c_stop();
  return;
}

Thanks

Ed Arnold
Ttelmah
Guest







Re: send an entire structure thru i2c
PostPosted: Mon Mar 01, 2004 8:18 am     Reply with quote

Ed Arnold wrote:
I first searched for this subject on this forum, but got a bunch of similar related i2c or stucture topics. But still not the answers I an looking for. Well here goes. The following code should explain all, but what I want to do is send an entire structure to a slave PIC to print out a receipt. Can anyone help to fill in the blanks.

Code:


static struct
{
  int UID;
  int time[3];
  int QTY_A;
  int QTY_B;
  int KEY;
  int PLU;
}TRANS_QUE[16];

print_receipt()
{
 int addr=0x40;
  i2c_start();
  i2c_write(addr);
  i2c_write(1);      // print command
  i2c_start();
  i2c_write(addr);
  for(i=0;i<9;i++)    // send entire structure
     {
       i2c_write(TRANS_QUE[]);
     }
  i2c_stop();
  return;
}

Thanks

Ed Arnold

Give your structure a name, say 'my_struct' (after the structure keyword, and before the definition). Then have something like:
Code:

void I2C_STRUCT(struct my_struct *val) {
    int ctr;
    int8 *ptr;
    ptr=(int8 *)val;
    for (ctr=0;ctr<sizeof(my_struct);ctr++) {
       i2c_write(*(val++));
    }
}


The function expects to receive a pointer to a structure of type 'my_struct', and then walks through this one int8 value at a time, sending each byte in turn.
You could do a similar thing, by declaring a union to contain the structure, and an array of bytes the same size, then again walk through this array and send these one at a time.

Best Wishes
SteveS



Joined: 27 Oct 2003
Posts: 126

View user's profile Send private message

PostPosted: Mon Mar 01, 2004 8:24 am     Reply with quote

First, to be clear, when I say a 'member' I mean a single structure out of the array of structures. When I say 'element' I mean one value out that structure (like UID):

A couple of things:

I2C_write takes an 8 bit int as an argument. You are sending it - well I'm not quite sure - you are almost sending it a member from an array of strucures, but you don't have an index number in the brackets..

I think you really are trying to send all the elements from a single member of the structure array, but you have 8 ints in each member of the structure array and your loop will go 9 times (0,1,2,3,4,5,6,7,8).

---

You need a a bit more code to get there. First you need to choose which of the 16 members of the structure of arrays you want to access. Then walk thru that structure member int by int, sending the 8 elements of that structure.

Try starting with a simple structure (not an array of structures) and figure out how to send the elements of that structure out. Once that is working, add in the complexity of an array of structures.

SteveS
Ed Arnold



Joined: 02 Nov 2003
Posts: 18

View user's profile Send private message Visit poster's website

send stucture thru i2c followup
PostPosted: Mon Mar 01, 2004 1:23 pm     Reply with quote

Thanks for all of the replies. First, Ttelmah, this stucture is part of pre-existing code which works fine. When I tried to use a name or tag and pointers with this stucture, I got a compiler error, so I removed them and now the old code works. Second, SteveS, I do wish to send only one member from an array of structures. I made a mistake with my for loop. It should be set up to send 8 bytes and as for the member index[], it will be some index variable which I haven't created yet. It looks like I may have to use pointers after all, but I would like to avoid it if possible.

Thanks for the input so far,

Ed Arnold
SteveS



Joined: 27 Oct 2003
Posts: 126

View user's profile Send private message

PostPosted: Mon Mar 01, 2004 2:42 pm     Reply with quote

I think it may be hard to not use pointers. Why don't you want to?

Ttelmah's code example points in the right direction. I took your code, modified it a bit (similiar to what Ttelmah suggests) and it works fine:

1. Modify your print_receipt to take an index into your array of structures: void print_receipt(int8 idx) - where idx is 0-15

2. Add a pointer to an int8 in the routine: int8 * sp;

3. Point to the first member of the indexed structure:
sp = &TRANS_QUE[idx].UID;

4. Use that pointer in your call to I2C_write: i2c_write(*sp++);
Each time you call it, sp points to the next value in that structure.

- Now, I used putc instead of I2C_write to test it, but it should work the same.

- SteveS
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group