Torello
Joined: 29 Sep 2006 Posts: 120
|
|
Posted: Sun Jan 24, 2010 7:50 am |
|
|
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
}
} |
|
|