You are on page 1of 13

Software Filtering of an ADC input | 68HC12 | Embedded...

Página 1 de 13

Home Blogs! Forums Comp.Arch.Embedded TI MCU Center Books Links

Sign in
Discussion Groups | 68HC12 | Software Filtering of an ADC input
username:

password:
Join our technical discussions about Freescale
Microcontrollers: M68HC12. (Freescale Semiconductor is a
Ti Stellaris Mcu Subsidiary of Motorola).
Up to 80 MHz, 256 KB Flash, 96 KB
SRAM & USB, CAN, Ethernet &
Not a member? C/C++.
www.ti.com/stellaris

Search 68hc12

Embedded software design


Cost effective ISO 9001 software
developers available.
Search tips www.LHPSoftware.com/Solutions

Subscribe to 68hc12
Advertise Here
Your Email

Software Filtering of an ADC input - Andrew Leech - Mar 7 7:04:00 2004

I guess this is a bit of a general question, rather than a specific


68hc12 by Keywords HCS12 question, but I want to implement it on an HCS12.....

68HC1 | 812A4 | 9S12DP256 | I want to be able to 'filter' the value returned from an on-board ADC.
Bootloader | CodeWarrior |
D60A | Debugger | DP256 | ECT When I say 'filter' I think that perhaps 'average' would be a better
| EEPROM | EVB | Flash | HC1 | description.
HCS12 | I2C | IAR | ICC1 |
Interrupts | LCD |
M68KIT912DP256 | I want to be able to set an amount of filtering (say 20%) and none of
MC9S12DP256 | the descriptions of digital filters I've found seem to allow this.
MC9S12DP256B | Metrowerks | They all seem to be designed to filter a specific frequency range that
Motor | MSCAN | Multilink | PLL |
must be stated in the calculations used to design the filter.
Quadrature | SDI | SPI |
Transceiver | XFC
I thought about having a ring buffer to which you place the ADC
results at a constant time interval, then summing the buffer, and
Sponsor
dividing by the number of samples taken. But then how do you allow for
Get EASY design support the amount of filtering you want to apply ??
with
I'm going round in circles with this !!
TI's C2000™
controlSUITE™ software Any thoughts or ideas would be very much appreciated.
suite!
I also need the routine to be fairly quick and not take up too much
RAM as I'm getting a bit short on it.

Sponsor Andy

______________________________
Complete portfolio of 32-bit MCUs plus RFID, low-power RF and ZigBee® solutions. Order a Stellaris
Kit today!

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )

Ti Stellaris Up to 80 MHz, 256 KB Flash, 96 KB SRAM & USB, CAN, Ethernet & C/C++. www.ti.com/stellaris
Discover the benefits of
using the Cosmic HC08 / HCS08 Tools C Compiler, Asm, Sim, Mon08, BDM Debugger and Flash Programmer www.cosmic-software.com
world's lowest power
microcontroller! Spa, Massagem e Bem Estar Saia da correria. Relaxe com nossos descontos de até 70%. Confira! ClubeUrbano.GROUPON.com.br/Spa

Re: Software Filtering of an ADC input - Author Unknown - Mar 7 9:18:00 2004

Discussion Groups This works ok, but if you change NUMSAMP much bigger, you need to make tot
a long.
This keeps the rolling average of NUMSAMP samples but subtracting off the
Comp.Arch.Embedded old sample from the total, adding in the new sample in the total, and
recomputing the avg with one of those clever divide by shifting tricks. By keeping the
68HC12 total, you dont need to add em all up every time.
//---used in rolling avg--------
AT91SAM ARM #define NUMSAMP 8
typedef struct{
AVRclub unsigned char ndx;
int tot;
BasicX
int avg;
int dat[NUMSAMP]; //avg over NUMSAMP readings
FPGA-CPU
}Trollavg;

http://www.embeddedrelated.com/groups/68hc12/show/6089... 21/11/2010
Software Filtering of an ADC input | 68HC12 | Embedded... Página 2 de 13

//----global vars in bss at 0x0100-------


LPC2000
Trollavg rollavg[NUMICHAN];
LPC900 //--------------------
void initrollavg(Trollavg *p){
M68HC11 //init rollavg struct
char i;
MSP430 p->ndx=0;
p->tot=0;
Piclist p->avg=0;
for(i=0; i < NUMSAMP; i++){
Rabbit-Semi p->dat[i]=0;
}
TI ARM Processors MCUs
}
//--------------------
TI C2000
int calcrollavg(Trollavg *p, int n){
//return rollavg of last NUMSAMP readings
See Also p->tot-=p->dat[p->ndx]; //subtract old value from tot
p->tot+=n; //add in new value
p->dat[p->ndx++]=n; //remember new value, bump ndx
if(p->ndx==NUMSAMP) p->ndx=0; //rewind
p->avg=p->tot >> 3; //2^3=8=NUMSAMP; avg is tot/NUMSAMP
return p->avg;
} [Non-text portions of this message have been removed]

