View previous topic :: View next topic |
Author |
Message |
arunkish
Joined: 23 Dec 2008 Posts: 83
|
Help with A/D |
Posted: Tue Nov 23, 2010 8:40 pm |
|
|
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
|
|
Posted: Tue Nov 23, 2010 8:54 pm |
|
|
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: 19513
|
|
Posted: Wed Nov 24, 2010 3:03 am |
|
|
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
|
|
Posted: Wed Nov 24, 2010 6:43 am |
|
|
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
|
|
Posted: Wed Nov 24, 2010 6:58 am |
|
|
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
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Thu Nov 25, 2010 2:56 am |
|
|
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
|
|
Posted: Mon Nov 29, 2010 9:15 am |
|
|
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.
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: 19513
|
|
Posted: Mon Nov 29, 2010 10:09 am |
|
|
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
|
|
Posted: Tue Nov 30, 2010 9:27 pm |
|
|
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
|
|
Posted: Tue Nov 30, 2010 10:09 pm |
|
|
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
|
|
Posted: Wed Dec 01, 2010 7:08 am |
|
|
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. |
|
|
|