Hi there,

First of all thanks for providing me so much support in this forum. I have come here with another question.

I have a project using MC9s12xd256 MCU. I have found that a instruction of this type;

corr_dc = kp_Dc*error;

takes around 0.7ms and if kp_dc is float and if i change kp_dc as int, the time of execution is 0.007ms, so there is a big difference. As my application is very time critical. Do you advice me to use fixed point implementation instead of floating point. Here is the all floating code in the main loop of my project.

float kp=0.2;float ki=0.0023;float kd=0.033;correction = kp*error + ki*iterm + kd*dterm;

float com_ratio[7]={23.44,23.44 ...};

for(int i=0;i<7;i++)

{

com_val = (int)(com_ratio*sen_val);

}

I think I can save a lot of time by implementing fixed point. Also regarding fixed point representation, is there is a library avaliable for mc9s12x or I will have to write it on my own...

To substitute multiply by float like 22.45, you can multiply by 2245, then divide by 100. But you need to care about possible integer overflows and switch to long integers when necessary.

Multiply by 2245 will overflow for signed multipliers below -14 and above +14. Unsigned x*2245 will overflow starting from unsigned short x > 29. Switching to long int will give you muchj more freedom but often at a cost of slower operations.

You may try to find biggest common divisors and extend short int usage range. For example 2245 and 100 can be both divided by 5. So multiply by 22.45 can be replaced by *449 and then /20. Overflow margins will rise to signed +-72 and unsigned 145.

Excerpts from your code are absolutely useless without all declarations of all used objects. Is error variable integer? Is sen_val float? Sometimes it is faster to operate with all float parameters than doing intermediate conversions from / to integers.