View previous topic :: View next topic |
Author |
Message |
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Alais naming method for bits?. |
Posted: Fri Feb 14, 2003 4:07 pm |
|
|
OK this works quite well
int8 PacketBuffer[24];
#define slave PacketBuffer[0]
Now how can I do this for bits?
I tried this and it does not work.
#bit CMD_Open = PacketBuffer[2].4
#bit CMD_Close = PacketBuffer[2].5
___________________________
This message was ported from CCS's old forum
Original Post ID: 11704 |
|
|
R.J.Hamlett Guest
|
Re: Alais naming method for bits?. |
Posted: Fri Feb 14, 2003 4:23 pm |
|
|
:=OK this works quite well
:=
:=int8 PacketBuffer[24];
:=#define slave PacketBuffer[0]
:=
:=Now how can I do this for bits?
:=I tried this and it does not work.
:=
:=#bit CMD_Open = PacketBuffer[2].4
:=#bit CMD_Close = PacketBuffer[2].5
Realistically, you can't.
The same comment applies (that the 'bit' definition requires a constant). Also, you cannot access a 'bit' in a existing value, using the . format either.
Since arrays of bits are not allowed, you cannot do this by declaring a union.
However you can have definitions like:
#define CMD_Open (PacketBuffer[2] & 0x10)
Then these can be used like:
if (CMD_Open) {
}
However writing ones for the other direction (writing), gets bulkier...
Note also, that you should allways enclose such definitions in brackets. The reason is that otherwise, when they are 'expanded' into the code, it is possible for unexpected results to occur, if the placement in the code is not quite how you originally visualised the definition being used. (this applies even to simple numeric definitions - though you will get away with this most of the time...).
In the case given, without the brackets, normal 'C' precedence will apply, and if you include the value into a line with some other arithmetic, the result may be very wrong...
If you want to ensure that the resulting value is a simple 'logic' return (rather than the 32 value that will result from the simple definition), you can instead use:
#define CMD_Open ((PacketBuffer[2] & 0x10)!=0)
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 11707 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: Alais naming method for bits?. |
Posted: Fri Feb 14, 2003 4:35 pm |
|
|
I thought of this method.
I don't know if there is an easier way to do it.
char array[10];
#byte array_offset_5 = array + 5
#bit my_bit = array_offset_5.3
___________________________
This message was ported from CCS's old forum
Original Post ID: 11709 |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: Alais naming method for bits?. |
Posted: Fri Feb 14, 2003 4:45 pm |
|
|
:=:=OK this works quite well
:=:=
:=:=int8 PacketBuffer[24];
:=:=#define slave PacketBuffer[0]
:=:=
:=:=Now how can I do this for bits?
:=:=I tried this and it does not work.
:=:=
:=:=#bit CMD_Open = PacketBuffer[2].4
:=:=#bit CMD_Close = PacketBuffer[2].5
:=Realistically, you can't.
:=The same comment applies (that the 'bit' definition requires a constant). Also, you cannot access a 'bit' in a existing value, using the . format either.
:=Since arrays of bits are not allowed, you cannot do this by declaring a union.
:=However you can have definitions like:
:=#define CMD_Open (PacketBuffer[2] & 0x10)
:=Then these can be used like:
:=
:=if (CMD_Open) {
:=
:=}
:=
:=However writing ones for the other direction (writing), gets bulkier...
:=Note also, that you should allways enclose such definitions in brackets. The reason is that otherwise, when they are 'expanded' into the code, it is possible for unexpected results to occur, if the placement in the code is not quite how you originally visualised the definition being used. (this applies even to simple numeric definitions - though you will get away with this most of the time...).
:=In the case given, without the brackets, normal 'C' precedence will apply, and if you include the value into a line with some other arithmetic, the result may be very wrong...
:=If you want to ensure that the resulting value is a simple 'logic' return (rather than the 32 value that will result from the simple definition), you can instead use:
:=#define CMD_Open ((PacketBuffer[2] & 0x10)!=0)
:=
:=Best Wishes
It may not be supported by 'C' but it should be. I want to define the location of bits using an indirect address.
I know that this works;
int8 Status_Byte0;
#locate Status_Byte0 = 0x100
#bit LSO = Status_Byte0.0
#bit LSC = Status_Byte0.1
#bit LSA = Status_Byte0.2
#bit LSB = Status_Byte0.3
#bit Opening = Status_Byte0.4
#bit Closing = Status_Byte0.5
#bit Open_Torque= Status_Byte0.6
#bit Close_Torque = Status_Byte0.7
I wanted to do something similar to avoid having to copy the bit in order to use it. I have an incomming packet that has bits in fixed positions and want to access them directly. The only difference is that I want to use an address that is relative to a variable that is dynamicly assigned at compile time. The compiler should support this in some way.
___________________________
This message was ported from CCS's old forum
Original Post ID: 11710 |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: Alais naming method for bits?. |
Posted: Fri Feb 14, 2003 4:47 pm |
|
|
:=I thought of this method.
:=I don't know if there is an easier way to do it.
:=
:=char array[10];
:=#byte array_offset_5 = array + 5
:=#bit my_bit = array_offset_5.3
:=
:=
Thanks.
A good definition can save a lot of code.
___________________________
This message was ported from CCS's old forum
Original Post ID: 11711 |
|
|
R.J.Hamlett Guest
|
Re: Alais naming method for bits?. |
Posted: Fri Feb 14, 2003 4:48 pm |
|
|
:=I thought of this method.
:=I don't know if there is an easier way to do it.
:=
:=char array[10];
:=#byte array_offset_5 = array + 5
:=#bit my_bit = array_offset_5.3
I had tried this earlier, and it didn't work (assuming that this was the normal 'C' shortcut of using the array name as it's address, it seemed a logical approach). It kept saying that values must be a constant.
Must have been typing something wrong. :-)
This is definately the best solution.
___________________________
This message was ported from CCS's old forum
Original Post ID: 11712 |
|
|
R.J.Hamlett Guest
|
Re: Alais naming method for bits?. |
Posted: Fri Feb 14, 2003 4:51 pm |
|
|
:=It may not be supported by 'C' but it should be. I want to define the location of bits using an indirect address.
:=
Yes. The 'restriction' I was talking about was the CCS one, where you are not allowed arrays of bits. This is a CCS limit, not a 'C' one. However the good news is that the simple definition (which I had tried earlier, and failed with), works. :-)
Best Wishes
:=I know that this works;
:=
:=int8 Status_Byte0;
:=#locate Status_Byte0 = 0x100
:=#bit LSO = Status_Byte0.0
:=#bit LSC = Status_Byte0.1
:=#bit LSA = Status_Byte0.2
:=#bit LSB = Status_Byte0.3
:=#bit Opening = Status_Byte0.4
:=#bit Closing = Status_Byte0.5
:=#bit Open_Torque= Status_Byte0.6
:=#bit Close_Torque = Status_Byte0.7
:=
:=I wanted to do something similar to avoid having to copy the bit in order to use it. I have an incomming packet that has bits in fixed positions and want to access them directly. The only difference is that I want to use an address that is relative to a variable that is dynamicly assigned at compile time. The compiler should support this in some way.
___________________________
This message was ported from CCS's old forum
Original Post ID: 11713 |
|
|
Hans Wedemeyer Guest
|
Re: Alais naming method for bits?. |
Posted: Fri Feb 14, 2003 11:15 pm |
|
|
If you only need 32 or less bits consider this
Using the built in functions
bit_test()
bit_set()
bit_clear()
code produced by the compiler is lean.
It is possible to access any bit programmatically, no constants required.
A few examples
int32 ThirtyTwoBits; // 32 bits
#define CMD_Open Bit_Test( ThirtyTwoBits, 4 ) // bit 4 = CMD_Open
void main()
{
int i;
// test all 32 bits
for ( i=0; i<32; i++)
{
if ( CMD_Open == 1 )
{
// do something
}
}
// alternate set and clear bits
for ( i=0; i< 32; i++)
{
if ( i \% 2 )
{
Bit_Set ( ThirtyTwoBits, i );
}
else
{
Bit_Clear ( ThirtyTwoBits, i );
}
}
}
If you need to load incoming values byte wise, then you can use a union of four byte and one int32, and still manipulate and test individual bits.
___________________________
This message was ported from CCS's old forum
Original Post ID: 11722 |
|
|
Trampas Guest
|
Re: Alais naming method for bits?. |
Posted: Sat Feb 15, 2003 9:23 am |
|
|
I don't know if CCS supports this but here is how you would create a structure in C with bit definitions
typedef struct my_data {
unsigned int bit0:1;
unsigned int bit1:1;
unsigned int bit2:1;
unsigned int bit3:1;
unsigned int bit4:1;
unsigned int bit5:1;
unsigned int bit6:1;
unsigned int bit7:1;
} BYTE_BITS;
void main()
{
char i;
i=0x03;
if(((BYTE_BITS)i).bit0)
{
//do something
}
}
You can also use a neat feature called a union, I would recommend getting the K&R book on C, it will be money well spent!
<a href="http://www.amazon.com/exec/obidos/ASIN/0131103628/qid=1045322579/sr=2-3/ref=sr_2_3/002-4195490-8741648" TARGET="_blank">http://www.amazon.com/exec/obidos/ASIN/0131103628/qid=1045322579/sr=2-3/ref=sr_2_3/002-4195490-8741648</a>
Trampas
___________________________
This message was ported from CCS's old forum
Original Post ID: 11726 |
|
|
Hans Wedemeyer Guest
|
Re: Alais naming method for bits?. |
Posted: Sat Feb 15, 2003 4:30 pm |
|
|
CCS supports bit fields.
I fail to see how your example (BTW bad syntax)is solving the problem of "Alais naming method for bits"
CCS has a better solution, see my earlier reply.
___________________________
This message was ported from CCS's old forum
Original Post ID: 11746 |
|
|
Trampas Guest
|
Re: Alais naming method for bits?. |
Posted: Sat Feb 15, 2003 5:50 pm |
|
|
Well I am not the smartest that is why I need a good compiler to help me with my syntax errors. Also I must have misunderstood the question.
Trampas
:=CCS supports bit fields.
:=
:=I fail to see how your example (BTW bad syntax)is solving the problem of "Alais naming method for bits"
:=
:=CCS has a better solution, see my earlier reply.
___________________________
This message was ported from CCS's old forum
Original Post ID: 11752 |
|
|
|