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 CCS Technical Support

Stable ADC reading

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



Joined: 18 Oct 2006
Posts: 8

View user's profile Send private message

Stable ADC reading
PostPosted: Thu Dec 28, 2006 8:35 am     Reply with quote

I'm using PIC18F8722 and PIC18F6722 for my last projects and I had really hard time measuring the analog inputs in 10 bit adc mode. Even using Vref+/- inputs couldn't help a lot. I have tried many accumulation methods but they gave me not so stable result for measuring of slowly changed values for temperature or battery voltage readings for example.
One method that I wrote and I found as stable enough for these micros is this one:

Code:

#define bit_size 16  // 4,8,16,32...
#define readings bit_size*3
#separate
int16 get_stable_analog_reading(int8 ach) {
   int16 a,b[bit_size];
   int8  c[bit_size];
   int8 i,n;
   set_adc_channel(ach);
   delay_us(5);
   for(i=0; i<bit_size; i++) c[i]=0;
   for(i=0; i<readings; i++) {
      a=read_adc();
      b[a & (bit_size-1)]=a;
      c[a & (bit_size-1)]++;
   }
   n=0;
   for(i=1; i<bit_size; i++) {
      if(c[i]>=c[n]) n=i;
   }
   return b[n];
}


Hope it can help someone until we have clear adc errata pages for these micros Cool
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Dec 28, 2006 3:03 pm     Reply with quote

1. Make a very simple A/D test program. Get rid of all that array stuff.
See the short program in my post near the end of this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=28887

2. Increase the delay after you change the channel to 20 us.

3. What is the output impedance of the device or circuit that's driving
the A/D pin ? The 18F8722 data sheet says it must be 2.5K max.

4. Add a .01 uF capacitor between the A/D pin on the PIC and ground.
Put the cap very close to the PIC pin.
Assen



Joined: 18 Oct 2006
Posts: 8

View user's profile Send private message

PostPosted: Fri Dec 29, 2006 7:45 am     Reply with quote

It's not a problem of aditional capacitors, low resistors or time to change the channel. This is an internal noise problem of the chip. I bought the original demo board from Microchip and it is the same - when you put the potentiometer to any position and leave it like that for minutes you can get floating result with +/- 2-3 digits, never less than +/-2 even with 10nF+100nF+1uF close to the pin. I changed the software with no change of the adc channel - the same result. Errata for these micros is clear enough - internal noise, +/-1.5 digits is the error when using Vref pins and up to 3 digits for the other pins.
Ttelmah
Guest







PostPosted: Fri Dec 29, 2006 10:21 am     Reply with quote

This is not what the erratum says.
The fault in the errata, is that the offset error is out of spec, _not_ a noise problem. Your system is showing a noise problem, and it sounds as if there may also be a long term drift in the reference.
Most of the PICs only give their noise floor ADC bhaviour, if sleep mode is selected for the ADC reading. However your drift, sounds as if your Vref, may not be adequately stable, or smooth.

Best Wishes
w2drz



Joined: 27 Dec 2006
Posts: 55
Location: Western New York - USA

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

PostPosted: Sat Dec 30, 2006 10:13 pm     Reply with quote

Hi,

Try 2 things in your code.

1. replace the value (5) you have and replace it with a (25) us in the:
set_adc_channel(ach);
delay_us(5);
This settling time can be reduced in -5- us steps at a time to suit your results.
In the read loop add a: delay_us(6); delay before each read time.
this can be adjusted up or down, I find the us is fine.

I find this helps or cures problem in some chips I have used.

tom
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