CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Wrong filter Output

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








Wrong filter Output
PostPosted: Sun Feb 04, 2007 2:47 pm     Reply with quote

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: 1909

View user's profile Send private message

PostPosted: Sun Feb 04, 2007 3:33 pm     Reply with quote

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

View user's profile Send private message

Re: Wrong filter Output
PostPosted: Sun Feb 04, 2007 7:47 pm     Reply with quote

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








PostPosted: Sun Feb 04, 2007 8:32 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Feb 05, 2007 12:29 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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