|
|
View previous topic :: View next topic |
Author |
Message |
davidVu Guest
|
Averaging ADC inputs |
Posted: Tue Jan 02, 2007 1:45 am |
|
|
I need help with a code that averages the voltage from the ADC output over a 1 minute period and stores it in a variable. |
|
|
Ttelmah Guest
|
|
Posted: Tue Jan 02, 2007 6:10 am |
|
|
Hint.
The answer depends on how often you read the ADC...
Also, you make no mention of whether you want a 'rolling' average (available at any time), or just a result at the end of a minute.
If you are reading the ADC at a known interval (say 100mSec), timed off an interrupt, then all you need to do, is mainating a 'sum', in a 32 bit value (60*10*1024 = 614400 - too large for an int16), and at the end of the minute, divide this by 600. The division will depend on the sampling interval.
For a 'rolling' average, since the total number of samples involved is too large to store, one solution, is the 'chasing' average, using code like:
Code: |
int16 adc_avg() {
static int32 sum;
int16 rval;
sum+=read_adc();
rval=sum/600;
sum-=rval;
return rval;
}
|
Here, the new value is added to a running 'sum', and then the new average value generated and subtracted from the same sum. Call this at regular intervals, and you get a nice 'smoothed' result. However this does not give the simple 'average', but instead the sum of the differences from the rolling average. For many applications this is 'nicer' since the value can be used at any point. Changing the calling interval, and division factor, gives different damping levels.
Best Wishes |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
|
|
|
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
|