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

set variable to average of ten ADC readings

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



Joined: 19 Nov 2003
Posts: 45
Location: Oxford

View user's profile Send private message Visit poster's website

set variable to average of ten ADC readings
PostPosted: Wed May 24, 2006 4:34 am     Reply with quote

Hi guys, wondered if we coudl discuss the most elegant method of taking a signal average

I have a PIC18F452 and I am using a port into a DAC to put out an anolog voltage. I have a fairly noisy AI and would like to do is to take the average voltage over a time and then set the output of the DAC to the average input.

what I am trying to do at the moment (it works ok) is this and I am sure there must be a more elogant solution without using the dreaded float.

Nice

Code:

int adcValue;
long defaultI, voltReadInt;
float voltRead;

defaultI=0;
adcValue=0;
voltReadInt=0;
voltRead=0;

for (i=0;i<11;i++)
{
   if(i!=0)
   {
      adcValue=Read_ADC();
      voltReadInt = voltReadInt + adcValue;
      printf("\n\r adcValue = %u, voltReadInt = %lu",adcValue, voltReadInt );
   }      
   delay_ms(5);
}
voltRead=(float)voltReadInt/10;
defaultI=(int)voltRead;
printf("\n\r VoltRead = %f, VoltReadInt = %lu,  Final defaultI = %lu",voltRead, voltReadInt, defaultI );


I get this output

adcValue = 97, voltReadInt = 97
adcValue = 97, voltReadInt = 194
adcValue = 97, voltReadInt = 291
adcValue = 91, voltReadInt = 382
adcValue = 94, voltReadInt = 476
adcValue = 99, voltReadInt = 575
adcValue = 99, voltReadInt = 674
adcValue = 99, voltReadInt = 773
adcValue = 104, voltReadInt = 877
adcValue = 99, voltReadInt = 976
VoltRead = 97.600000, VoltReadInt = 976, Final defaultI = 97
_________________
Nice!!!
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed May 24, 2006 4:47 am     Reply with quote

What you have should work fine.

If you add 5 to the sum before diviting by 10 you will get a rounded result instead of a truncated one, giving a final value of 98 for a sum of 976.

If your A/D gives generaly good readings with an occasional really bad one then simple averaging doesn't work well. I often use what I call "Olymoic scoring" which is to sum the readings as you do, plus record the largest and smallest readings. Once you have taken your readings subtract the largest and smallest from the sum and divide the remaining 8 readings by 8 to get an average with the worst offenders removed. I find it gives very good results with only a little more code.
_________________
The search for better is endless. Instead simply find very good and get the job done.
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Wed May 24, 2006 6:35 am     Reply with quote

What SherpaDoug said, plus I would use either unsigned int16 or unsigned int32 instead of float for your accumulator. Runs faster but keep in mind that you could overflow.

Furthermore, if you always take a power of 2 number of samples into your average, the divide becomes a simple shift.

Another way (although not strictly an average) would be a simple digital filter:

Code:
newVal = (newVal + Read_ADC()) / 2;

but be sure to seed newVal with the result of a Read_ADC() call prior to using the "filter". You can modify this filter several ways, another good one is
Code:
newVal = (newVal + newVal + newVal + Read_ADC() / 4;
which places more emphasis on past values than the current one.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Ttelmah
Guest







PostPosted: Wed May 24, 2006 7:24 am     Reply with quote

There is also the 'rolling average' system that has been posted here many times in the past.
Code:


int16 adcsum;
int16 voltRead;

adcsum+=Read_ADC();
voltread=adcsum/16;
adcsum-=voltread;

printf("\n\r VoltRead = %5.1w",voltRead);


I have cut of a lot of bits that you may want to put back, but this gives effectively a 1/16th average, with just one addition, one subtraction, and a single division by 16, which because it is a nice binary value, will be done using subtraction.
I output effectively the result /10, by using the '%5.1w' format, which treats an integer as a scaled float for the purposes of output. This will be a lot quicker than the normal average, and will just keep updating each time it is called (provided the 'sum' is static of course).

Best Wishes
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