View previous topic :: View next topic |
Author |
Message |
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
How to declare a bit from a variable.... |
Posted: Wed Oct 12, 2022 9:18 pm |
|
|
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: 19520
|
|
Posted: Thu Oct 13, 2022 1:56 am |
|
|
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: 417
|
|
Posted: Thu Oct 13, 2022 11:03 am |
|
|
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: 19520
|
|
Posted: Thu Oct 13, 2022 11:49 am |
|
|
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: 417
|
|
Posted: Thu Oct 13, 2022 1:27 pm |
|
|
I got this error on CurrendData definition
Expecting a {
My compiler version is 5.091 _________________ Electric Blue |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Thu Oct 13, 2022 1:47 pm |
|
|
Need to see the code..... |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Thu Oct 13, 2022 1:57 pm |
|
|
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: 19520
|
|
Posted: Fri Oct 14, 2022 1:32 am |
|
|
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: 417
|
|
Posted: Fri Oct 14, 2022 10:36 am |
|
|
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 |
|
|
|