| Author |
Message |
Thomson
Joined: 15 Oct 2004 Posts: 181 Helped: 3
|
23 Mar 2006 10:04 How to efficiently convert the 32-bit data into 8-bit data? |
|
|
|
|
Hello,
Currently, I encountered one confusing problem about converting the 32-bit data variable into 4 8-bit data variables. Although i've searched some C reference books about this conversion; however i've no idea whetehr it's good for current my embedded system development.
Some Information: the project i'm implementing is concerning the 8051 MCU which can access byte per cycle; however the internal data bus is AHB bus which is 32-bit. Therefore the atomic data handling unit is 32-bit including the data and address!
Are there good suggestios?
Thansk in advance!
Thomson
|
|
| Back to top |
|
 |
IanP
Joined: 05 Oct 2004 Posts: 6490 Helped: 1542 Location: West Coast
|
23 Mar 2006 10:28 Re: How to efficiently convert the 32-bit data into 8-bit da |
|
|
|
|
One option is to use 4-ports of 89C52 microcontroller, but this means that almost all pins are wired ..
Another option would be to use external "hardware" and connect 4 8-bit D-latches (74LS/HCT374, for example) to all 32 bits, clock and latch data in them and then read 4 8-bit ICs in a sequence ..
This should convert one 32-bit word to four 8-bit words ..
Regards,
IanP
Last edited by IanP on 23 Mar 2006 10:44; edited 1 time in total |
|
| Back to top |
|
 |
Thomson
Joined: 15 Oct 2004 Posts: 181 Helped: 3
|
23 Mar 2006 10:42 Re: How to efficiently convert the 32-bit data into 8-bit da |
|
|
|
|
| IanP wrote: |
One option would be to connect 4 8-bit D-latches (74LS/HCT374, for example) to all 32 bits, clock and latch data in them and then read 4 8-bit ICs in a sequence ..
This should convert one 32-bit word to four 8-bit words ..
Regards,
IanP |
Thanks, IanP!
However i'm sorry to describe the problem unclearly! In fact, all i'd like to solve is about software instead of the hardware implementation.
Of course, in the actual circuit implementation, the similar hardware has been included to realize the 32-bit addressessing operations.
|
|
| Back to top |
|
 |
mimomod
Joined: 25 Jan 2006 Posts: 109 Helped: 15
|
23 Mar 2006 11:05 How to efficiently convert the 32-bit data into 8-bit data? |
|
|
|
|
Hi Thomson,
if you won't change the data externally, then it means you just need to transfer the 32-bit data via 8-bit bus. This could be done by masking 4 8-bit data (each group of 8-bit data is masked accordingly) and then shift the data to match 8-bit bus avalailable.
But, if you are also going to process/change the data externally, then the problem gets much more complicated since it means you have to design your system to deal with arithmetic and logic operations of 4 8-bit data like 32-bit data.
best
|
|
| Back to top |
|
 |
