|
|
View previous topic :: View next topic |
Author |
Message |
James Guest
|
Need some help #locating some data in the wrong endian |
Posted: Fri Jun 02, 2006 1:27 pm |
|
|
I posted this in another thread but its offtopic now and I can't really figure it out. I did search, but found very little on endians. Also, I'm locating a struct to my data for a good reason, its a little odd but it does exactly what I want, so onto my problem:
Code: |
int data[8];
struct struct_spi {
int16 block1;
int16 block2;
int bitmapped;
};
struct struct_spi mySPIstruct
#locate mySPIstruct = data
|
Which works great! Except for one thing. The values in data[] come from the source in the wrong endian. So, say my
data = 0x11,0x22,0x33,0x44,0x55
What I want is:
mySPIstruct.block1 = 0x1122
mySPIstruct.block2 = 0x3344
mySPIstruct.bitmapped = 0x55
but ofcourse when I run this I get:
mySPIstruct.block1 = 0x2211
mySPIstruct.block2 = 0x4433
mySPIstruct.bitmapped =0x55
Is there a way to use locate and structs to solve this without swapping variables at runtime? Just adjusting the struct, not screwing up the order of values in data[] or any new structs that I locate in the same spot.
Thank You Everyone, |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jun 02, 2006 2:04 pm |
|
|
I don't see how it can be done, because int16 inherently stores data
in Lo-Hi format. It seems to me that you're looking for some pragma
that will automatically swap the bytes for you, by inserting appropriate
ASM code. I don't think it exists. I think that, in your routine that
loads the array, you need to add code to do the byte swapping right
after the array is loaded. |
|
|
Guest
|
|
Posted: Fri Jun 02, 2006 2:59 pm |
|
|
Hmm... Problem with that is since the two share memory locations I'll be changing data[] too.
On second thought I agree that I can't do what I wanted to. Unless there is someway to specify the location to each half of an int16.
The whole reason I wanted to get away from this is that I'm always pulling data in and sometimes I need them swaped, was trying to save from doing the very repetitive:
Code: |
temp = (int16)data[0] << 8;
temp += data[1];
|
And if I define a struct I can access my data by meaningful name. Maybe I'll just do both, swap and use the struct for fun. Oh well.
Maybe there is a better way to swap then what I was doing ? |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Jun 02, 2006 5:17 pm |
|
|
Code: | temp = (int16)data[0] << 8;
temp += data[1]; | On a PIC18 this takes 14 bytes, 7 clock cycles.
Shorter is Code: | temp = make16(data[0], data[1]); | 8 bytes, 4 clock cycles. |
|
|
|
|
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
|