______________________________
Have a look at the new TI MCU Center on EmbeddedRelated.com!

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )

Re: Software Filtering of an ADC input - Andrew Leech - Mar 7 9:42:00 2004

I thought about doing it like this, but it would need to have a large
array to average the samples, and I can't afford the RAM space that
would use.

I'm sure there must be some clever way of doing this without using an
array - just can't see it yet.

PS : I'm writing it in Assemebler.

Andy

--- In , BobGardner@a... wrote:


> This works ok, but if you change NUMSAMP much bigger, you need to
make tot
> a long.
> This keeps the rolling average of NUMSAMP samples but subtracting
off the
> old sample from the total, adding in the new sample in the total, and
> recomputing the avg with one of those clever divide by shifting
tricks. By keeping the
> total, you dont need to add em all up every time.
> //---used in rolling avg--------
> #define NUMSAMP 8
> typedef struct{
> unsigned char ndx;
> int tot;
> int avg;
> int dat[NUMSAMP]; //avg over NUMSAMP readings
> }Trollavg;
> //----global vars in bss at 0x0100-------
> Trollavg rollavg[NUMICHAN];
> //--------------------
> void initrollavg(Trollavg *p){
> //init rollavg struct
> char i;
> p->ndx=0;
> p->tot=0;
> p->avg=0;
> for(i=0; i < NUMSAMP; i++){
> p->dat[i]=0;
> }
> }
> //--------------------
> int calcrollavg(Trollavg *p, int n){
> //return rollavg of last NUMSAMP readings
> p->tot-=p->dat[p->ndx]; //subtract old value from tot
> p->tot+=n; //add in new value
> p->dat[p->ndx++]=n; //remember new value, bump ndx
> if(p->ndx==NUMSAMP) p->ndx=0; //rewind
> p->avg=p->tot >> 3; //2^3=8=NUMSAMP; avg is tot/NUMSAMP
> return p->avg;
> } > [Non-text portions of this message have been removed]

http://www.embeddedrelated.com/groups/68hc12/show/6089... 21/11/2010
Software Filtering of an ADC input | 68HC12 | Embedded... Página 3 de 13

______________________________ Home Blogs Discussion Groups


Comp.Arch.Embedded
Complete portfolio of 32-bit MCUs plus RFID, low-power RF and ZigBee® solutions. Order a Stellaris Embedded Systems Books
Kit today! Internet Resources Contact

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )

Re: Re: Software Filtering of an ADC input - Robert Imhoff - Mar 7 10:03:00 2004

I suppose a sort of "sledgehammer" method would be to put an


appropriately chosen capacitor across the analog input (and a series
resistance towards the source of the signal) and let the hardware do
the filtering ... On Mar 7, 2004, at 3:42 PM, Andrew Leech wrote: > I thought about doing it
like this, but it would need to have a large
> array to average the samples, and I can't afford the RAM space that
> would use.
>
> I'm sure there must be some clever way of doing this without using an
> array - just can't see it yet.
>
> PS : I'm writing it in Assemebler.
>
> Andy
>
> --- In , BobGardner@a... wrote:
>> This works ok, but if you change NUMSAMP much bigger, you need to
> make tot
>> a long.
>> This keeps the rolling average of NUMSAMP samples but subtracting
> off the
>> old sample from the total, adding in the new sample in the total, and
>> recomputing the avg with one of those clever divide by shifting
> tricks. By keeping the
>> total, you dont need to add em all up every time.
>> //---used in rolling avg--------
>> #define NUMSAMP 8
>> typedef struct{
>> unsigned char ndx;
>> int tot;
>> int avg;
>> int dat[NUMSAMP]; //avg over NUMSAMP readings
>> }Trollavg;
>> //----global vars in bss at 0x0100-------
>> Trollavg rollavg[NUMICHAN];
>> //--------------------
>> void initrollavg(Trollavg *p){
>> //init rollavg struct
>> char i;
>> p->ndx=0;
>> p->tot=0;
>> p->avg=0;
>> for(i=0; i < NUMSAMP; i++){
>> p->dat[i]=0;
>> }
>> }
>> //--------------------
>> int calcrollavg(Trollavg *p, int n){
>> //return rollavg of last NUMSAMP readings
>> p->tot-=p->dat[p->ndx]; //subtract old value from tot
>> p->tot+=n; //add in new value
>> p->dat[p->ndx++]=n; //remember new value, bump ndx
>> if(p->ndx==NUMSAMP) p->ndx=0; //rewind
>> p->avg=p->tot >> 3; //2^3=8=NUMSAMP; avg is tot/NUMSAMP
>> return p->avg;
>> }

