View previous topic :: View next topic |
Author |
Message |
CMatic
Joined: 11 Jan 2012 Posts: 69
|
Can't access bit field bits |
Posted: Tue Jan 19, 2016 11:06 am |
|
|
CCS PCM C Compiler, Version 5.026
I have defined a structure as follows:
Code: |
typedef struct {
int1 Units:1; //bit0 - Units LSB
int1 Sound:1; //bit 1- sound
int1 Alarm:1; //bit 2- Alarm
int1 Bcklgt:1; //bit 3- Bcklgt
int1 Autopwroff:1; //bit 4- Autopwroff
int1 BatLow:1; //bit 5- BatLow
int1 Reserved:1; //bit 6- Reserved
int1 Factory:1; //bit 7- Factory MSB
} user_t;
user_t config = 0x80; |
And when I try to use the variable config.Factory in another module it gives me error with Factory underlined as the error. I am using extern config in the other module. When I hover my cursor on config it shows all elements but Factory shows "Expecting union/structure". Please help and thanks in advance. |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
Re: Can't access bit field bits |
Posted: Tue Jan 19, 2016 11:25 am |
|
|
You have not defined a structure with a byte bit field. you have defined a structure with eight int1s (i.e. booleans) each of one bit wide, which they are anyway!
Even with a byte-wide bit field you would not be able to access all the bits, i.e. the byte this way. To do that requires a union:
Code: |
typedef union user_t {
int8 All_Bits;
struct {
int8 Units:1; //bit0 - Units LSB
int8 Sound:1; //bit 1- sound
int8 Alarm:1; //bit 2- Alarm
int8 Bcklgt:1; //bit 3- Bcklgt
int8 Autopwroff:1; //bit 4- Autopwroff
int8 BatLow:1; //bit 5- BatLow
int8 Reserved:1; //bit 6- Reserved
int8 Factory:1; //bit 7- Factory MSB
} Single_Bits;
} My_union;
...
My_union.All_Bits = 0x80;
My_union.Single_Bits.BatLow = FALSE;
...
if (My_union.Single_Bits.Factory)
{
// Do stuff
}
|
You need to check up on how to define bit fields and unions. Also, you do need to be careful of the bit order. CCS C provides a more certain, but non-portable way of accessing bits. Personally, I wouldn't use bit fields at all, just booleans.
PS: I hope I got that right, my bit field knowledge is not all it should be, and my unions can be a bit sketchy. |
|
|
CMatic
Joined: 11 Jan 2012 Posts: 69
|
|
Posted: Tue Jan 19, 2016 11:41 am |
|
|
RF_Developer Thanks very much for replying. Yes, I can see why you also don't want to use bit fields. Since I am trying to conserve EEPROM space, can you suggest what would be a better way to save users configurations? |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Tue Jan 19, 2016 12:22 pm |
|
|
#bit will do what you want
int8 config
#bit Units = config.0
#bit Factory = config.7 |
|
|
CMatic
Joined: 11 Jan 2012 Posts: 69
|
|
Posted: Tue Jan 19, 2016 1:22 pm |
|
|
gaugeguy wrote: | #bit will do what you want
int8 config
#bit Units = config.0
#bit Factory = config.7 |
Thanks for the comment, I am trying this out now and will post my results. |
|
|
CMatic
Joined: 11 Jan 2012 Posts: 69
|
|
Posted: Tue Jan 19, 2016 6:14 pm |
|
|
Guageguy I tried your #bit suggestion but it did not work.
I tried the config.Factory = 1; And the compiler did compile it correctly, however, when I tried to use this statement in another module, this statement does not work? Am I missing something here?
Thanks |
|
|
drolleman
Joined: 03 Feb 2011 Posts: 116
|
|
Posted: Tue Jan 19, 2016 9:51 pm |
|
|
you didn't do it properly. #bit works I use it in almost every project. |
|
|
CMatic
Joined: 11 Jan 2012 Posts: 69
|
|
Posted: Tue Jan 19, 2016 10:16 pm |
|
|
drolleman wrote: | you didn't do it properly. #bit works I use it in almost every project. |
I tried the following and the compiler shows error on config.0 when I hover over it, it says "expecting a declaration" ?
here is the code snip.
int8 config = 0x80;
#bit Units = config.0;
#bit Sound = config.1;
#bit Alarm = config.2;
#bit Bcklgt = config.3;
#bit Autopwroff = config.4;
#bit BatLow = config.5;
#bit Reserved = config.6;
#bit Factory = config.7; |
|
|
drolleman
Joined: 03 Feb 2011 Posts: 116
|
|
Posted: Tue Jan 19, 2016 11:43 pm |
|
|
put complete code up.
what you have here factory will be 1 all other bits will be 0
if config = 0x34 = 00110100b
then
#bit Units = 0
#bit Sound = 0;
#bit Alarm = 1;
#bit Bcklgt =1;
#bit Autopwroff = 0;
#bit BatLow = 1;
#bit Reserved = 0;
#bit Factory = 0; |
|
|
CMatic
Joined: 11 Jan 2012 Posts: 69
|
|
Posted: Wed Jan 20, 2016 12:23 am |
|
|
Don't mean any disrespect but the code is very large and in 7 modules.
The structure is as follows:
Code: |
struct {
int8 Units:1; //bit0 - Units LSB
int8 Sound:1; //bit 1- sound
int8 Alarm:1; //bit 2- Alarm
int8 Bcklgt:1; //bit 3- Bcklgt
int8 Autopwroff:1; //bit 4- Autopwroff
int8 BatLow:1; //bit 5- BatLow
int8 Reserved:1; //bit 6- Reserved
int8 Factory:1; //bit 7- Factory MSB
} config;
#locate config = 0x2000 //define where this variable will be located
#bit Units = config.0;
#bit Sound = config.1;
#bit Alarm = config.2;
#bit Bcklgt = config.3;
#bit Autopwroff = config.4;
#bit BatLow = config.5;
#bit Reserved = config.6;
#bit Factory = config.7; |
Using the above structure, I am able to set Factory bit = 1, as follows:
However, in my other module (where I declare:extern config), when I try to use config.Factory = 1; I get error saying "Expecting structure/union", and this is where I don't understand the compiler? Thanks for looking into it.
This is working in my main function. |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Wed Jan 20, 2016 7:24 am |
|
|
You should not use both the structure and #bit.
Delete the structure and access the bits as
Factory = TRUE; or Factory = 1;
config = 0x80; |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jan 20, 2016 7:51 am |
|
|
Quote: | Don't mean any disrespect but the code is very large and in 7 modules. |
You still have not told the people working on your problem what PIC you
are using, and if you are using Multiple Compilation Units or not. |
|
|
drolleman
Joined: 03 Feb 2011 Posts: 116
|
|
Posted: Wed Jan 20, 2016 2:22 pm |
|
|
your structure uses 8 bytes of data. if you use
int8 config;
this uses 1 byte of data or 8 bits isn't that what you want? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Wed Jan 20, 2016 2:32 pm |
|
|
drolleman wrote: | your structure uses 8 bytes of data. if you use
int8 config;
this uses 1 byte of data or 8 bits isn't that what you want? |
No. He is using bitfields. The :1 means use one bit of the int8, as if this is an integer. You can use sizes like :4 to give you nibble sized integers.
Used correctly, this is perfectly acceptable, but his declarations are problematic. The bitfield, it needs to be declared in all modules using it. |
|
|
CMatic
Joined: 11 Jan 2012 Posts: 69
|
|
Posted: Wed Jan 20, 2016 3:20 pm |
|
|
PCM programmer The pic is 16f1825 and there is no multiple compilations |
|
|
|