embedded - C, Is it possible to ASSIGN a macro to a structure variable? -


i'm new embedded programming. i've got pic microcontroller 2 header files: pic.h , timer_peripheral.h.

in pic.h timer configuration register has been defined as:

__extension__ typedef struct tagt1conbits { union {   struct {     unsigned :1;     unsigned tcs:1;     unsigned tsync:1;     unsigned :1;     unsigned tckps:2;     unsigned tgate:1;     unsigned :6;     unsigned tsidl:1;     unsigned :1;     unsigned ton:1;   };    struct {     unsigned :4;     unsigned tckps0:1;     unsigned tckps1:1;    }; }; } t1conbits; 

and in timer.h, macros have been defined as:

/* timer1 control register (t1con) bit defines */  #define t1_on               0xffff      /* timer1 on */ #define t1_off              0x7fff      /* timer1 off */ 

which correspond microcontroller data sheet. unsigned type 16 bits. tried assign t1_on struct variable t1conbits in way:

t1conbits=t1_on   // wrong. 

i know can initialize struct, want assignment in main function , don't want using struct members like:

t1conbits.tcs=1;  t1conbits.tsync=1; 

is there way that? if not, why think these macros in timer.h have been defined?

thanks insight

this doesn't work because you're trying assign number variable of unrelated type. fact number matches structure's underlying bit pattern compiler doesn't care whole lot about.

try this:

union bitsint {     struct tagt1conbits con;     int bits; };  t1conbits = ((union bitsint){ .bits = t1_on }).con; 

you put data of 1 type in, same data of unrelated type out.

(using union cast bit patterns valid of up-to-date c standards.)


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

IIS->Tomcat Redirect: multiple worker with default -