______________________________
Complete portfolio of 32-bit MCUs plus RFID, low-power RF and ZigBee® solutions. Order a Stellaris
Kit today!

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )

Re: Re: Software Filtering of an ADC input - Author Unknown - Mar 7 10:59:00 2004

In a message dated 3/7/04 9:42:57 AM Eastern Standard Time,


writes:

I thought about doing it like this, but it would need to have a large
array to average the samples, and I can't afford the RAM space that
would use.

I'm sure there must be some clever way of doing this without using an
array - just can't see it yet.

============================================
OK, there's another trick. When I use floating point, I get a good averaging

http://www.embeddedrelated.com/groups/68hc12/show/6089... 21/11/2010
Software Filtering of an ADC input | 68HC12 | Embedded... Página 4 de 13

when I 'add 1/64th of the difference' between the raw value and the smoothed
value... but this doesnt work for integers and real long averages because
you compute the difference then try and take 1/64th or 1/128th of it or
1/256th, it right shifts down to nothing. So the trick, which the folks in the
comp.dsp newsgroup told me, it to keep the avgeraged value scaled up by 256
sitting around, then when you compute the difference between the raw and avg
values, you dont need to take 1/256th of it, you just add it into the avg*256
total.thats already there. [Non-text portions of this message have been removed]

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )

Re: Re: Software Filtering of an ADC input - Author Unknown - Mar 7 11:04:00 2004

In a message dated 3/7/04 10:04:57 AM Eastern Standard Time, swar@compuserve.


com writes:

I suppose a sort of "sledgehammer" method would be to put an


appropriately chosen capacitor across the analog input (and a series
resistance towards the source of the signal) and let the hardware do
the filtering ... ================================
I think the big picture is that there is a hw and a sw solution to just
about every problem, and tayloring the mix in the solution to fit the context of
the problem is part of the engineering process.
Also I've noticed that the hw guys tend to push the hard stuff they dont
know how to do over to the sw guys, and they sw guys sort of do the same thing.
Every once in a while you'll see some rare bird that knows both hw and sw, but
they are few and far between..... [Non-text portions of this message have been removed]

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )

Re: Software Filtering of an ADC input - Andrew Leech - Mar 7 11:12:00 2004

An RC filter approach would work, but it would make it fairly tricky


to adjust from software. I could use a digital pot to alter the R
component, but the whole job becomes rather messy.

I'll just keep working on it. I'll figure out a good solution to it
i'm sure. I just thought that someone else might have had a better
idea than me ! --- In , BobGardner@a... wrote:
> In a message dated 3/7/04 10:04:57 AM Eastern Standard Time,
swar@compuserve.
> com writes:
>
> I suppose a sort of "sledgehammer" method would be to put an
> appropriately chosen capacitor across the analog input (and a series
> resistance towards the source of the signal) and let the hardware do
> the filtering ... > ================================
> I think the big picture is that there is a hw and a sw solution to
just
> about every problem, and tayloring the mix in the solution to fit
the context of
> the problem is part of the engineering process.
> Also I've noticed that the hw guys tend to push the hard stuff they
dont
> know how to do over to the sw guys, and they sw guys sort of do the
same thing.
> Every once in a while you'll see some rare bird that knows both hw
and sw, but
> they are few and far between..... > [Non-text portions of this message have been removed]

______________________________
Have a look at the new TI MCU Center on EmbeddedRelated.com!

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )

Re: Software Filtering of an ADC input - Jim Galloway - Mar 7 11:21:00 2004

This small function ,

void filter( uint a, uint *b, int nf )


{
*b += a - *b/nf ;
}

filters ATD result sample in a, into a corresponding filtered variable b.


This is equivalent to an RC time constant filtering with a time constant of
nf samples. The output in b, averages to nf * a.
I typically use a value of 8 for nf, so an ADT input ranging from 0 to 1023

http://www.embeddedrelated.com/groups/68hc12/show/6089... 21/11/2010
Software Filtering of an ADC input | 68HC12 | Embedded... Página 5 de 13

results in an output of 0 to 8184.


Obviously, if you try too large a value for nf, you will exceed the 16 bit
range.
This is easy to implement in asm, especially if you restrict to powers of 2
for nf so you can use a shift instead of a divide.

Jim At 07:04 AM 3/7/2004, you wrote:


