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

16Bit to 8Bit

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







16Bit to 8Bit
PostPosted: Sun Jun 07, 2009 3:30 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Jun 07, 2009 4:28 am     Reply with quote

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. Wink
shalts



Joined: 20 Oct 2006
Posts: 17
Location: Ankara

View user's profile Send private message Send e-mail

PostPosted: Wed Jul 08, 2009 7:23 am     Reply with quote

Dear Bungee,

I thank you in advance at least:)

your post saved my time a lot ...
_________________
Murat Shalt Unal
Ttelmah
Guest







PostPosted: Wed Jul 08, 2009 8:16 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Wed Jul 08, 2009 8:16 am     Reply with quote

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
PostPosted: Wed Jul 08, 2009 12:37 pm     Reply with quote

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

View user's profile Send private message

Re: 16Bit to 8Bit
PostPosted: Wed Jul 08, 2009 1:09 pm     Reply with quote

Anonymous wrote:

Can't you just put it into a union
That is another way ... I just directly adressed the problem Wink
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