View previous topic :: View next topic |
Author |
Message |
oxxyfx
Joined: 24 May 2007 Posts: 97
|
ADC in a for cycle |
Posted: Tue May 29, 2007 8:58 pm |
|
|
Hello,
I want to do 5 ADC readings one after the other and then calculate an avarage of the 5 readings.
I am not sure if I have to initialize the ADC every time inside of the loop, or outside?
Example 1:
Code: |
int8 v[5];
int cX;
For(cX = 0; cX < 3; cX++) {
setup_adc_ports(AN2);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel( 2 );
delay_us(30);
v[cX] = read_adc();
} |
or example 2?
Code: |
int8 v[5];
int cX;
setup_adc_ports(AN2);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel( 2 );
For(cX = 0; cX < 3; cX++) {
delay_us(30);
v[cX] = read_adc();
} |
I am not sure whichone should be used. Example 2 makes sense to initialize the ADC port and channel outside of the loop, but then how does the ADC know that it has to start the conversion so I can read it's value?
Thanks. |
|
|
inservi
Joined: 13 May 2007 Posts: 128
|
|
Posted: Wed May 30, 2007 12:38 am |
|
|
Hello,
Did you try each version ?
I think that in this example, you make only 4 loop:
For(cX = 0; cX < 3; cX++)
Try with "cX < 4" for 5 loops.
find 'set_adc' in the help. you will see there is a sample file 'ex_admm.c'
then try it !
dro _________________ in médio virtus |
|
|
jfk1965
Joined: 21 Oct 2003 Posts: 58
|
|
Posted: Wed May 30, 2007 1:08 am |
|
|
Example 2 is the way to go.
The read adc command initialises the conversion.
JFK |
|
|
oxxyfx
Joined: 24 May 2007 Posts: 97
|
|
Posted: Wed May 30, 2007 6:35 am |
|
|
Dro, thanks, I mistyped the loops when I cut then out and pasted them here. I didn't try any of this yet, I am still waiting form a new shipment of PIC's to arrive - hopefully today.
Jfk, Thank you, in case of the second example, should I put the delay after the v[cX] = read_adc(); to give some time for the conversion to complete?
Thx. |
|
|
jfk1965
Joined: 21 Oct 2003 Posts: 58
|
|
Posted: Wed May 30, 2007 8:22 am |
|
|
oxxyfx wrote: |
Jfk, Thank you, in case of the second example, should I put the delay after the v[cX] = read_adc(); to give some time for the conversion to complete?
Thx. |
The delay is fine where it is as it stills gives a delay until the conversion starts.
JFK |
|
|
|