View previous topic :: View next topic |
Author |
Message |
vtrx
Joined: 11 Oct 2017 Posts: 142
|
Reading the average ADC value. |
Posted: Wed Aug 19, 2020 2:25 pm |
|
|
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
|
Re: Reading the average ADC value. |
Posted: Wed Aug 19, 2020 2:42 pm |
|
|
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:
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Wed Aug 19, 2020 2:44 pm |
|
|
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
|
Re: Reading the average ADC value. |
Posted: Wed Aug 19, 2020 3:02 pm |
|
|
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:
|
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
|
|
Posted: Wed Aug 19, 2020 3:03 pm |
|
|
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. |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Wed Aug 19, 2020 3:03 pm |
|
|
:P
Thing is, what you wrote was perfectly valid C code. It's just not what you intended. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Wed Aug 19, 2020 3:45 pm |
|
|
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: 1345
|
|
Posted: Wed Aug 19, 2020 9:21 pm |
|
|
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:
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
|
|
Posted: Thu Aug 20, 2020 6:24 pm |
|
|
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:
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
|
|
Posted: Fri Aug 21, 2020 1:18 pm |
|
|
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: 9221 Location: Greensville,Ontario
|
|
Posted: Fri Aug 21, 2020 2:32 pm |
|
|
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 |
|
|
|