View previous topic :: View next topic |
Author |
Message |
mds
Joined: 06 Nov 2005 Posts: 29
|
pre assign value to bit in a struct |
Posted: Tue Sep 07, 2010 6:03 pm |
|
|
Hi all,
Can you pre assign a value to a bit inside a struct at compile time?
I often pre assign my vars with values when defining them
such as
Id like to do something similar with the struct byte.
Code: |
struct dbg_typ{
unsigned fspr :7; ///<Spare
unsigned f_rep_en :1; ///<Enable report printing
}dbgrep;
|
Id like to set f_rep_en to be true on start up.
Obviously at some startup config section in my code I could simply say
But I thought it would be neater, and less chance of being forgotten if I did it at the same point the struct is defined. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 07, 2010 8:08 pm |
|
|
The demo program below shows how to initialize a structure in C.
The output of the program is:
Quote: |
fspr = 2a, f_rep_en = 01
|
Code: |
#include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
struct dbg_typ{
unsigned fspr :7;
unsigned f_rep_en :1;
}dbgrep = {0x2A, 1};
//======================================
void main(void)
{
printf("fspr = %x, f_rep_en = %x \r", dbgrep.fspr, dbgrep.f_rep_en);
while(1);
}
|
|
|
|
mds
Joined: 06 Nov 2005 Posts: 29
|
|
Posted: Tue Sep 07, 2010 9:01 pm |
|
|
haahh many thanks sir. |
|
|
|