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

Analog to Digital Accuracy 16F819 10Bit.

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



Joined: 06 Sep 2003
Posts: 49

View user's profile Send private message

Analog to Digital Accuracy 16F819 10Bit.
PostPosted: Mon Feb 09, 2004 10:06 am     Reply with quote

I'm using a 16F819 for the first time, I usually use the 16F876. With the code below I'm taking 10 AtoD readings and computing the average. With RA0 connected to ground the AtoD value is 0x00 as expected. When RA0 is connected directly to Vdd (same trace as is powering the PIC) the reading is 0x03FC instead of 0x03FF as would be expected. It very consistently gives 0x03FC. Is there something wrong in the code? Any adeas why this is happening?

Code:

//test

#include <16f819.h>
#device *=16 ADC=10


#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

#zero_ram
#case

//define these as necessary for particular circuit
#define ToPC            PIN_B2 
#define FromPC            PIN_B3 
#define LEDStatus              PIN_B4   


// Function Prototypes
void testLEDS(void);

void main(void)
{
   int16 int16Temp, atodValue;
   int8 i;


    output_high(LEDStatus);  //off
   #use rs232(BAUD=19200, INVERT, XMIT=ToPC, RCV=FromPC)

   setup_port_a(RA0_ANALOG);
      setup_adc(ADC_CLOCK_INTERNAL);
   set_adc_channel(0);

   printf("Running...\r\n");

   while(1)
   {
      output_low(LEDStatus);     //on
        delay_ms(20);
      output_high(LEDStatus);    //off
        delay_ms(1000);

      printf("Running...\r\n");

      int16Temp = 0;
      for(i=0;i<10;i++)
      {
         atodValue = read_adc();
         int16Temp = int16Temp + atodValue;
         printf("atodValue = %LX\r\n", atodValue);
      }
      atodValue = 0;
      atodValue = int16Temp / (long)10;
      printf("atodValue AVG= %LX\r\n", atodValue);

    } //end while(1)

}  //end main

Ttelmah
Guest







PostPosted: Mon Feb 09, 2004 11:37 am     Reply with quote

I think in these circumstances, I'd look at the generated assembler. The result is what you would see on the chips with an 8bit converter, if you specify a 10bit result, where the compiler will 'left justify' the 8bit result. The suspicion is that the compiler for some reason is doing this. The device editor does correctly list the part as having a 10bit AD (there are a number of errors in some parts, which need the device editor to fix, or some bodging round).
Try a 'bodge', like:
Code:

#byte ADRESH=0x1E
#byte ADRESL=0x9E
#bit ADGO=0x1F.2

read_adc(ADC_START_ONLY);
while (ADGO) ;
result=make16(ADRESH,ADRESL);


If 'result', contains the right value, then the problem is a fault with the compiler not handling the 10bit ADC on the 81x correctly, and you should moad to CCS.

Best Wishes
Bill T
Guest







0x03FC instead of 0x03FF
PostPosted: Mon Feb 09, 2004 10:35 pm     Reply with quote

If I understand correctly, the result is off by 3 ADC counts, out of the 2^10 or so possible. Gimme a break! There is nothing wrong. The power for the PIC isn't perfectly clean to be sure, and isn't a good ADC reference. And ADCs aren't perfect either, especially at the extremes. Get a good clean reference, inject a signal from a function generator, and check it out.

The PIC part does lots of stuff besides ADC - and look at the price. I would imagine the ADC performance has been traded off for other goodies. That's why they sell ADCs, and that's why they can get very expensive.....
SteveS



Joined: 27 Oct 2003
Posts: 126

View user's profile Send private message

ADC accuracy
PostPosted: Tue Feb 10, 2004 7:41 am     Reply with quote

Actually the ADC in the PIC is very good. One of the electronics trade magazines did a test on the PIC 10 bit ADC a couple years ago and they were surprised at how well it performed. I tried to find the article just now but can't locate it.....

Anyway, besides the suggestions by others, read the reference manual section on the ADC. Timing, choice of clock, input impedance all play a role. The most accurate conversion technique uses the R/C ADC clock with the micro put to sleep during conversion.

I would also filter the ADC input; keeping in mind restrictions on input impedance.

- SteveS
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Tue Feb 10, 2004 8:37 am     Reply with quote

Its a new chip I think. I would not be supprised to find that the compiler is truncating the last two bits because the headder file has a bug.
Freddie



Joined: 06 Sep 2003
Posts: 49

View user's profile Send private message

Re: 0x03FC instead of 0x03FF
PostPosted: Tue Feb 10, 2004 3:33 pm     Reply with quote

I've tried several PICS and crystals. I'm going to send a note to CCS.
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