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

Struct?

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







Struct?
PostPosted: Sun Jan 24, 2010 5:36 am     Reply with quote

Have question about Struct.

Lets say I've got:
Code:

struct {
int16 a;
int8 b;
int8 c;
int16 d;
};

I want to transfer it via SPI.

Is there any method to make it automatically ? (in 'for (;;) or while loop'?)

Or do I need to do it manual like
spi_write(struct.b); spi_write(struct.c) etc.
Torello



Joined: 29 Sep 2006
Posts: 120

View user's profile Send private message

PostPosted: Sun Jan 24, 2010 7:50 am     Reply with quote

Hi,

Below a solution. You need to setup the Spi-bus first. The function typecasts the struct as pointer to a byte array. Therefore you also need to pass along the size of this array. The sizeof takes care of this.

Be aware: int16 are put in memory as Lsbyte-Msbyte, so that is also the order that this function will put out int16.


Code:



// --- Typedef the struct
Typedef struct{   
struct {
int16 a;
int8 b;
int8 c;
int16 d;} IamStructType;

//--- Declare the struct variable
IamStructType  Iam;


//--- call the function somewhere in your program
spi_writeStruct(&Iam, sizeof(Iam));


// === function ====================================
void spi_WriteStruct(int8 *src, int8 szf) {
// usage: spi_WriteStruct( (int8*)&Struct, sizeof(Struct) )
   int8 wbyte;
   int8 idx;
   for(idx=1; idx < szf; idx++) {                         
      wbyte = src[idx];                                    //get the byte out the structure         
      spi_write(wbyte);                                    //spi write the byte

   }   
}
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