You are on page 1of 3

CCS :: View topic - Wrong filter Output http://ccsinfo.com/forum/viewtopic.php?t=29748&highlight=iir+f...

FAQ Forum Help Official CCS Support Search Register

Profile Log in to check your private messages Log in

Wrong filter Output

CCS Forum Index -> General CCS C Discussion

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 Posted: Sun Feb 04, 2007 3:33 pm

You can't mix fractions with integers, plain and simple.


Joined: 24 Jun 2004
Posts: 553
Location: Edmonton AB Here's what I would do:
Canada

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

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 Re: Wrong filter Output


Posted: Sun Feb 04, 2007 7:47 pm

Joined: 08 Sep 2003 Anonymous wrote:


Posts: 1104
Location: Houston 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

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.

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 Posted: Mon Feb 05, 2007 12:29 pm

Your right it's only a 10 bit ADC.


Joined: 08 Sep 2003
Posts: 1104 When the ADC is configured to return a 10 bit value.
Location: Houston
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: All Posts Oldest First Go

All times are GMT - 6 Hours


CCS Forum Index -> General CCS C Discussion

Page 1 of 1

Jump to: General CCS C Discussion Go

You can post new topics in this forum


You can 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

3 от 3 26.2.2007 г. 10:07

You might also like