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

How to declare a bit from a variable....

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



Joined: 13 Apr 2011
Posts: 406

View user's profile Send private message

How to declare a bit from a variable....
PostPosted: Wed Oct 12, 2022 9:18 pm     Reply with quote

How to declare a bit from a variable that is inside of an Structure?

Example
Code:

struct Alarm_St1{
   unsigned int8 ratioOne;
   unsigned int8 ratioZero;
   int1 ratioOV;
   int1 NewPacketReady;
   int1 ReadBits;
   unsigned int8 TimerPacket;
   unsigned int8 BitCount;
   unsigned int32 LastPacket;
   unsigned int8 LastData;
   unsigned int8 LastCRC;
   unsigned int8 CRC_result;
   unsigned int8 CurrentPacket[6];
   unsigned int8 CurrentData;   
}Alarm;

#bit TamperX = DSC.CurrentData.0

unsigned int8 Dummy;
#bit DummyZero = Dummy.0



DummyZero is ok but TamperX returns the following error
A numeric expression must appear here

I also tried adding some parentheses or brackets with similar results.

I want to able to do something like

Code:
 
if(TamperX)
{
   Alarm();
   TamperX=FALSE;
}

_________________
Electric Blue
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Thu Oct 13, 2022 1:56 am     Reply with quote

Use a union and if you want a short name, a define. So:
Code:

union bits {
   unsigned int8 whole;
   int1 bits[8];
};

struct Alarm_St1{
   unsigned int8 ratioOne;
   unsigned int8 ratioZero;
   unsigned int8 TimerPacket;
   unsigned int8 BitCount;
   unsigned int32 LastPacket;
   unsigned int8 LastData;
   unsigned int8 LastCRC;
   unsigned int8 CRC_result;
   unsigned int8 CurrentPacket[6];
   union bits CurrentData; 
   //comment here. You will find the compiler tends to prefer
   //int1 declarations are done after other types.
   int1 ratioOV;
   int1 NewPacketReady;
   int1 ReadBits; 
}Alarm;

//You now have access to the bits in currentdata, as bits[0]..7
//give this a short name if required
#define TamperX  Alarm.CurrentData.bits[0]

unsigned int8 Dummy;
#bit DummyZero = Dummy.0


void main()
{
   //you can now test as a simple variable.
   if (TamperX)
   {
      //do what you want
   }
   while(TRUE)
   {


      //TODO: User Code
   }
}


Note that since the location is fixed, the compiler optimises this totally, so
the test compiles as:

Code:

....................    if (TamperX)
0001E:  BTFSS  15.0


Where 'Alarm' here is stored in 0x4..0x16.

Note that what was CurrentData, is now CurrentData.whole
E_Blue



Joined: 13 Apr 2011
Posts: 406

View user's profile Send private message

PostPosted: Thu Oct 13, 2022 11:03 am     Reply with quote

So, that way, is not needed to call bit_test function?

Code:

if(bit_test(TamperX))
{

}


Also to compare Alarm.CurrentData with another 8 bit variable or value I must use

Code:

if(Alarm.CurrentData.whole==A5)
{
    ....
}

_________________
Electric Blue
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Thu Oct 13, 2022 11:49 am     Reply with quote

You were trying to declare a bit, so not using a bit_test.
Yes on using .whole.
If you are prepared to use bit_test, then you can just leave the data
declared as a byte not a union, and use bit_test on this.
E_Blue



Joined: 13 Apr 2011
Posts: 406

View user's profile Send private message

PostPosted: Thu Oct 13, 2022 1:27 pm     Reply with quote

I got this error on CurrendData definition

Expecting a {

My compiler version is 5.091
_________________
Electric Blue
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Oct 13, 2022 1:47 pm     Reply with quote

Need to see the code.....
E_Blue



Joined: 13 Apr 2011
Posts: 406

View user's profile Send private message

PostPosted: Thu Oct 13, 2022 1:57 pm     Reply with quote

The code is the same of Ttelmah made.

The whole code has around 22K lines and I'm not allowed to show it.
_________________
Electric Blue
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Fri Oct 14, 2022 1:32 am     Reply with quote

The code I posted, with a suitable chip header, compiles as runs. It was
cut and pasted from my test.
Here you have posted CurrendData, not CurrentData. Are you sure you
haven't got some similar typo?.
E_Blue



Joined: 13 Apr 2011
Posts: 406

View user's profile Send private message

PostPosted: Fri Oct 14, 2022 10:36 am     Reply with quote

Yes, there's not any typo in the code.
I just decided to declare the variables outside the struct.

Code:
unsigned int8 CurrentData,LastData;

#bit Alarm_Tamper = CurrentData.0
#bit Alarm_Rest = CurrentData.1
#bit Alarm_BatErr = CurrentData.3
#bit Alarm_SenOpen = CurrentData.5
#bit Alarm_HearBit = CurrentData.6

#bit Alarm_OldTamper = LastData.0
#bit Alarm_OldRest = LastData.1
#bit Alarm_OldBatErr = LastData.3
#bit Alarm_OldSenOpen = LastData.5
#bit Alarm_OldHeartBit = LastData.6

_________________
Electric Blue
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