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

struct

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







struct
PostPosted: Wed Sep 12, 2007 8:45 am     Reply with quote

I have 4 flags:

int1 flag1
int1 flag2
int1 flag3
int1 flag4

I put them into a struct

struct {
int1 flag1
int1 flag2
int1 flag3
int1 flag4 } all_flags

It is possible to do:
all_flags=0110

instead of:

all_flags.flag2=1;
all_flags.flag3=1;

Thank you
Ttelmah
Guest







PostPosted: Wed Sep 12, 2007 9:43 am     Reply with quote

Put them in a union.
This is exactly what a union is for:
Code:

struct flags {
   int8 flag1:1;
   int8 flag2:1;
   int8 flag3:1;
   int8 flag4:1;
};

union {
   struct flags b;
   int8 whole;
} all_flags;

Note the use of a bit field defintion, rather than an int1. Ensures the fields are consecutive in one byte.
Then:

all_flags.b.flag1

Will access the bits, while:

all_flags.whole

Accesses the whole byte.
You can also in CCS, map a byte onto the same address, and access through this, but the above, is 'generic C', rather than a 'CCS special'.

Best Wishes
Marc
Guest







again
PostPosted: Wed Sep 12, 2007 1:27 pm     Reply with quote

I can't understand. My question is: is it possible to build a variable with four bits, that is to say not a byte variable but a nibble variable or a three bits variable?

Thank You
libor



Joined: 14 Dec 2004
Posts: 288
Location: Hungary

View user's profile Send private message

PostPosted: Wed Sep 12, 2007 1:50 pm     Reply with quote

Try this:
Code:
struct flags {
   int8 flag1:1;
   int8 flag2:1;
   int8 flag3:1;
   int8 flag4:1;
};

struct nibbles {
   int8 niblo:4;
   int8 nibhi:4;
};

union {
   struct flags b;
   struct nibbles n;
   int8 whole;
} all_flags;


all_flags.n.niblo acceses the 4 bit nibble. Caveat here: I think you cannot cross byte boundaries with a bitfield structure element.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Wed Sep 12, 2007 5:08 pm     Reply with quote

The 12F, 16F & 18F series PICs are eight bit devices. You cannot declare a variable to be a 4-bit variable. The declaration 'int1' will take one bit, from an 8-bit byte. 'int8' will use the whole byte. 'int16' will use two bytes to make one variable. If you only want to manipulate four bits then you can declare the variable as 'int8' and then only use four of the bits. Masking the variable with something like 0x0F will ensure that nothing is stored into the four higher bits and, when read, nothing is in the four higher bits of the variable you're working with.

Clear as mud?

Ronald
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