invexed
Reged: Nov 10 2003
Posts: 31
|
|
Hi all
I split a long variable in 4 char variables and transmit all this through RS232 port but i want to do this more efficiently
At this moment this is the best C code I have find to split a variable but assembler code generated seems to be very long
Total_tx_I=0x12345678; B3=(unsigned char) (Total_tx_I>>24); B2=(unsigned char) (Total_tx_I>>16); B1=(unsigned char) (Total_tx_I>>8); B0=(unsigned char) (Total_tx_I);
Really i dont need to shift data. If i know where are this registers i can read this parts directly
Doing this in assembler it saves much more space and clock cycles but is difficult to integrate a piece of assembler code with a full C program.
C compiler shoud have some way to read a long, integer, float or double variable and convert it in some separated char variables directly
Someone know something more about this...... ????
Thanks!!!
|
Rahul
  
Reged: Nov 26 2004
Posts: 149
|
|
Quote:
C compiler shoud have some way to read a long, integer, float or double variable and convert it in some separated char variables directly
The compiler can already do this:
Code:
long Total_tx_I=0x12345678;
unsigned char * p = (unsigned char *) &Total_tx;
B3 = *p++;
B2 = *p++;
B1 = *p++;
B0 = *p;
Or something like that. I may have reversed the Endian-ness. Or something.
|
Ryan
HI-TECH team member
   
Reged: Jan 06 2004
Posts: 503
Loc: Brisbane, Australia
|
|
I think Rahul's method fits the 'streaming' application nicely. If you were in need of accessing the bytes more often you could achieve a similar effect as follows:
Code:
union {
unsigned long total;
struct {
unsigned char b0;
unsigned char b1;
unsigned char b2;
unsigned char b3;
} bytes;
} tx;
|
invexed
Reged: Nov 10 2003
Posts: 31
|
|
I'm trying to find some method more "direct" but this is much better than i have try
Good idea !!!! i didnt have thought in pointers
Yuor code it uses only 5us..... my last code 50us !!!!
Thanks for your idea !!!!
|
jtemples
Guru
  
Reged: Oct 16 2003
Posts: 1607
Loc: Southern California
|
|
What compiler are you using? PICC/PICC-18 generate optimal code for those kinds of constructs -- they don't do any shifting.
|
invexed
Reged: Nov 10 2003
Posts: 31
|
|
Thanks Ryan
This is exactly what i was looking for !!!
|
invexed
Reged: Nov 10 2003
Posts: 31
|
|
I'm using PICC-18 compiler
This code using PICC compiler dont do shiftings and works ok .....
in this 2 compilers i use all optimizations
|
achims
 
Reged: Jul 11 2004
Posts: 17
|
|
Hi invexed,
Rahul and Ryan posted efficient ways to access Bytes in longs. I would like to add one more makro, as we use it commonly. This makro will work with same efficency, as all this is optimized by the compiler.
#define BACC(var, off) (*((unsigned char *) &(var) + (off))) // ByteACCess(variable, offset)
The makro casts pointer to 'var' to a pointer of unsigned char. 'off' is added to this pointer, and then the content (the unsigned char pointed to) is referenced.
you might use it like e.g.:
long l = 123456; unsigned char c; void putChar(unsigned char uc);
c = BACC(l,0); // read out first byte BACC(l,3) = 0; // set last byte to 0 putChar(BACC(l,0); send all 4 bytes of l putChar(BACC(l,1); putChar(BACC(l,2); putChar(BACC(l,3);
this Makro could be used for any structure and encapsulates sophisticated casts in one perhaps more readable makro.
|
hhpp
Reged: Dec 16 2004
Posts: 76
|
|
Hi, I finally end a program with 700 lines, I thouhgt itt was ok, but when I run it, It brings a lot of mistekes in linking. i.e. command line..... enter... .: can't find 0x69 words for psect rbsso in segment bank0(erroor) .obj:: 68: fixup overflow in expresion ............
that's the problem,
first I
bank1 char,a,c,b,g,....; bank2 char trama[92],dato[12]....; char gu,gh,.. ; tha's the way that i think to improive it , but i couldn't
plese help me, I need your help.
|
Dan Henry
Guru
  
Reged: Oct 16 2003
Posts: 3872
Loc: Colorado
|
|
Quote:
can't find 0x69 words for psect rbsso in segment bank0(erroor)
FAQ #29
|