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 support@ccsinfo.com

Averaging ADC inputs

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







Averaging ADC inputs
PostPosted: Tue Jan 02, 2007 1:45 am     Reply with quote

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







PostPosted: Tue Jan 02, 2007 6:10 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jan 02, 2007 8:30 am     Reply with quote

There is a good discusion regarding averaging and smoothing an ADC value in:

http://www.ccsinfo.com/forum/viewtopic.php?t=22033&highlight=averaging


Humberto
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