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 CCS Technical Support

Struct as a Byte - Solved

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



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

Struct as a Byte - Solved
PostPosted: Tue Aug 11, 2020 5:54 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 11, 2020 6:08 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 11, 2020 6:13 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 12, 2020 1:39 am     Reply with quote

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

View user's profile Send private message

Same issue as before
PostPosted: Sat Aug 15, 2020 9:05 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Aug 15, 2020 10:37 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Aug 15, 2020 11:20 am     Reply with quote

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