|
|
View previous topic :: View next topic |
Author |
Message |
neurus
Joined: 31 Mar 2004 Posts: 23 Location: Switzerland
|
Convert 32byte to 8 byte |
Posted: Wed Dec 14, 2005 8:02 am |
|
|
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
|
|
Posted: Wed Dec 14, 2005 8:41 am |
|
|
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
|
|
Posted: Wed Dec 14, 2005 8:45 am |
|
|
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
|
|
Posted: Wed Dec 14, 2005 8:49 am |
|
|
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
|
|
Posted: Wed Dec 14, 2005 8:49 am |
|
|
hi
thank very much. Just what I looked for!
Aloha |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Dec 14, 2005 4:33 pm |
|
|
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
|
|
Posted: Thu Dec 15, 2005 6:24 am |
|
|
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 |
|
|
|
|
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
|