>I guess this is a bit of a general question, rather than a specific
>HCS12 question, but I want to implement it on an HCS12.....
>
>I want to be able to 'filter' the value returned from an on-board ADC.
>When I say 'filter' I think that perhaps 'average' would be a better
>description.
>
>I want to be able to set an amount of filtering (say 20%) and none of
>the descriptions of digital filters I've found seem to allow this.
>They all seem to be designed to filter a specific frequency range that
>must be stated in the calculations used to design the filter.
>
>I thought about having a ring buffer to which you place the ADC
>results at a constant time interval, then summing the buffer, and
>dividing by the number of samples taken. But then how do you allow for
>the amount of filtering you want to apply ??
>
>I'm going round in circles with this !!
>
>Any thoughts or ideas would be very much appreciated.
>
>I also need the routine to be fairly quick and not take up too much
>RAM as I'm getting a bit short on it.
>
>Andy >
>--------------------------------------------------------To learn more
>about Motorola Microcontrollers, please visit
>http://www.motorola.com/mcu
>o learn more about Motorola Microcontrollers, please visit
>http://www.motorola.com/mcu
>
>Yahoo! Groups Links

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )

Re: Re: Software Filtering of an ADC input - Robert Imhoff - Mar 7 11:24:00 2004

You could use a sort of PID or PI method: take the average of, say, the
last 16 samples as the "i" value, the current value as "p", evtl. the
difference between the last and the present sample as the "D" amount
and combine the three with adjustable weighting to obtain your filtered
value. On Mar 7, 2004, at 5:12 PM, Andrew Leech wrote: > An RC filter approach would work, but
it would make it fairly tricky
> to adjust from software. I could use a digital pot to alter the R
> component, but the whole job becomes rather messy.
>
> I'll just keep working on it. I'll figure out a good solution to it
> i'm sure. I just thought that someone else might have had a better
> idea than me !

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )

Re: Software Filtering of an ADC input - Robert Smith - Mar 7 11:54:00 2004

Andrew --

I have used the following method with good results on several projects:

1. Allocate an accumulator in RAM to hold the running average.

2. For every A/D sample update the accumulator as follows:

Accum = (Prior Accum * 7/8) + 1/8 * New Sample.

Working in assembler this is almost trival to implement. Use shifts and


accumulates.

Shift 'Prior Accum' left 3 to obtain 1/8 of 'Prior Accum'

Subtract 1/8 Prior Accum from 'Prior Accum' to obtain 7/8 'Prior Accum'

Now compute 1/8 of 'New Sample' by shifting right 3 and add this to '7/8
Prior Accum' to
obtain the Updated accumulator and smoothed value.

http://www.embeddedrelated.com/groups/68hc12/show/6089... 21/11/2010
Software Filtering of an ADC input | 68HC12 | Embedded... Página 6 de 13

The smoothing factor of 1/8 was arbitrairly chosen but is representative of


factors that have worked well in the past.

You can adjust the "degree of filtering" by using different smoothing


factors such as 1/4 or 1/16.
This only requires that you adjust the shift counts.

Note: If you do this all in fixed point, you can expect to see binary
truncation effects in the result. In effect the output will develop a dead
zone such that new samples very near the running average will not affect the
filter output. The output will jump from one smoothed value to another as
the new samples change by a few counts. This creates a sort of dead zone
effect. This is an inevitble consequence of binary truncation and will show
up in any filtering technique that you devise.

It is much better to eliminate the noise at the source by careful attention


to hardware design and to use little or no software filtering.

Warning: Be very careful to consider the dynamic range of the intermediate


results of this calculation. Binary overfow at any step of this process
will produce absolute rubbish for output!!!

You can examine the results of this filtering process by clever use of
Excel.

Good Luck, Bob Smith --- Avoid computer viruses, Practice safe hex ---

-- Specializing in small, cost effective


embedded control systems --

http://www.smithmachineworks.com/embedprod.html Robert L. (Bob) Smith


Smith Machine Works, Inc.
9900 Lumlay Road
Richmond, VA 23236 804/745-1065
----- Original Message -----
From: "Andrew Leech" <>
To: <>
Sent: Sunday, March 07, 2004 7:04 AM
Subject: [68HC12] Software Filtering of an ADC input > I guess this is a bit of a general
question, rather than a specific
> HCS12 question, but I want to implement it on an HCS12.....
>
> I want to be able to 'filter' the value returned from an on-board ADC.
> When I say 'filter' I think that perhaps 'average' would be a better
> description.
>
> I want to be able to set an amount of filtering (say 20%) and none of
> the descriptions of digital filters I've found seem to allow this.
> They all seem to be designed to filter a specific frequency range that
> must be stated in the calculations used to design the filter.
>
> I thought about having a ring buffer to which you place the ADC
> results at a constant time interval, then summing the buffer, and
> dividing by the number of samples taken. But then how do you allow for
> the amount of filtering you want to apply ??
>
> I'm going round in circles with this !!
>
> Any thoughts or ideas would be very much appreciated.
>
> I also need the routine to be fairly quick and not take up too much
> RAM as I'm getting a bit short on it.
>
> Andy >
> --------------------------------------------------------To learn more
about Motorola Microcontrollers, please visit
> http://www.motorola.com/mcu
> o learn more about Motorola Microcontrollers, please visit
> http://www.motorola.com/mcu
>
> Yahoo! Groups Links

