|
|
View previous topic :: View next topic |
Author |
Message |
hatanet
Joined: 08 Sep 2003 Posts: 9 Location: South Africa
|
split a 16-bit structure into 2x 8-bit message bytes |
Posted: Fri Aug 22, 2003 7:28 am |
|
|
Hi Guys,
How can I split a 16-bit structure like the one below into 2x 8-bit message bytes, in order to pass the to a procedure as shown.
struct ADDRESS_16B {
int DEPL : 5; // DEPL[4:0]
boolean izero; // unused_discriptor [5]=0
int STRU : 2; // STRU[7:6]
int BUF : 4; // DEPL[11:8]
int discriptor : 4; // discriptor[15:12]=0x00
} ADDR_16B;
void osd_16_send(unsigned char MSB_Message, unsigned char LSB_Message) { }
Dankie!
Hatanet
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517148 |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
Re: split a 16-bit structure into 2x 8-bit message bytes |
Posted: Fri Aug 22, 2003 9:21 am |
|
|
void osd_16_send(unsigned char *pMessage)
{
// *pMessage - contains LSB
pMessage++;
// *pMessage - contains MSB
// You can probably do this also
// pMessage[0] - contains LSB
// pMessage[1] - contains MSB
}
osd_16_send(&ADDR_16B);
:=Hi Guys,
:=
:=How can I split a 16-bit structure like the one below into 2x 8-bit message bytes, in order to pass the to a procedure as shown.
:=
:=struct ADDRESS_16B {
:= int DEPL : 5; // DEPL[4:0]
:= boolean izero; // unused_discriptor [5]=0
:= int STRU : 2; // STRU[7:6]
:= int BUF : 4; // DEPL[11:8]
:= int discriptor : 4; // discriptor[15:12]=0x00
:= } ADDR_16B;
:=
:=void osd_16_send(unsigned char MSB_Message, unsigned char LSB_Message) { }
:=
:=Dankie!
:=Hatanet
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517149 |
|
|
R.J.Hamlett Guest
|
Re: split a 16-bit structure into 2x 8-bit message bytes |
Posted: Fri Aug 22, 2003 3:28 pm |
|
|
:=Hi Guys,
:=
:=How can I split a 16-bit structure like the one below into 2x 8-bit message bytes, in order to pass the to a procedure as shown.
:=
:=struct ADDRESS_16B {
:= int DEPL : 5; // DEPL[4:0]
:= boolean izero; // unused_discriptor [5]=0
:= int STRU : 2; // STRU[7:6]
:= int BUF : 4; // DEPL[11:8]
:= int discriptor : 4; // discriptor[15:12]=0x00
:= } ADDR_16B;
:=
:=void osd_16_send(unsigned char MSB_Message, unsigned char LSB_Message) { }
:=
:=Dankie!
:=Hatanet
There are a lot of methods of doing this, with various degrees of 'elegance'. One method has allready been posted. Another solution, is to simply declare a union, with a two byte array, declared to occupy the same space (generally this results in _very_ compact code, making it my 'favourite' way of doing this type of transfer). Hence something like:
struct ADDRESS_16B {
int DEPL : 5; // DEPL[4:0]
boolean izero; // unused_discriptor [5]=0
int STRU : 2; // STRU[7:6]
int BUF : 4; // DEPL[11:8]
int discriptor : 4; // discriptor[15:12]=0x00
};
union {
struct ADDRESS_16B ADDR_16B;
int8 b[2];
} STOR;
Then instead of accessing 'ADDR_16B.discriptor', you would access 'STOR.ADDR_B.disriptor', while to access the bytes, you can then access STOR.b[0], and STOR.b[1].
You can allways use 'brute force casting', on the address of a variable, so (for instance), on the original variable (ADDR_16B), you can use:
*((char *)&ADDR_16B)
which should happily give the first byte, while:
*((char *)&ADDR_16B+1)
should give the second. I am (historically) a little careful of using pointer arithmetic, since it was an area the compiler had several problems with in the past. It does seem OK now though.
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517159 |
|
|
|
|
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
|