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

Convert 32byte to 8 byte

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



Joined: 31 Mar 2004
Posts: 23
Location: Switzerland

View user's profile Send private message

Convert 32byte to 8 byte
PostPosted: Wed Dec 14, 2005 8:02 am     Reply with quote

hi

I want to convert a variable with 32 bytes into 4 variables with 8 bytes, because I've to save the data into the eeprom.
I'm working with a Pic 18F252 and CCS.

Thank you

Pablo
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Dec 14, 2005 8:41 am     Reply with quote

I assume you mean to convert a 32 BIT variable to 4 variables of 8 BITS each. Check out the functions make8() and make32().
_________________
The search for better is endless. Instead simply find very good and get the job done.
Ttelmah
Guest







PostPosted: Wed Dec 14, 2005 8:45 am     Reply with quote

Code:

union joinbytes {
    int8 b[4];
    int32 word;
}


If you declare your variable, instead of as an int32, as a union like that shown above, so:
Code:

union joinbytes variable;


Then you can talk to the int32 version, as 'variable.word', and use this wherever the int32 would normally be used. When you want the bytes. variable.b[0].... variable.b[3] are the four bytes.

You can read the bytes out of this, and write them to the eeprom, and also go the other way when needed.

The same system can also be used to move data from fp values, or any other form.

Best Wishes
Ttelmah
Guest







PostPosted: Wed Dec 14, 2005 8:49 am     Reply with quote

As a general note, As SherpaDog points out, there are make8, and make32 functions, but the advantage of the union, is it involves no data movement at all. If you have the union declared, and write variable.b[0] to the eeprom, the compiler generates a single byte fetch, from the original variable, making this very efficient.
The union, will also wrk on other compilers, when the makex functions, are CCS specific.

Best Wishes
neurus



Joined: 31 Mar 2004
Posts: 23
Location: Switzerland

View user's profile Send private message

PostPosted: Wed Dec 14, 2005 8:49 am     Reply with quote

hi

thank very much. Just what I looked for!

Aloha
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Wed Dec 14, 2005 4:33 pm     Reply with quote

Ttelmah wrote:
As a general note, As SherpaDog points out, there are make8, and make32 functions, but the advantage of the union, is it involves no data movement at all. If you have the union declared, and write variable.b[0] to the eeprom, the compiler generates a single byte fetch, from the original variable, making this very efficient.
The union, will also wrk on other compilers, when the makex functions, are CCS specific.

Best Wishes


The way you wrote this gives me the impression that make8() would be less efficient because it involves "data movement" which is not true. The make8() function will resolve to single byte fetches. But I'd agree that a union is a better choice.
Ttelmah
Guest







PostPosted: Thu Dec 15, 2005 6:24 am     Reply with quote

The 'make8' function, approximates the action of the union. However the make32, involves moving the four bytes into four seperate variables, adding a second set of variables, and an extra data movement.

Best Wishes
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