______________________________
Complete portfolio of 32-bit MCUs plus RFID, low-power RF and ZigBee® solutions. Order a Stellaris
Kit today!

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )

Re: Software Filtering of an ADC input - theobee00 - Mar 7 15:48:00 2004

--- In , "Andrew Leech" <andrew_leech@y...> wrote:

> I want to be able to 'filter' the value returned from an on-board ADC.
> When I say 'filter' I think that perhaps 'average' would be a better

http://www.embeddedrelated.com/groups/68hc12/show/6089... 21/11/2010
Software Filtering of an ADC input | 68HC12 | Embedded... Página 7 de 13

> description.

> I want to be able to set an amount of filtering (say 20%) and none of
> the descriptions of digital filters I've found seem to allow this.
> They all seem to be designed to filter a specific frequency range that
> must be stated in the calculations used to design the filter.

Hi Andrew,

I am not sure if I read you right but it seems you are trying to compensate=
for some sort of noise in the incoming signal.

I have found it is best to consider analogue signals in their entirety, the=


bane of the computer programmers

I.e. the origin of the signals is important because it determines the frequ=
ency range of interest.

Say a small motor with a varying load needs a faster response time then a w=
eigh-cell where accuracy is of more importance and the final measurement may=
take a second or so.

So it is best to start a project with the input considerations first, lower=


the required frequency response as far as possible first of.

Say for the small motor it could mean a flywheel or other means of mass inc=
rease, for a weigher it could mean a dashpot.

The next step is to minimise the frequency response from amplifiers and oth=
er electronics to what the measurement requires, and of course to what the c=
omputer can handle.

Since this is a relatively cheap step, don't spare the caps, it will preven=
t all sorts of aliassing problems in your digital filters if the signal vari=
ations (or mains noise) don't get that far or are minimised before they get =
to the converters.

I can't over stress that, you will find it takes an amazingly long time and=
an even more amazing sample rate to get some form of accuracy in a signal e=
mbedded in a sine-wave, to say nothing about reduced headroom in the signal =
paths etc.
Keep it out of a device that has to work in a sample/hold fashion and only =
has a limited A/D accuracy as far as possible.

The last step is the one you are interested in, the measurement and/or cont=
rol loop in the computer.

You will find there are as many schemes as there are Engineers, but in the =
end they all try to do the same thing, provide a digital low pass filter in =
some form or another, the cut of frequencies, poles etc determined by how ke=
en the designer is and how fast the computer.

There are some rather involved tomes that will give you all the good stuff,=
but it is very rare to have to go that far, simple solutions usually suffic=
e in most situations.

I.e. a few stages of RC low pass filtering limiting the signals presented t=
o the lowest frequency needed, followed by something similar in the computer=
is usually enough to serve the purpose.

The trade of in computer filters is of course easily seen, the more involve=
d the filter, the slower your sample rate (or the faster the computer has to=
be), often negating the gains by introducing aliassing problems.
I.e. sampling at a rate that coincides with some harmonic or other of the i=
ncoming signal will cause all sorts of grief.

If you have made sure to keep higher frequencies out of the signal path in =
the first place, a simple exponential scheme along the lines of adding the n=
ew value to a multiple of the previous value and weighing it back usually su=
ffices.

If you find you have to lower the cut of frequency beyond the time availabl=
e, re-evaluate the input filtering rather then trying to compensate in the C=
PU with all sorts of interesting algorithms.

Hope this helps,

Theo

______________________________
Have a look at the new TI MCU Center on EmbeddedRelated.com!

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )

http://www.embeddedrelated.com/groups/68hc12/show/6089... 21/11/2010
Software Filtering of an ADC input | 68HC12 | Embedded... Página 8 de 13

Re: Re: Software Filtering of an ADC input - linktek - Mar 7 16:58:00 2004

Hi,

You can always use IIR or a more computative FIR software filter with
taylored coefficients.

However, on the KISS solution. Lets say the problem is lack of data
memory. You would like a
adjustable software filter - field tunable. So how about a cascaded running
average filter. In stead of one long array, you first average
the first 8 samples into a running average, and stuff the result of the
first average into a second array doing a running average of the results of
the first running average output in another 8 sample array and so on and so
on .... till you run out of memory. Its obvious to see that this is a easely
tunable filter algorithm.

Now stuff that into your excel spreadsheet !!!


I adjustable software filter can take the for of I
----- Original Message -----
From: "theobee00" <>
To: <>
Sent: Sunday, March 07, 2004 12:48 PM
Subject: [68HC12] Re: Software Filtering of an ADC input --- In , "Andrew Leech"
<andrew_leech@y...> wrote:

