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

Reading the average ADC value.

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



Joined: 11 Oct 2017
Posts: 142

View user's profile Send private message

Reading the average ADC value.
PostPosted: Wed Aug 19, 2020 2:25 pm     Reply with quote

Hi.
I'm trying to 'filter' the ADC value, but I always get zero.
What is wrong?
ADC 10 bits.

Code:
 

...    int32 aux0;

       aux0 = 0;
       set_adc_channel(0);
     for(o=0;o<10,o++;)
      {   
       delay_us(20);
       aux0 += read_adc();
      }
       Ad0 = aux0/10;
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

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

Re: Reading the average ADC value.
PostPosted: Wed Aug 19, 2020 2:42 pm     Reply with quote

vtrx wrote:
Hi.
I'm trying to 'filter' the ADC value, but I always get zero.
What is wrong?
ADC 10 bits.

Code:
 

...    int32 aux0;

       aux0 = 0;
       set_adc_channel(0);
     for(o=0;o<10,o++;)
      {   
       delay_us(20);
       aux0 += read_adc();
      }
       Ad0 = aux0/10;


Your for loop condition looks wrong.

It should be:
Code:
for(o=0;o<10;o++)
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Aug 19, 2020 2:44 pm     Reply with quote

I don't see anything 'wrong' but using 'o' as a variable is kinda hard on my eyes, looks like a '0' or 'O' to me....


You really need to post the 'printing' portion of your program as well as the ADC section.....

hmm.. are you SURE the ADC is properly connected and working ? What is the source of the voltage ? Is there a bad (grounded) trace on the PCB, or miswiring of a perfboard ?

Please tell us that this IS a REAL PIC and parts and NOT a'simulation'.

Which PIC and compiler version ??

Jay
vtrx



Joined: 11 Oct 2017
Posts: 142

View user's profile Send private message

Re: Reading the average ADC value.
PostPosted: Wed Aug 19, 2020 3:02 pm     Reply with quote

dluu13 wrote:
vtrx wrote:
Hi.
I'm trying to 'filter' the ADC value, but I always get zero.
What is wrong?
ADC 10 bits.

Code:
 

...    int32 aux0;

       aux0 = 0;
       set_adc_channel(0);
     for(o=0;o<10,o++;)
      {   
       delay_us(20);
       aux0 += read_adc();
      }
       Ad0 = aux0/10;


Your for loop condition looks wrong.

It should be:
Code:
for(o=0;o<10;o++)


thank you very much, that was it.
The compiler should have warned me ...

Wrong:
for(o=0;o<10,o++;)

Right:
for(o=0;o<10;o++)
vtrx



Joined: 11 Oct 2017
Posts: 142

View user's profile Send private message

PostPosted: Wed Aug 19, 2020 3:03 pm     Reply with quote

temtronic wrote:
I don't see anything 'wrong' but using 'o' as a variable is kinda hard on my eyes, looks like a '0' or 'O' to me....


sorry, for me it was also weird, i already changed it. Embarassed
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

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

PostPosted: Wed Aug 19, 2020 3:03 pm     Reply with quote

:P

Thing is, what you wrote was perfectly valid C code. It's just not what you intended.
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Aug 19, 2020 3:45 pm     Reply with quote

I have a hard time with the light green 'code' colour.
I missed the colon for a semicolon.
I like the big, black, bold font though !!

Getting old is so much fun, not....

Course now I'm wondering why the compiler didn't flag it as an error ? hmm...
Jay
jeremiah



Joined: 20 Jul 2010
Posts: 1346

View user's profile Send private message

PostPosted: Wed Aug 19, 2020 9:21 pm     Reply with quote

temtronic wrote:

Course now I'm wondering why the compiler didn't flag it as an error ? hmm...
Jay


It's valid. C allows for a string of comma separated statements and it allows for empty for loop params. Essentially the loop condition was technically:

Code:

o<10,o++


In C when you have a set of comma separated statements, it evaluates all of them and returns the result of the last one as the result for all of the statements. So the for loop was checking the o++ which was 0 and ending the loop right off without even running it.
vtrx



Joined: 11 Oct 2017
Posts: 142

View user's profile Send private message

PostPosted: Thu Aug 20, 2020 6:24 pm     Reply with quote

jeremiah wrote:
temtronic wrote:

Course now I'm wondering why the compiler didn't flag it as an error ? hmm...
Jay


It's valid. C allows for a string of comma separated statements and it allows for empty for loop params. Essentially the loop condition was technically:

Code:

o<10,o++


In C when you have a set of comma separated statements, it evaluates all of them and returns the result of the last one as the result for all of the statements. So the for loop was checking the o++ which was 0 and ending the loop right off without even running it.


I did not expect an error warning, but that the result would be null.
I typed the wrong code snippet and didn't notice.
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

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

PostPosted: Fri Aug 21, 2020 1:18 pm     Reply with quote

The thing is, you have already initialized the aux0 to 0 so if it doesn't run through the for loop, it is still valid.
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Aug 21, 2020 2:32 pm     Reply with quote

hmm, one possible problem...
How are the variables 'o' and 'Ad0' declared ?
To me 'o' should be unsigned interger8 or char, 'Ad0' should be unsigned int32.

Also, for averaging, think like a 'computer', take 8 readings ! The average is then a simple and FAST /8 which the compiler will use the rotate instruction.

Jay
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