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 support@ccsinfo.com

pre assign value to bit in a struct

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



Joined: 06 Nov 2005
Posts: 29

View user's profile Send private message

pre assign value to bit in a struct
PostPosted: Tue Sep 07, 2010 6:03 pm     Reply with quote

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
Code:

int8 fred=0;

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
Code:

dbgrep=0x80;


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

View user's profile Send private message

PostPosted: Tue Sep 07, 2010 8:08 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Sep 07, 2010 9:01 pm     Reply with quote

haahh many thanks sir.
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