General Forums >> General Programming

Pages: 1 | 2 | 3 | >> (show all)
invexed



Reged: Nov 10 2003
Posts: 31
Best method to split variables
      #16319 - Tue Mar 15 2005 04:59 PM

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!!!


Post Extras: Print Post   Remind Me!   Notify Moderator  
Rahul

****

Reged: Nov 26 2004
Posts: 149
Re: Best method to split variables [Re: invexed]
      #16320 - Tue Mar 15 2005 05:12 PM

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.


Post Extras: Print Post   Remind Me!   Notify Moderator  
Ryan
HI-TECH team member
*****

Reged: Jan 06 2004
Posts: 503
Loc: Brisbane, Australia
Re: Best method to split variables [Re: Rahul]
      #16322 - Tue Mar 15 2005 05:45 PM

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;



Post Extras: Print Post   Remind Me!   Notify Moderator  
invexed



Reged: Nov 10 2003
Posts: 31
Re: Best method to split variables [Re: Rahul]
      #16323 - Tue Mar 15 2005 05:49 PM

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 !!!!




Post Extras: Print Post   Remind Me!   Notify Moderator  
jtemplesModerator
Guru
****

Reged: Oct 16 2003
Posts: 1607
Loc: Southern California
Re: Best method to split variables [Re: invexed]
      #16324 - Tue Mar 15 2005 05:53 PM

What compiler are you using? PICC/PICC-18 generate optimal code for those kinds of constructs -- they don't do any shifting.

Post Extras: Print Post   Remind Me!   Notify Moderator  
invexed



Reged: Nov 10 2003
Posts: 31
Re: Best method to split variables [Re: Ryan]
      #16325 - Tue Mar 15 2005 06:01 PM

Thanks Ryan

This is exactly what i was looking for !!!


Post Extras: Print Post   Remind Me!   Notify Moderator  
invexed



Reged: Nov 10 2003
Posts: 31
Re: Best method to split variables [Re: jtemples]
      #16326 - Tue Mar 15 2005 06:06 PM

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


Post Extras: Print Post   Remind Me!   Notify Moderator  
achims

***

Reged: Jul 11 2004
Posts: 17
Re: Best method to split variables [Re: invexed]
      #16420 - Mon Mar 21 2005 05:50 PM

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.


Post Extras: Print Post   Remind Me!   Notify Moderator  
hhpp



Reged: Dec 16 2004
Posts: 76
Re: linking problem [Re: invexed]
      #16458 - Wed Mar 23 2005 09:04 PM

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.


Post Extras: Print Post   Remind Me!   Notify Moderator  
Dan HenryModerator
Guru
****

Reged: Oct 16 2003
Posts: 3872
Loc: Colorado
Re: linking problem [Re: hhpp]
      #16466 - Sat Mar 26 2005 01:29 PM

Quote:

can't find 0x69 words for psect rbsso in segment bank0(erroor)



FAQ #29


Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1 | 2 | 3 | >> (show all)



Extra information
0 registered and 2 anonymous users are browsing this forum.

Moderator:  Dan Henry, jtemples, mikerj, Andrew L 

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is enabled
      UBBCode is enabled

Rating:
Topic views: 10506

Rate this topic

Jump to

Contact Us | Privacy statement HI-TECH Software

Powered by UBB.threads™ 6.5.5