How this function is supposed to work? Or how can I achieve the following:
My GLOBAL_Q is 19
Correction is max 4095.
Gain = _IQsat((Gain + Correction), _IQ(4000.0), _IQ(0.0));
The thing is that I want to limit Gain to be between 0 - 4000. Gain + Correction can and will overflow and I don't want it to do it. The above line does the following: If the result overflows then the Gain will be 0 and then on nect cycle it continues normal adding. I also tried:
temp = IQtoIQ18(Gain) +_IQtoIQ18(Correction);
temp = _IQsat(temp, _IQtoIQ18(4000.0), _IQtoIQ18(0.0));
Gain = _IQ18toIQ(temp);
And this doesn't work at all.
EDIT: Ok, here is one solution but this is not so nice:
temp_calc = _IQtoIQ18(Gain) + _IQtoIQ18(Correction);
if(temp_calc < 0)
{
Gain = 0;
}
else if(temp_calc > _IQ18(4000.0))
{
Gain = _IQ(4000.0);
}
else
{
Gain = _IQ18toIQ(temp_calc);
}