swapgo
Joined: 24 Jun 2004 Posts: 127 Helped: 2
|
23 Mar 2006 11:33 Re: How to efficiently convert the 32-bit data into 8-bit da |
|
|
|
|
Hi
The simple solution is as follows.
int32 var32bit;
int8 var8bit1;
int8 var8bit2;
int8 var8bit3;
int8 var8bit4;
var8bit1 = var32bit; // Lower Order byte
var8bit2 = var32bit >> 8;
var8bit3 = var32bit >> 16;
var8bit4 = var32bit >> 24; // Higher Order Byte
-------------------------------------------------------------------
Another approch
int32 var32bit;
int8 var8bit[4];
int8 i;
for(i=0;i<4;i++)
{
var8bit[i] = var32bit<<(i* ;
}
// var8bit [0] will contain lower order byte
// var8bit[3] will contain Higher order byte
Regards
Gopi
Added after 15 minutes:
| swapgo wrote: |
Hi
for(i=0;i<4;i++)
{
var8bit[i] = var32bit<<(i* ;
}
Gopi |
The Clown is actually blocking my Code
for(i=0;i<4;i++)
{
var8bit[i] = var32bit<<( i * 8 ) ;
}
Regards
Gopi
|
|
| Back to top |
|
 |
silvio
Joined: 31 Dec 2001 Posts: 801 Helped: 90
|
23 Mar 2006 12:32 Re: How to efficiently convert the 32-bit data into 8-bit da |
|
|
|
|
swapgo,
a stupid question :
Why will always assuming that compiler will assign the least significant byte of int32 variable (bits 7:0) to an int8.
Why not the most significant byte, mean bits 31:24
This has something to do or are somehow related to storing data in little or big endian format ?
Next, you wrote : "var8bit2 = var32bit >> 8;"
then a little bit bellow : "var8bit[i]=var32bit<<( i * 8 )" , assuming i=1
Hope it's not the annoying clown who change the shift direction.
|
|
| Back to top |
|
 |
Google AdSense

|
23 Mar 2006 12:32 Ads |
|
|
|
|
|
|
| Back to top |
|
 |
Thomson
Joined: 15 Oct 2004 Posts: 181 Helped: 3
|
24 Mar 2006 4:08 Re: How to efficiently convert the 32-bit data into 8-bit da |
|
|
|
|
| silvio wrote: |
swapgo,
a stupid question :
Why will always assuming that compiler will assign the least significant byte of int32 variable (bits 7:0) to an int8.
Why not the most significant byte, mean bits 31:24
This has something to do or are somehow related to storing data in little or big endian format ?
Next, you wrote : "var8bit2 = var32bit >> 8;"
then a little bit bellow : "var8bit[i]=var32bit<<( i * 8 )" , assuming i=1
Hope it's not the annoying clown who change the shift direction. |
Yes, This is a concern for the C programming!
But it seems no other good approaches to realize this function! Woe!!
In addition, i found that this implementation will consume pretty assembly Codes and cycles as well when using 32-bit varaibles as parameters passing ( my current MCU is 8-bit), which is un-accepted for my current systematic application!
One way I'm thinking that can alleviate such conditions is that using 8-bit parameters to replace one 32-bit parameter, although this will some extra efforts.
Any suggestsion are welcome!
Thanks swapgo!
Thomson
|
|
| Back to top |
|
 |
yego
Joined: 28 Oct 2004 Posts: 154 Helped: 14 Location: Middle of Nowhere
|
24 Mar 2006 10:47 Re: How to efficiently convert the 32-bit data into 8-bit da |
|
|
|
|
Thomson,
When I need to use data in several different ways I usually use union of the structures of required types. Once you have this union you may see your data in any format defined in your structures.
In your case one structure would be for the 32-bit variable, and the other for four 8-bit variables.
It doesn't consume much code.
regards, yego
|
|
| Back to top |
|
 |
LBdgWgt
Joined: 06 Mar 2006 Posts: 83 Helped: 5
|
24 Mar 2006 15:06 Re: How to efficiently convert the 32-bit data into 8-bit da |
|
|
|
|
I think one can also use union data type to solve the problem, isn't right?
perhaps it may look like this:
typedef union {
UINT32 long_var;
UINT8 char_var[4];
} MyUnion;
then you don't have to convert actually, just access the right byte to get the desired byte you want.
best regards,
|
|
| Back to top |
|
 |
yego
Joined: 28 Oct 2004 Posts: 154 Helped: 14 Location: Middle of Nowhere
|
24 Mar 2006 15:56 Re: How to efficiently convert the 32-bit data into 8-bit da |
|
|
|
|
That's exactly the purpose of introducing Unions into the C language
Regards, yego
|
|
| Back to top |
|
 |
Thomson
Joined: 15 Oct 2004 Posts: 181 Helped: 3
|
25 Mar 2006 9:45 Re: How to efficiently convert the 32-bit data into 8-bit da |
|
|
|
|
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef bit BOOL;
typedef union {
DWORD long_var;
BYTE char_var[4];
} MyUnion;
unsigned char ahb_sg_wr(MyUnion ahb_addr, MyUnion ahb_data)
{
unsigned char op_ready;
op_ready = 0;
//op_start_address_reg = ahb_addr;
//op_start_address_reg0 = (BYTE) (0x000000ff & ahb_addr);
//op_start_address_reg1 = (BYTE) ((0x0000ff00 & ahb_addr)>> ;
//op_start_address_reg2 = (BYTE) ((0x00ff0000 & ahb_addr)>>16);
//op_start_address_reg3 = (BYTE) ((0xff000000 & ahb_addr)>>24);
op_start_address_reg0 = ahb_addr.char_var[3];
op_start_address_reg1 = ahb_addr.char_var[2];
op_start_address_reg2 = ahb_addr.char_var[1];
op_start_address_reg3 = ahb_addr.char_var[0];
//op_data_count_reg = 4; //4 bytes are read
//op_type_reg = 2;
//op_data_fifo_reg2 = ahb_data[0]; //just append
//op_data_fifo_reg2 = ahb_data[1]; //just append
//op_data_fifo_reg2 = ahb_data[2]; //just append
//op_data_fifo_reg2 = ahb_data[3]; //just append
// op_data_fifo_reg2 = (BYTE)(0x000000ff & ahb_data) ;
//op_data_fifo_reg2 = (BYTE)((0x0000ff00 & ahb_data)>> ;
//op_data_fifo_reg2 = (BYTE)((0x00ff0000 & ahb_data)>>16) ;
//op_data_fifo_reg2 = (BYTE)((0xff000000 & ahb_data)>>24) ;
op_data_fifo_reg2 = ahb_data.char_var[3];
op_data_fifo_reg2 = ahb_data.char_var[2];
op_data_fifo_reg2 = ahb_data.char_var[1];
op_data_fifo_reg2 = ahb_data.char_var[0];
op_cs_reg = 6;
while (!op_ready)
op_ready = op_cs_reg & 1;
op_data_fifo_cs_reg2 = 4;
op_data_fifo_cs_reg2 = 0;
return TRUE;
}
ahb_addr.long_var = 0x01020304;
ahb_data.long_var = 0x10203040;
ahb_sg_wr(ahb_addr,ahb_data);
The following code snippets are portion of my application used for 8051 MCU!
Unfortunately, this approache also generates too big corresponding assembly codes and the low simulation speed (for the ahb_sg_wr task).
And this schema also relates the byte allocation with the actual C Compliler as well. In fact ,how does the complier alignes the corresponding byte with the specific location of the long word(32-bit here)?
However, this method does have advantage speed over the correspoding method mentioned by above shifting operations. This is a big improvement in fact!
Currently, i've utilized one method of passing 4-byte parameters isolatedly and directly instead of a 32-bit one just as below:
ahb_sg_wr(a1,a2,a3,a4,d1,d2,d3,d4); to reduce the convertions.
Virtually, this way will have better code size and speed for my Keil Compiler. Nevertheless, the speed is still an issue since task calling will cost quite a lot of cycles (or assembly codes as well).
Currently, i'm wondering whether a 32-bit MCU shall be used to replace the 8-bit MCU to alleviate the timing pressure, but if some better methods can be used to realize this objective, then it's much better since i realy don't like to replace this 8-bit MCU!
Here thanks for all of your help a lot!
Thomson
|
|
| Back to top |
|
 |
Thomson
Joined: 15 Oct 2004 Posts: 181 Helped: 3
|
27 Mar 2006 3:07 Re: How to efficiently convert the 32-bit data into 8-bit da |
|
|
|
|
Hi,
Thanks for all of your advices!
Now the speed has been improved to 73 MCU cycles, which can be endurable although not very good! The following is my approach and may be useful for others:
<1> reduce the unnessary parameters so that these parameters can be fitted into the registers!
<2> reduce the unnessary assignments in the tasks.
<3> make the return data type void if okay
<4> make the condition variable as a sbit instead of a char varaible as possible
<5> make the function more specifci instead of general, which, of course, will add some extra codes, but the speed can be improved!
Thomson
|
|
| Back to top |
|
 |
Thomson
Joined: 15 Oct 2004 Posts: 181 Helped: 3
|
28 Mar 2006 6:54 Re: How to efficiently convert the 32-bit data into 8-bit da |
|
|
|
|
The following method is used by my group now!
#define MSB(word) (BYTE)(((WORD)(word) >> & 0xff)
#define LSB(word) (BYTE)((WORD)(word) & 0xff)
#define SWAP_ENDIAN(word) ((BYTE*)&word)[0] ^= ((BYTE*)&word)[1];\
((BYTE*)&word)[1] ^= ((BYTE*)&word)[0];\
((BYTE*)&word)[0] ^= ((BYTE*)&word)[1]
|
|
| Back to top |
|
 |
swapgo
Joined: 24 Jun 2004 Posts: 127 Helped: 2
|
28 Mar 2006 13:00 Re: How to efficiently convert the 32-bit data into 8-bit da |
|
|
|
|
Hi silvio,
Yes Ur right we cannot assume that all the compilers follow the same conventions!!!
But What I gave here will work for PIC and Atmel 8051 Microcontrollers.
And the shift direction which I gave is wrong! I am sorry its a typo error!!!
Thanks for pointing this out!
Regards
Gopi
|
|
| Back to top |
|
 |