|
|
View previous topic :: View next topic |
Author |
Message |
FRED_SUN
Joined: 28 Jul 2008 Posts: 7
|
transforms between signed and unsigned data |
Posted: Fri Jan 30, 2009 5:02 pm |
|
|
Hi,
I need to do pid calculate in the interrupt, a signed int32 data, div by 60000, it take too long. I use make8() to take 2 hi bytes, then use make16 to make a 16bit data, but how can I handle it? My code is below:
Code: |
signed int32 i_sum_a = 0;
unsigned int8 i_term_0 = 0;
unsigned int8 i_term_1 = 0;
signed int16 i_term = 0;
i_term_0 = make8(i_sum_a,2);
i_term_1 = make8(i_sum_a,3);
i_term = make16(i_term_1,i_term_0);
|
Are there any problems? i_term_0 and i_term_1 should be sign or unsigned?
Thinks
Fred Sun |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: transforms between signed and unsigned data |
Posted: Fri Jan 30, 2009 5:28 pm |
|
|
FRED_SUN wrote: | Hi,
I need to do pid calculate in the interrupt, a signed int32 data, div by 60000, it take too long. I use make8() to take 2 hi bytes, then use make16 to make a 16bit data, but how can I handle it?... |
First of all, you probably don't need to divide by 60000. Every implementation is different, depending on the ranges of the inputs and outputs, so I can't give you specific advice. But in general, look for ways to combine several multiplications and divisions into one. For example, if you have a gain factor in your PID algorithm, and then you want to divide by 60000, then why not redefine your gain parameter to include this rescaling? Also unsigned arithmetic is a little faster. Perhaps you can examine the ranges of elements of the calculation and use offsets to make all the numbers inherently positive. That way you could use unsigned arithmetic. What you can't do is blindly copy the theoretical formula for PID feedback without regard to the representation of the terms involved. With some careful analysis, you may be able to get away with 16-bit math instead of 32-bit math. _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jan 30, 2009 6:13 pm |
|
|
If you just want to truncate the bottom 16 bits, you can do this:
Code: | i_term = i_sum_a >> 16; |
Here's what it compiles to:
Code: |
.... i_term = i_sum_a >> 16;
0036: MOVFF i_sum_a+3,i_term+1
003A: MOVFF i_sum_a+2,i_term |
|
|
|
Ttelmah Guest
|
|
Posted: Sat Jan 31, 2009 3:13 am |
|
|
Also remember what a union can do for you.
If you declare your values like:
union {
signed int32 fullval;
signed int16 halfval[2];
} val;
Then val.fullval is the 32bit signed number, and val.halfval[1] is the top 16bits of this number, still signed, and accessed directly, without moving anything in memory at all!....
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
|