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

what exectly does read_adc(ADC_START_AND_READ) do

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



Joined: 30 Sep 2003
Posts: 89

View user's profile Send private message

what exectly does read_adc(ADC_START_AND_READ) do
PostPosted: Wed Jan 18, 2006 1:22 pm     Reply with quote

The manual is not real clear about this.

I have 2 choices as to what this does.


set_adc_channel(next_channel);
value=read_adc(ADC_START_AND_READ);

Does it start a conversion of next_channel and then give you the value of the current channel, or does it start the conversion of next_channel wait for it to complete and then give you the value?
_________________
-Pete
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

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

PostPosted: Wed Jan 18, 2006 2:03 pm     Reply with quote

Doing an A to D converstion takes time. If you want you can start the conversion and go off and do other stuff, then come back and read the results. Other wise you can do a start-and-read which will wait for the results to become available((waisting time)).
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Thu Jan 19, 2006 8:19 am     Reply with quote

By the way you probably want some time delay between selecting the channel and starting the conversion. It takes the analog circuitry a little while to settle to the new voltage before the digital circuits can start measuring it. Look in the data sheet for "settling time"
I usually select the next channel as soon as I am done reading the old one. Then the analog circuits can settle while I am processing the data from the last reading.
_________________
The search for better is endless. Instead simply find very good and get the job done.
pfournier



Joined: 30 Sep 2003
Posts: 89

View user's profile Send private message

PostPosted: Thu Jan 19, 2006 8:48 am     Reply with quote

I've gotten 2 replies, but no one has answered the question I asked.
_________________
-Pete
Ttelmah
Guest







PostPosted: Thu Jan 19, 2006 9:25 am     Reply with quote

The ADC 'cycle' comprises a whole series of 'parts'. First, you have to connect the input multiplexor to the voltage required (set_adc_channel). Then you need to wait for the capacitor inside the chip to charge to the voltage you want to read (the time taken for this, depends on the source impedance, the DC resistance present inside the chip, and the capacitor size - all in the data sheet - typically at least 10uSec needs to be allowed for this). Then the ADC circuit itself, can be triggered to do a read (read_adc(ADC_START_ONLY)). This takes twelve cycles of the internal ADC clock (so typically about 24uSec) to complete. You can then read the value (read_adc(ADC_READ_ONLY)). This only takes a couple of machine cycles transfer the bytes from the registers.
The default operation, is to trigger the start, and then wait for the chip to actually complete the cycle. This is 'read_adc(ADC_START_AND_READ)'. This is the _default_ behaviour, if you just call 'read_adc()'.
The downside of this, is that you sit waiting for many microseconds, when you could be performing other operations.

Best Wishes
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

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

PostPosted: Thu Jan 19, 2006 9:55 am     Reply with quote

To answer your question.

NO.

The chanel stays to what you set untill you change it. set_adc_channel(1);
For all commands.


if doing two chan I make a int16 loop counter in main while(1) loop


Code:
while(1){
  MainWhileCnt++;
  if (MainWhileCnt==0)
  {
    value1=read_adc(ADC_READ_ONLY);//messed up on first go-round of loop
    set_adc_channel(0);//set for next reading MainWhileCnt==32767
    read_adc(ADC_START_ONLY);
  }
  if (MainWhileCnt==32768)
  {
    value0=read_adc(ADC_READ_ONLY);
    set_adc_channel(1);//set for next reading MainWhileCnt==0
    read_adc(ADC_START_ONLY);
  }
}


you could of course get around the messed up first read by pr-setting the chan to 1 and doing a start before you get into the loop
Ttelmah
Guest







PostPosted: Thu Jan 19, 2006 10:04 am     Reply with quote

What you post, has the significant problem, that it'll return garbage.
You select an ADC channel, and immediately start the read. The 'result' will be a value something between the value on the last selected line, and the new line. You need a delay between the 'select', and the 'start', if the reading is really going to make sense. The _reading_ begins as soon as you send 'read_adc(ADC_START_ONLY)'. As soon as this is done, the input voltage is disconnected from the internal capacitor, and the sample cycle starts.

Best Wishes
pfournier



Joined: 30 Sep 2003
Posts: 89

View user's profile Send private message

PostPosted: Thu Jan 19, 2006 10:24 am     Reply with quote

Ttelmah wrote:
The default operation, is to trigger the start, and then wait for the chip to actually complete the cycle. This is 'read_adc(ADC_START_AND_READ)'. This is the _default_ behaviour, if you just call 'read_adc()'.


Ttelmah seems to have answered the question. I assumed the same thing. I can't see the use of the ADC_START_AND_READ. What is needed is a ADC_READ_AND_START instead.
_________________
-Pete
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

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

PostPosted: Thu Jan 19, 2006 10:44 am     Reply with quote

I must admit I tryed to simplifiy my code from what I use.
Code:
  while(1){
    MainWhileCnt++;
    if (MainWhileCnt==0)
    {
      set_pwm2_duty(read_adc(ADC_READ_ONLY));
      set_adc_channel(0);//set for next reading
    }
    if (MainWhileCnt==16384)
    {
      read_adc(ADC_START_ONLY);
    }
    if (MainWhileCnt==32768)
    {
      set_pwm1_duty(read_adc(ADC_READ_ONLY));
      set_adc_channel(1);//set for next reading
    }
    if (MainWhileCnt==49152)
    {
      read_adc(ADC_START_ONLY);
    }
}

Just trying to get the idea accross. Guess I caused more harm than good.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Thu Jan 19, 2006 11:27 am     Reply with quote

Once defined the ADC setups, the AD convertion itself is a step by step procedure.
CCS offers and enhace the (PIC) hability to do this secuence in different 'flavours'.

The basic operation:
value = read_adc (parameter);
can get different behaviours according to the selected -optional- parameters:

1)-------read_adc (ADC_START_AND_READ );

Default option, has the same efect that
value = read_adc ();

2)-------read_adc (ADC_START_ONLY);

This option set the GO/DONE bit in the ADCON0 register, disconnect the holding capacitor
from the Analog Input, start the conversion and quit, do not wait for the end of convertion.

3)-------read_adc (ADC_READ_ONLY);

This option clear the GO/DONE bit in the ADCON0 Register, set the bit 6 (ADIF) in the PIR1
register, generates an interrupt if INT_AD was enabled and read the last conversion result
stored in ADRESH and ADRESL registers.


pfournier wrote:
Quote:

I can't see the use of the ADC_START_AND_READ.

Not exactly. If you enable the INT_AD you must use the ADC_START_AND_READ (default) option.

pfournier wrote:
Quote:

What is needed is a ADC_READ_AND_START instead.

Not exactly. If you are using the AD feature and doesn't want to spend time waiting
at the end of conversion, you need to use ADC_START_ONLY option.


Humberto
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