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

ADC in a for cycle

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



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

ADC in a for cycle
PostPosted: Tue May 29, 2007 8:58 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed May 30, 2007 12:38 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed May 30, 2007 1:08 am     Reply with quote

Example 2 is the way to go.
The read adc command initialises the conversion.

JFK
oxxyfx



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

PostPosted: Wed May 30, 2007 6:35 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed May 30, 2007 8:22 am     Reply with quote

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