View previous topic :: View next topic |
Author |
Message |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
Struct as a Byte - Solved |
Posted: Tue Aug 11, 2020 5:54 pm |
|
|
Hi all,
In building my shinny new RTOS-ish widget, i obviously need to deal with access control and permisions/prioritys...for this i have a lot of int1 flags.
I figured it would be neater to pack these into a byte but by using a struct with 8 x int1 variables inside. That way i could check with one IF if the system is free... however this doesn't work, unless i assign the struct value to a string:
Code: | i=GSM_Access_Control;
fprintf(FTDI,"ACCESS_CONTROL: %X",i);
if(i==0x03) fprintf(FTDI,"MATCH");
//delay_ms(1000);
GSM_Access_Control.GPRS_Reset_In_Process++; |
I wrote the above snippet to test if the bitwise operations work.. they do.
but I would like to do without having to use the temporary "i" variable.
Code: | fprintf(FTDI,"ACCESS_CONTROL: %X",GSM_Access_Control); |
This print works just fine, with the right value being displayed.
Code: | if(GSM_Access_Control==0x03) |
When i try this it does not work.
Tried it as a pointer... and the same failed results.
a union comes to mind, but it feels redundant.
Mostly I'm just trying to avoid using Bit_Set() and the rest of this family of functions and just treat each bit as a variable.
Any suggestions would be appreciated. _________________ CCS PCM 5.078 & CCS PCH 5.093
Last edited by Gabriel on Tue Aug 11, 2020 6:14 pm; edited 1 time in total |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1907
|
|
Posted: Tue Aug 11, 2020 6:08 pm |
|
|
Cast it instead.
Code: | fprintf(FTDI,"ACCESS_CONTROL: %X", (unsigned int8)GSM_Access_Control); |
This should work. Same for evaluating GSM_Access_Control. Just cast it to an int8 and it should work. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Tue Aug 11, 2020 6:13 pm |
|
|
Hey newguy!
Thanks i totally forgot about that! doh!
not the sleek neatness i hoped for, but it works.
Funny how this works:
Code: | fprintf(FTDI,"ACCESS_CONTROL: %X",GSM_Access_Control); |
Yet for this, casting is needed:
Code: | if(((unsigned int)GSM_Access_Control)==0x03) |
I can live with that.
Thanks! _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Wed Aug 12, 2020 1:39 am |
|
|
Use bitfields, instead of the int1.
Code: |
struct {
int8 first:1;
int8 second:1;
int8 third:1;
int8 fourth:1;
int8 fifth:1;
int8 sixth:1;
int8 seventh:1;
int8 eightth:1;
} GSM_Access_Control;
void main()
{
GSM_Access_Control=0;
GSM_Access_Control.first=1;
GSM_Access_Control.second=1;
printf("ACCESS_CONTROL: %X",GSM_Access_Control);
while(TRUE)
{
}
}
|
Shows generating a structure, filling it with zero, then setting two bits
and printing the result. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
Same issue as before |
Posted: Sat Aug 15, 2020 9:05 am |
|
|
Hi All, i switched to bitfields as recommended.
I still have the same issue.
The following line does not compile:
Code: | if(GSM_Access_Control==0x03) fprintf(FTDI,"MATCH\r\n"); |
It does work after casting GSM_Access_Control as an unsigned int.
I guess this is gonna be one of those things that doesn't go away. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Sat Aug 15, 2020 10:37 am |
|
|
The automatic 'self casting' behaviour must be a feature of the later compilers.
The code I posted works on 5.094... |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Sat Aug 15, 2020 11:20 am |
|
|
Hi Ttelmah, thank you so much for your help.
I changed my strategy to a simpler method and ive achieved my goal.
I do have to say i learned something on this thread: bit fields as per your last post.
So although i did not use it today, its been added to the bag of tricks.
Thank you for that.
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
|