View previous topic :: View next topic |
Author |
Message |
Marc Guest
|
struct |
Posted: Wed Sep 12, 2007 8:45 am |
|
|
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
|
|
Posted: Wed Sep 12, 2007 9:43 am |
|
|
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 |
Posted: Wed Sep 12, 2007 1:27 pm |
|
|
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
|
|
Posted: Wed Sep 12, 2007 1:50 pm |
|
|
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
|
|
Posted: Wed Sep 12, 2007 5:08 pm |
|
|
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 |
|
|
|