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

Why is this code not taking the numerical averge correctly?

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



Joined: 06 Sep 2003
Posts: 49

View user's profile Send private message

Why is this code not taking the numerical averge correctly?
PostPosted: Sun Jan 25, 2004 1:27 pm     Reply with quote

The code below, running on a 16f876a, CCSv1.184, produces the following output. Can anyone figure out why the numerical average is coming out to be 0385? It should be around 0333.

atodValue = 0333
atodValue = 0333
atodValue = 0333
atodValue = 0334
atodValue = 0333
atodValue = 0334
atodValue = 0333
atodValue = 0332
atodValue = 0333
atodValue = 0334
atodValue = 0333
atodValue AVG= 0385


Code:

int16 atodValue, int16Temp;
   
int16Temp = 0;
for(i=0;i<=10;i++)
{
   atodValue = Read_ADC();
   int16Temp = int16Temp + atodValue;
   fprintf(serialPC, "atodValue = %LX\r\n", atodValue);
   delay_ms(50);
}

atodValue = 0;
atodValue = int16Temp / (long)10;
fprintf(serialPC, "atodValue AVG= %LX\r\n", atodValue);


Last edited by Freddie on Sun Jan 25, 2004 1:55 pm; edited 1 time in total
Gerrit



Joined: 15 Sep 2003
Posts: 58

View user's profile Send private message

PostPosted: Sun Jan 25, 2004 1:45 pm     Reply with quote

Hi Freddie,

Looks to me that you take 11 samples <= 10

and you devide by 10 that shot be +- 10 % more for output value.


Gerrit
Freddie



Joined: 06 Sep 2003
Posts: 49

View user's profile Send private message

PostPosted: Sun Jan 25, 2004 1:52 pm     Reply with quote

Yes, that was it! Sometimes you just can't see it for yourself. Thanks.
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