PDA

View Full Version : How to make adcin stable?



Pic2008
- 15th December 2008, 08:56
1. Using the following code, I would like to know if using a higher ADC_SAMPLEUS will make adcin with more stable/accurate result?

2. How about the min delay before executing adcin again?



DEFINE OSC 20

Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS

ADCON1= %00001001


loop:
ADCIN 0, adval ' Read channel0 RA.0
ADCIN 1, adval2 ' Read channel1 RA.1
pauseus 100
goto loop

mackrackit
- 15th December 2008, 11:33
A capacitor might be needed, what are you reading?
For example...
A LM34/35 will be unstable sometime depending on cable and all will need a capacitor from signal to ground (zero rail) to keep it from fluctuating. 22uf works well.

Charles Linquis
- 15th December 2008, 13:33
I see you are using an external Vref. Are you certain it is stable? Also, I have had better results using the "real" clock (not R/C) for the ADC clock. At 20Mhz, you can use Fosc/32 (ADCON2 = %10000010) in most chips.
Make sure the source impedance into the ADC pin is < 2K, and use a .1uF to ground from the ADC Pin.
If you have the time, take 4 to 16 samples, add them together and divide to get an average.

Pic2008
- 15th December 2008, 13:52
I only need 2 analog inputs, no other configuration without Vref+ unless I enable more analog input pins.

I'm connecting phototransistor with pull up 350K to +5V, does this mean I need to increase ADC_SAMPLEUS?

The circuit only have one +5V source, so I connect Vref+ to Vdd pin of PIC directly without any resistor. Is this ok?

Charles Linquis
- 15th December 2008, 15:14
You can connect Vref to Vdd, but it is easier just to set ADCON1 bits 5&4 to "0" which connects Vref to Vdd internally.

Also, READ YOUR DATASHEET. Here is a cut and paste directly from the A/D section of the 18F8722 datasheet:

The
maximum recommended impedance for analog
sources is 2.5 kΩ.

Other chips have similar recommendations.

Your 350K is WAAAAAY too high! If you really need a 350K pull-up, you will
have to buffer the analog signal with an op-amp to get accurate readings.


If you just want to sense the presence of an object, you would be better off using a digital input, and connecting the collector of your phototransistor to 5V through a 10K or 22K resistor.

Ioannis
- 15th December 2008, 20:15
I would suggest to use an opamp as a unity gain buffer befor feeding the ADC of the PIC.

As Charles stated 350K is too high for the ADC to work reliable.

Also to be sure that the PIC is working OK, use a trimmer in the place of the phototransistor and check the results. If they are stable and seem expected then you can be sure that PIC is OK. Of course it would be a very good idea to average the samples. Have a look at Darrels averaging routine at his site:

http://www.pbpgroup.com/modules/wfsection/article.php?articleid=7

Ioannis

rogerroger
- 17th January 2009, 16:50
Can anyone tell me why I am getting 16 bit results from this code? I expect at 5 volts in I should see 1023 but I see 65535.I am using a 16f616.



DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50
TRISA = 255 ' Set PORTA to all input
ADCON1 = 2 ' PORTA is analog
ADCIN 0, A0 ' PORTA.0 PIN 13

Samoele
- 17th January 2009, 16:57
change your ADCON1 =2 with

ADCON1=%10000010

rogerroger
- 17th January 2009, 17:31
Thank you for your reply. I made that change, I am now back to 8 bit results. I would like to have 10 bit results.

Thanks

Samoele
- 17th January 2009, 18:20
your variable A0 by ADCIN must be defined as word and no as byte

rogerroger
- 17th January 2009, 18:45
Thank you so much for your help!

This is all of the code,

A0 VAR WORD
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50
TRISA = 255 ' Set PORTA to all input
'ADCON1 = 2 ' This gives me 16 bit results
ADCON1=%10000010 'This gives me 8 bit results
ADCIN 0, A0 ' PORTA.0 PIN 13
SEROUT PORTC.5,6,[" A0 = ",#A0,10]

Samoele
- 20th January 2009, 07:41
your defines are ok.

A0 VAR WORD
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50
TRISA = 255 ' Set PORTA to all input
ADCON1=%10000010 'This gives me 8 bit results
ADCIN 0, A0 ' PORTA.0 PIN 13
SEROUT PORTC.5,6,[" A0 = ",#A0,10]

note!
When use 8 bit PIC then working this only with 8 bit resolution. Check datasheet from your PIC