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

Strange ADC behavior on PIC16F88

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



Joined: 17 Oct 2005
Posts: 98

View user's profile Send private message

Strange ADC behavior on PIC16F88
PostPosted: Wed Nov 02, 2005 6:59 am     Reply with quote

Hello forum,

I'm using the ADC on PIC16F88 and it works fine. But, I don't get the same reading every time even though the voltage on the ADC input is exactly the same.

This is my code:

.h file:
Code:
#include <16F88.h>
#device adc=10
#fuses NOWDT,INTRC_IO, NOPUT, NOMCLR, BROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG, NOPROTECT, NOFCMEN, NOIESO
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_B3,rcv=PIN_B5,bits=8,invert)


.c file:
Code:
#include "myfile.h"

void main()
{
   int16 i;
   setup_adc_ports(sAN0|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_8);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
     
   output_low(PIN_B3);
   delay_ms(2);

   while(1)
   {
      i = read_adc();
      printf("%lu", i);
      putc(13);
      putc(10);
      delay_ms(10000);
   }
}


The input of the ADC is connected to a resistor divider. The output from RS232 is like this:

886
877
886
877
886
877
... you get the point.

So, what the heck is going on? It's the same problem at different "update rates" (10000ms or 1000ms).

Hope you can help.

Regards,
Futterama
SherpaDoug



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

View user's profile Send private message

PostPosted: Wed Nov 02, 2005 7:22 am     Reply with quote

Try putting a cap, say 0.1 uF, from the A/D input to the A/D ground pin. Your resistor divider may have too high an impedance.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Futterama



Joined: 17 Oct 2005
Posts: 98

View user's profile Send private message

PostPosted: Wed Nov 02, 2005 7:48 am     Reply with quote

SherpaDoug wrote:
Try putting a cap, say 0.1 uF, from the A/D input to the A/D ground pin. Your resistor divider may have too high an impedance.

The capacitor doesn't help.

I've changed the resistor divider to a 1k pot, and that doesn't help either.

I've changed to another PIC16F88, that doesn't help, so the PIC itself is fine.

It's always the same numbers coming out, even when changing PIC device.

I'm lost. What good is the ADC if it's faulty?
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Wed Nov 02, 2005 8:47 am     Reply with quote

You forgot to select the channel you want converted.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
gs



Joined: 22 Aug 2005
Posts: 30
Location: Ioannina - Greece

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

PostPosted: Wed Nov 02, 2005 12:22 pm     Reply with quote

measure the value 10 times in 1.5 ms intervals, Sum the results and divide by 10. This will give you a more stable value.

I use the following code in one of my projects. I also use RC filter (1K, 100nF) at the analog inputs
Code:
int16 mesi_timi()
{
   int16 value,sum;
   int   i;
   sum=0;
         for (i=1; i<=10; i++)
      {
      value=Read_ADC();
      sum=sum+value;
      delay_us(1500);
      }
    value=sum/10;
   return value;
}

_________________
www.hlektronika.gr
Futterama



Joined: 17 Oct 2005
Posts: 98

View user's profile Send private message

PostPosted: Thu Nov 03, 2005 12:21 am     Reply with quote

asmallri wrote:
You forgot to select the channel you want converted.

That's right. But I guess since I use port 0 it doesn't matter, cause setting the port to 0 doesn't help.

gs wrote:
measure the value 10 times in 1.5 ms intervals, Sum the results and divide by 10. This will give you a more stable value.

I use the following code in one of my projects. I also use RC filter (1K, 100nF) at the analog inputs
Code:
int16 mesi_timi()
{
   int16 value,sum;
   int   i;
   sum=0;
         for (i=1; i<=10; i++)
      {
      value=Read_ADC();
      sum=sum+value;
      delay_us(1500);
      }
    value=sum/10;
   return value;
}

Well, yes, but I'll also get a very stable reading by only reading every second measurement (since they are the same).

As you can see, it can't be analog noise since the values are the same two repeating regardless of the convertion rate.
Futterama



Joined: 17 Oct 2005
Posts: 98

View user's profile Send private message

PostPosted: Thu Nov 03, 2005 12:49 am     Reply with quote

DOH! I found the reason for my error.

My supply IC can deliver 40mA. I had a LED blinking every time a conversion was sent (I cut this part out of my posted code). The LED caused the voltage to drop a bit every time it was on. That's why I got these strange readings! Rolling Eyes

Thank you all - gs, I'll use your posted code to get stable readings, though I'll probably read 100 times instead of 10 Wink

Regards,
Futterama
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 03, 2005 1:17 am     Reply with quote

Also, the capacitor does make a difference. Just now I was testing
your problem on a PicDem2-Plus, with a 16F88 and PCM vs. 3.235,
and I got this output:

Notice that it bounces around a little bit.
Quote:

888
883
889
888
884
887
884
883
883
887
884


Then I added a 0.1 uF (i.e., 100 nF) capacitor close to the RA0 pin
and the GND pin on the 16F88. Then I got this output:
Quote:

886
885
885
886
886
886
886
886

It's much more stable.
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