> I want to be able to 'filter' the value returned from an on-board ADC.
> When I say 'filter' I think that perhaps 'average' would be a better
> description.

> I want to be able to set an amount of filtering (say 20%) and none of
> the descriptions of digital filters I've found seem to allow this.
> They all seem to be designed to filter a specific frequency range that
> must be stated in the calculations used to design the filter.

Hi Andrew,

I am not sure if I read you right but it seems you are trying to compensate=
for some sort of noise in the incoming signal.

I have found it is best to consider analogue signals in their entirety, the=


bane of the computer programmers.

I.e. the origin of the signals is important because it determines the frequ=
ency range of interest.

Say a small motor with a varying load needs a faster response time then a w=
eigh-cell where accuracy is of more importance and the final measurement
may=
take a second or so.

So it is best to start a project with the input considerations first, lower=


the required frequency response as far as possible first of.

Say for the small motor it could mean a flywheel or other means of mass inc=
rease, for a weigher it could mean a dashpot.

The next step is to minimise the frequency response from amplifiers and oth=
er electronics to what the measurement requires, and of course to what the
c=
omputer can handle.

Since this is a relatively cheap step, don't spare the caps, it will preven=
t all sorts of aliassing problems in your digital filters if the signal
vari=
ations (or mains noise) don't get that far or are minimised before they get
=
to the converters.

I can't over stress that, you will find it takes an amazingly long time and=
an even more amazing sample rate to get some form of accuracy in a signal
e=
mbedded in a sine-wave, to say nothing about reduced headroom in the signal
=
paths etc.
Keep it out of a device that has to work in a sample/hold fashion and only =
has a limited A/D accuracy as far as possible.

The last step is the one you are interested in, the measurement and/or cont=
rol loop in the computer.

You will find there are as many schemes as there are Engineers, but in the =
end they all try to do the same thing, provide a digital low pass filter in
=
some form or another, the cut of frequencies, poles etc determined by how
ke=
en the designer is and how fast the computer.

There are some rather involved tomes that will give you all the good stuff,=

http://www.embeddedrelated.com/groups/68hc12/show/6089... 21/11/2010
Software Filtering of an ADC input | 68HC12 | Embedded... Página 9 de 13

but it is very rare to have to go that far, simple solutions usually


suffic=
e in most situations.

I.e. a few stages of RC low pass filtering limiting the signals presented t=
o the lowest frequency needed, followed by something similar in the
computer=
is usually enough to serve the purpose.

The trade of in computer filters is of course easily seen, the more involve=
d the filter, the slower your sample rate (or the faster the computer has
to=
be), often negating the gains by introducing aliassing problems.
I.e. sampling at a rate that coincides with some harmonic or other of the i=
ncoming signal will cause all sorts of grief.

If you have made sure to keep higher frequencies out of the signal path in =
the first place, a simple exponential scheme along the lines of adding the
n=
ew value to a multiple of the previous value and weighing it back usually
su=
ffices.

If you find you have to lower the cut of frequency beyond the time availabl=
e, re-evaluate the input filtering rather then trying to compensate in the
C=
PU with all sorts of interesting algorithms.

Hope this helps,

Theo
--------------------------------------------------------To learn more about
Motorola Microcontrollers, please visit
http://www.motorola.com/mcu
o learn more about Motorola Microcontrollers, please visit
http://www.motorola.com/mcu

Yahoo! Groups Links

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )

Re: Software Filtering of an ADC input - Steve Russell - Mar 7 18:21:00 2004

Andy,

I can't resist summarizing what I understand from the previous


correspondence on this topic and Jack Crenshaw's columns in Embedded
Systems Programming. See this mailing list archive for a thread starting
on Jun 28, 2003 8:43 pm
with subject: OT: digital filtering.

Filter in hardware before sampling to reduce the noise in the digital


values due to high frequency noise in the input signal. The high frequency
noise WILL be there, so just do this, don't worry about it.

A simple RC filter will do if you have bandwidth to spare, which is likely.

The ideal is to have a "brick wall" filter that has zero attenuation at
half the sampling frequency, and very large attenuation at the sampling
frequency and beyond.

A simple RC filter is fairly far from this ideal, but setting its cutoff
frequency as low as you can afford will help.

If the highest frequency of interest is well below a reasonable sampling


rate, then use some sort of digital low pass filter, such as averaging, is
needed.

The equivalent of an RC filer, which several have suggested, is the simple


filter which takes a fraction of the input and a fraction of the current
average and adds them.

I believe that the simplest statement of this in C is:

Avg +=( Avg - CurSample ) >> n.

Where Avg and CurSample are 16 bit fractions.

This takes a 16-bit subtract, a shift, and a 16-bit add. No array is


