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

Help with A/D

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



Joined: 23 Dec 2008
Posts: 83

View user's profile Send private message

Help with A/D
PostPosted: Tue Nov 23, 2010 8:40 pm     Reply with quote

Hello All,
I have a bunch of problems to be solved out with the ADC and I am seriously looking for your advice. I will post the schematic and the code in a while. Before that I would like to know a few things.

I am using 16F676 (SMD) and using an external interrupt. Once the interrupt fires, I am reading the ADC from Phototransistor. The results are not constant, I have a very big change with the ADC value. I am using 8 bit ADC and will this give such difference? Will the change in the reference voltage produces such results ? What is the best way to read the ADC from a Phototransistor?

Regards
Arun
arunkish



Joined: 23 Dec 2008
Posts: 83

View user's profile Send private message

PostPosted: Tue Nov 23, 2010 8:54 pm     Reply with quote

I am posting part of the code and the declarations that reads the value.
Code:


//*****************************
#include<16F676.h>
#fuses NOBROWNOUT,NOWDT,XT,NOPROTECT,NOMCLR,PUT
#use delay(clock=4000000)

static BYTE ref_val,val,STAT;

//*****************************


//*******************************************************/
void readvalue(void)
{
   
      setup_adc(ADC_CLOCK_INTERNAL);
      for(j=0;j<=800;j++);
      set_adc_channel(0);delay_us(100);
      val = read_adc();   
      setup_adc(ADC_OFF);
      STAT=0;   
         if(val>ref_val)
         {
            STAT=1;
         }
         

   
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19401

View user's profile Send private message

PostPosted: Wed Nov 24, 2010 3:03 am     Reply with quote

The odds are that the real problem is with the impedance's involved. You _need_ a buffer op-amp to have any hope of reading values from a photo-transistor. The 'odds' are that you have source impedances from the photo circuit that are many hundreds of kilo-ohms, when the PIC ADC requires source impedance's of only a few kR.....

As a comment, most of what you are doing, seems 'pointless' or slightly wrong. Comments inline:
Code:

void readvalue(void) {
   
      setup_adc(ADC_CLOCK_INTERNAL); //Is this a recommended clock
      //for a chip running at 4MHz?......

      for(j=0;j<=800;j++); //Why this. It is just equivalent to a delay.....
      //Assume 'j' is an int16, or this won't work.
     
      set_adc_channel(0);delay_us(100);

      val = read_adc();   

      setup_adc(ADC_OFF); //
      //Do you really need to save power by doing this?.
      /*STAT=0;   

      if(val>ref_val)
         {
            STAT=1;
         }*/
      //Why not:
      if (val>ref_val)
          STAT=1;
      else
          STAT=0;
      //Saves having to write to 'STAT' twice.
 }


Best Wishes
arunkish



Joined: 23 Dec 2008
Posts: 83

View user's profile Send private message

PostPosted: Wed Nov 24, 2010 6:43 am     Reply with quote

Hi
Thank you Ttelmah for your replay. I have declared j as long int. What is the recommended clock for the PIC that operates at 4 Mhz. Can u suggest me a buffer op-amp for this operation?

Thank you
arunkish



Joined: 23 Dec 2008
Posts: 83

View user's profile Send private message

PostPosted: Wed Nov 24, 2010 6:58 am     Reply with quote

I have included the schematic. Please advice.

Last edited by arunkish on Sun Sep 04, 2011 9:48 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 24, 2010 5:37 pm     Reply with quote

AN951 - Amplifying High-Impedance Sensors – Photodiode Example:
http://ww1.microchip.com/downloads/en/AppNotes/00951a.pdf

MCP606/7/8/9 Micropower CMOS Op-Amp:
http://ww1.microchip.com/downloads/en/devicedoc/11177d.pdf
See page 13 for photodiode example circuit.
Ttelmah



Joined: 11 Mar 2010
Posts: 19401

View user's profile Send private message

PostPosted: Thu Nov 25, 2010 2:56 am     Reply with quote

And, on the ADC clock, _read the data sheet_. Table 7.1. Look at the two white squares for the 4MHz column. Look at note4 below for the 'point' about ADC_CLOCK_INTERNAL.
You really do need to do this. It also in paragraph 7.2, has the point about the ADC source impedance. The data sheet needs to be used whenever designing with the PIC.

Best Wishes
arunkish



Joined: 23 Dec 2008
Posts: 83

View user's profile Send private message

PostPosted: Mon Nov 29, 2010 9:15 am     Reply with quote

Dear All,
I made sample readings of the AD input from phototransistor and now I am having the value with a +/- 3 change. Seems ok now.Smile

I tried to read out the light from IR and as I said I have a difference of +/- 3. I have a set value and once the analog value goes beyond the set value, LED glows up. Every thing works fine. But somehow, the value goes beyond the set point and the LED glows up when i am not increasing the light. It seems that the value suddenly raises and then comes back to the original level. I am confused with this and I have some questions.

1. I am powering the IR from the controller for 20 ms and reading the value. It draws upto 26 mA. Will that be a problem ?

2. I am also having the external interrupt enabled. Will that make such changes?

3. As mentioned in my first post, the supply at the VDD varies from 4.2 to 4.6. Is this the one causing the error?

Please help me to come out of this issue.

Thanks
Ttelmah



Joined: 11 Mar 2010
Posts: 19401

View user's profile Send private message

PostPosted: Mon Nov 29, 2010 10:09 am     Reply with quote

Yes, probably no, yes.....

Maximum output rating for a PIC pin is 25mA. Even at this, you are only warranted to get VoutH min, which is significantly below the supply voltage. For 26mA, you need a buffer.
Interrupt should not make any change, but this depends on your code.....
When using Vdd as Vref, the repeatability is dependant on the stability of the supply, and the accuracy, on the supplies accuracy. Sounds as if both are bad on your hardware.....

Best Wishes
arunkish



Joined: 23 Dec 2008
Posts: 83

View user's profile Send private message

PostPosted: Tue Nov 30, 2010 9:27 pm     Reply with quote

Sorry all, for asking a stupid question. I changed and checked the hardware part, but still the problem continues. I'm using 3.222 PCM and PCH versions of compiler. Are there any bugs with this version of compiler in the adc part. Can anyone recommend me a suitable version for working with ADC.
arunkish



Joined: 23 Dec 2008
Posts: 83

View user's profile Send private message

PostPosted: Tue Nov 30, 2010 10:09 pm     Reply with quote

I am sorry. That should not be the bug with the compiler part. If it is so, it should be giving me the trouble always. I just can't figure out what goes wrong.

Thank you
SherpaDoug



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

View user's profile Send private message

PostPosted: Wed Dec 01, 2010 7:08 am     Reply with quote

This does not seem like a compiler problem, but a hardware problem. You say VDD varies between 4.2V and 4.6V. What causes this change? Does it correlate with the light level or the 26mA load? 26mA will cause heating of the PIC die which will make the A/D drift.
_________________
The search for better is endless. Instead simply find very good and get the job done.
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