View previous topic :: View next topic |
Author |
Message |
kusek Guest
|
16Bit to 8Bit |
Posted: Sun Jun 07, 2009 3:30 am |
|
|
Hello All,
I have a requirement in which I need to convert a 16bit to 8 bit value (int16 to int8), transmit it and at the other end do the reverse.
Say for Example
Code: |
int16 value16bit;
int8 valuePart1;
int8 valuePart2;
valuePart1 = make8(value16bit,3);
valuePart2 = make8(value16bit,2);
value16bit=make16(valuePart1,ValuePart2)
|
I know it has to do with the Make8 and Make16 functions. But I am newbee and don't know what is the value that needs to sent for the offset.
The example from the manual doesn't seem to help me. It says
Code: |
int32 x;
int y;
y = make8(x,3); // Gets MSB of x
|
Thanks in Advance |
|
|
bungee-
Joined: 27 Jun 2007 Posts: 206
|
|
Posted: Sun Jun 07, 2009 4:28 am |
|
|
Well it is simple as this:
Sender....
Code: |
int16 a;
int8 b,c;
b=make8(a,0); //LSB
c=make8(a,1); //MSB
Transmit(b);
Transmit(c);
.....
|
Receiver....
Code: |
int16 a;
int8 b,c;
b=Receive();
c=Receive();
a=make16(c,b); //Put together MSB + LSB
.....
|
This code will not work if copied directly for obvious reasons (transmit/receive). But the make8/16 part is written in correct manner. |
|
|
shalts
Joined: 20 Oct 2006 Posts: 17 Location: Ankara
|
|
Posted: Wed Jul 08, 2009 7:23 am |
|
|
Dear Bungee,
I thank you in advance at least:)
your post saved my time a lot ... _________________ Murat Shalt Unal |
|
|
Ttelmah Guest
|
|
Posted: Wed Jul 08, 2009 8:16 am |
|
|
Just to provide some more data.
'MSB', stands for 'Most significant byte'. 'LSB', for 'Least significant byte'.
Bytes are ordered in RAM on the PIC, LSB.....MSB, numbered from '0', when addressing them.
So for the 32bit (4 byte) example in the original post, the 'MSB', is byte number 3.
For a 16 bit value (just two bytes), the LSB, is at address '0', and the MSB at address '1'. As in Bungee's example.
Best Wishes |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Wed Jul 08, 2009 8:16 am |
|
|
Just be aware that make8(), make16() and make32() functions are CCS specific which makes your code not compatible with other compilers. I would probably use a union which is a more standard C construct like what Ttelmah suggested in this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=30416&highlight=union |
|
|
Guest
|
Re: 16Bit to 8Bit |
Posted: Wed Jul 08, 2009 12:37 pm |
|
|
kusek wrote: | Hello All,
I have a requirement in which I need to convert a 16bit to 8 bit value (int16 to int8), transmit it and at the other end do the reverse.
Say for Example
Code: |
int16 value16bit;
int8 valuePart1;
int8 valuePart2;
valuePart1 = make8(value16bit,3);
valuePart2 = make8(value16bit,2);
value16bit=make16(valuePart1,ValuePart2)
|
I know it has to do with the Make8 and Make16 functions. But I am newbee and don't know what is the value that needs to sent for the offset.
The example from the manual doesn't seem to help me. It says
Code: |
int32 x;
int y;
y = make8(x,3); // Gets MSB of x
|
Thanks in Advance |
Can't you just put it into a union
Code: | union TXData{
unsigned char Ch[2];
int16 I;
}Data;
Data.I=value16bit;
valuePart1=Data.Ch[0];
valuePart2=Data.Ch[1]; //transmit data out, reverse at receiving end |
|
|
|
bungee-
Joined: 27 Jun 2007 Posts: 206
|
Re: 16Bit to 8Bit |
Posted: Wed Jul 08, 2009 1:09 pm |
|
|
Anonymous wrote: |
Can't you just put it into a union
| That is another way ... I just directly adressed the problem |
|
|
|