needed. There is a problem when n is 6 or greater because then you start
throwing away significant bits from CurSample. This can be gotten around
by clever code that makes Avg in effect a 24 or 32 bit fraction.

http://www.embeddedrelated.com/groups/68hc12/show/6089... 21/11/2010
Software Filtering of an ADC input | 68HC12 | Embedd... Página 10 de 13

The corner frequency of this filter is set by n. You should choose it to


be above the highest frequency of interest, but probably not as much as 8
times has high.

The software filter is much cheaper that an analog filter for low frequencies.

Hope this helps.


Steve Russell
Nohau Emulators At 04:04 AM 3/7/2004, you wrote:
>I guess this is a bit of a general question, rather than a specific
>HCS12 question, but I want to implement it on an HCS12.....
>
>I want to be able to 'filter' the value returned from an on-board ADC.
>When I say 'filter' I think that perhaps 'average' would be a better
>description.
>
>I want to be able to set an amount of filtering (say 20%) and none of
>the descriptions of digital filters I've found seem to allow this.
>They all seem to be designed to filter a specific frequency range that
>must be stated in the calculations used to design the filter.
>
>I thought about having a ring buffer to which you place the ADC
>results at a constant time interval, then summing the buffer, and
>dividing by the number of samples taken. But then how do you allow for
>the amount of filtering you want to apply ??
>
>I'm going round in circles with this !!
>
>Any thoughts or ideas would be very much appreciated.
>
>I also need the routine to be fairly quick and not take up too much
>RAM as I'm getting a bit short on it.
>
>Andy
*************************************************************************
Steve Russell mailto:
Senior Software Design Engineer http://www.nohau.com
Nohau Corporation phone: (408)866-1820 ext. 1873
51 East Campbell Avenue fax: (408)378-7869
Campbell, CA 95008
*************************************************************************

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )

Re: Software Filtering of an ADC input - Donald E Haselwood - Mar 7 20:55:00 2004

Andy,

From a frequency viewpoint, an average is a low-pass filter. For the


number of "taps" (values stored) it is not the sharpest filter. The
computation is simple, however, as shown in previous posts, and it has a
sharper cutoff than the simple RC digital filter.

The book, Digital Filters, R.W. Hamming, 1977, Prentice-Hall, has a section
on the frequency response of averages. The book (quite readable) is out of
print, but probably can be located through a university library system. In
case your interested, the passband equ:

H(w) = (w * sin(m+1/2) ) / ( (2*m+1)*sin(w/2) )


where w = freq (radians), m = number of taps

Here is a chapter of a (very readable) DSP book you can download (free)
that also talks about averages. He points out that the average is a filter
that responds the quickest to a step input, so for some situations it is
optimal.
http://www.dspguide.com/ch15.htm

You mention "20%" for the filtering. That sounds like you are thinking in
terms an IIR (infinite impluse response) filter, such as the simple digital
implementation of a RC filter, also in the previous posts. If you
implement that form for filter make sure the storage for the accumulated
value will accommodate the largest value. As that "%" gets smaller, which
has the effect of making the cut-off freq lower, the accumulated value gets
very large (but, unlike the circular buffer/average filter, only one value
has to be stored, so it doesn't take a lot of memory).

The characteristics of the signal you are measuring determines the which
type of filter is best (assuming it fits in memory!). The IIR/RC type of
filter "remembers" a big glitch, or noise spike, "forever," i.e. a noise
spike takes a long time to "fade away," whereas the averaging filtering
drops a big number after one cycle around the buffer.

Regards,

Donald E Haselwood At 07:04 AM 3/7/04, you wrote:


>I guess this is a bit of a general question, rather than a specific

http://www.embeddedrelated.com/groups/68hc12/show/6089... 21/11/2010
Software Filtering of an ADC input | 68HC12 | Embedd... Página 11 de 13

>HCS12 question, but I want to implement it on an HCS12.....


>
>I want to be able to 'filter' the value returned from an on-board ADC.
>When I say 'filter' I think that perhaps 'average' would be a better
>description.
>
>I want to be able to set an amount of filtering (say 20%) and none of
>the descriptions of digital filters I've found seem to allow this.
>They all seem to be designed to filter a specific frequency range that
>must be stated in the calculations used to design the filter.
>
>I thought about having a ring buffer to which you place the ADC
>results at a constant time interval, then summing the buffer, and
>dividing by the number of samples taken. But then how do you allow for
>the amount of filtering you want to apply ??
>
>I'm going round in circles with this !!
>
>Any thoughts or ideas would be very much appreciated.
>
>I also need the routine to be fairly quick and not take up too much
>RAM as I'm getting a bit short on it.
>
>Andy >
>--------------------------------------------------------To learn more
>about Motorola Microcontrollers, please visit
><http://www.motorola.com/mcu>http://www.motorola.com/mcu
>o learn more about Motorola Microcontrollers, please visit
><http://www.motorola.com/mcu>http://www.motorola.com/mcu >
>
>----------
>>Yahoo! Terms of Service.

______________________________
Complete portfolio of 32-bit MCUs plus RFID, low-power RF and ZigBee® solutions. Order a Stellaris
Kit today!

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )

