Professional Documents
Culture Documents
Author Message
I had a question.
Ex:
Code:
int16 output;
int16 newOutput;
double filter = 0.05;
newOutput = Read_ADC();
It seems because I have my values read as int16 and then output saved as int16 it does not like
the filter being of type double. I endup getting absurd int16 output values.
Any suggestions of how I fix this, at the same time make sure that my output is of the type
int16.
Thank you!!
Code:
int16 filter = 5;
int16 output = 0;
int16 newOutput;
1 от 3 26.2.2007 г. 10:07
CCS :: View topic - Wrong filter Output http://ccsinfo.com/forum/viewtopic.php?t=29748&highlight=iir+f...
newOutput = read_adc();
newOutput = newOutput * 100; // scale factor of 100 = 5/.05
This is a classic example of using integers to do fractional math. You'll have to check your values
to make sure that they'll never overflow the int16 type, and may have to increase these
variables to int32 instead. Even with the added time to perform math operations on int32 over
int16, you'll find that it's still much MUCH faster than using floats, and less ROM intensive as
well.
To save even more time, try to devise a way to use factors that are only powers of 2 so that you
can do the multiplications and divisions using only left and right shifts.
Ex:
Code:
int16 output;
int16 newOutput;
double filter = 0.05;
newOutput = Read_ADC();
It seems because I have my values read as int16 and then output saved as int16 it
does not like the filter being of type double. I endup getting absurd int16 output
values.
Any suggestions of how I fix this, at the same time make sure that my output is of
the type int16.
Thank you!!
If you configure the ADC to read as a 12 bit value this will deliver a 16 bit result that is filtered
almost the same as what you posted but the values are all integers and the filter value is 1/16
or 0.0625. This also compiles very nicely. The only real drawback is the rounding error for ends
of scale readings but with a bit more code even that can be removed.
Code:
output -= output/16;
output += Read_ADC();
2 от 3 26.2.2007 г. 10:07
CCS :: View topic - Wrong filter Output http://ccsinfo.com/forum/viewtopic.php?t=29748&highlight=iir+f...
Quote:
If you configure the ADC to read as a 12 bit value this will deliver a 16 bit result that is
filtered almost the same as what you posted but the values are all integers and the filter
value is 1/16 or 0.0625.
Quote:
The only real drawback is the rounding error for ends of scale readings but with a bit more
code even that can be removed.
Code:
output -= output/16;
output += Read_ADC();
Does the above code take care of the drawback of the rounding error for ends of scale reading?
Shouldn’t the second line of the code be:
Code:
output += (Read_ADC()/16);
Thank You.
The first one will compile and run slightly faster and smaller.
This will still have the rounding error at min and max input values. By using an intermediate
value the rounding error can be removed.
Page 1 of 1
3 от 3 26.2.2007 г. 10:07