View previous topic :: View next topic |
Author |
Message |
Guest
|
Wrong filter Output |
Posted: Sun Feb 04, 2007 2:47 pm |
|
|
I had a question.
I am using a PIC 18F2520. It has a 10bit ADC.
I implemented the 10 bit ADC and save the value in a 16 bit variable.
Ex:
Code: |
int16 output;
int16 newOutput;
double filter = 0.05;
newOutput = Read_ADC();
|
I need to have the 16 bit output for all applications.
I am using an exponential filter.
Code: |
output += (filter)*(newOutput – output);
|
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!! |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1907
|
|
Posted: Sun Feb 04, 2007 3:33 pm |
|
|
You can't mix fractions with integers, plain and simple.
Here's what I would do:
Code: | int16 filter = 5;
int16 output = 0;
int16 newOutput;
newOutput = read_adc();
newOutput = newOutput * 100; // scale factor of 100 = 5/.05
output = 100 * output;
output = output + (filter * (newOutput - output));
output = output/100; |
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. |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: Wrong filter Output |
Posted: Sun Feb 04, 2007 7:47 pm |
|
|
Anonymous wrote: | I had a question.
I am using a PIC 18F2520. It has a 10bit ADC.
I implemented the 10 bit ADC and save the value in a 16 bit variable.
Ex:
Code: |
int16 output;
int16 newOutput;
double filter = 0.05;
newOutput = Read_ADC();
|
I need to have the 16 bit output for all applications.
I am using an exponential filter.
Code: |
output += (filter)*(newOutput – output);
|
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();
|
|
|
|
Guest
|
|
Posted: Sun Feb 04, 2007 8:32 pm |
|
|
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.
|
I have a PIC18F2520. It has a 10 bit ADC. Am I allowed to configure it to a 12 bit ADC! If so how?
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. |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Mon Feb 05, 2007 12:29 pm |
|
|
Your right it's only a 10 bit ADC.
When the ADC is configured to return a 10 bit value.
output -= output/16; // Output =Output * .9375
output += (Read_ADC()*4); // Output =Output + (input * .0625)
When the ADC is configured to return a 16 bit value.
output -= output/16; // Output =Output * .9375
output += (Read_ADC()/16); // Output =Output + (input * .0625)
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. |
|
|
|