RE: Re: Software Filtering of an ADC input - Redd, Emmett R - Mar 8 8:40:00 2004

See below.

Emmett Redd Ph.D. mailto:


Associate Professor (417)836-5221
Department of Physics, Astronomy, and Materials Science
Southwest Missouri State University Fax (417)836-6226
901 SOUTH NATIONAL Dept (417)836-5131
SPRINGFIELD, MO 65804 USA

> -----Original Message-----


> From: theobee00 [mailto:]
> Sent: Monday, March 08, 2004 1:10 AM
> To:
> Subject: [68HC12] Re: Software Filtering of an ADC input
>
> --- In , linktek <linktek@s...> wrote: > > Now stuff that into your excel spreadsheet !!!
>
> Don't think excel has a clock.

As a model, Excel works because each column (or row) can represent the
calculation performed at each successive, uniformly spaced, time step. >
> Cheers,
>
> Theo

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )

Re: Re: Software Filtering of an ADC input - Author Unknown - Mar 8 10:51:00 2004

You can get an excellent tool for characterizing various filters from
Nuhertz Technologies (www.nuhertz.com). I used their Filter Free tool
years ago while researching digital filters for an embedded project. The
tool I used then (version 2.0) generated frequency response graphs,
electrical schematic equivalents, and transfer functions, which made code
generation much easier.

One aspect of this filtering discussion that needs mentioning is sample


rate. You can have an excellent filter algorithm set up, and lose all

http://www.embeddedrelated.com/groups/68hc12/show/6089... 21/11/2010
Software Filtering of an ADC input | 68HC12 | Embedd... Página 12 de 13

benefits from it by sampling at the wrong rate. One product I worked on


was originally set up with an IIR filter tailored for 60Hz rejection, but
the software guy was executing it every 100 ms (10 Hz sample rate). Checks
with the Nuhertz tool showed that this resulted in 0 db filtering. By
using the same IIR filter, but sampling at an 11 Hz rate, 60Hz noise
rejection was better than -45 db. This improvement was confirmed in later
lab tests.

Karl

linktek
<linktek@shaw. To:
ca> cc:
Subject: Re: [68HC12] Re: Software Filtering of an ADC input
03/07/2004
01:58 PM
Please respond
to 68HC12
Hi,

You can always use IIR or a more computative FIR software filter with
taylored coefficients.

However, on the KISS solution. Lets say the problem is lack of data
memory. You would like a
adjustable software filter - field tunable. So how about a cascaded
running
average filter. In stead of one long array, you first average
the first 8 samples into a running average, and stuff the result of the
first average into a second array doing a running average of the results of
the first running average output in another 8 sample array and so on and so
on .... till you run out of memory. Its obvious to see that this is a
easely
tunable filter algorithm.

Now stuff that into your excel spreadsheet !!! (deletia...)


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________

______________________________
Complete portfolio of 32-bit MCUs plus RFID, low-power RF and ZigBee® solutions. Order a Stellaris
Kit today!

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )

Re: Re: Software Filtering of an ADC input - Darrell N. - Mar 8 11:32:00 2004

You could try this:

Average = 80%(OldSample) + 20%(NewSample). Store Average at


OldSample.

The percentages can be changed to whatever you want. This does


not require a big buffer or serious calculations. It does have
some disadvantages, however.

Another method I use for simple filtering is:

OldSample + NewSample
-----------------------------------
2

This gives rudimentary filtering, but you can sprinkle it around


in your code after calculations to make things smoother.

I also use a 16 value ring filter, where the new value is added
to a sum, and the oldest value is subtracted out. For 16 bit
values, this requires 32 bytes of buffer, a 2 byte sum, and a 2
byte pointer, as well as your original and filtered values.
This is a total of 40 bytes, which is a pretty good chunk of
RAM.

> I thought about doing it like this, but it would need to have a
> large array to average the samples, and I can't afford the RAM
> space that would use.
>
> I'm sure there must be some clever way of doing this without
> using an array - just can't see it yet.

http://www.embeddedrelated.com/groups/68hc12/show/6089... 21/11/2010
Software Filtering of an ADC input | 68HC12 | Embedd... Página 13 de 13

Regards,
Darrell Norquay

Datalog Technology Inc. Calgary, Alberta, Canada


Voice: (403) 243-2220 Fax: (403) 243-2872
Email: Web: www.datalog.ab.ca

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )

http://www.embeddedrelated.com/groups/68hc12/show/6089... 21/11/2010

You might also like