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

ADC conversion problem with PIC16F876

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








ADC conversion problem with PIC16F876
PostPosted: Thu Mar 26, 2009 6:59 am     Reply with quote

Hello.
I have a little problem with the ADC of a PIC16F876. The voltage measured at the RA0 is not the same with the voltage converted via ADC. Here is the program:

Code:

#include <16F876.h>
#device ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <flex_lcd.c>

unsigned long value;
float result;


void main() {

   setup_port_a( ALL_ANALOG );
   setup_adc( ADC_CLOCK_INTERNAL );
   set_adc_channel( 0 );
   delay_ms(10);
   value = 0;
   
   lcd_init();
   printf(lcd_putc,"\f");
   printf(lcd_putc," Voltmetru HAHA   ");
   printf(lcd_putc,"\n -------------  ");
   printf(lcd_putc,"\n                ");
   printf(lcd_putc,"\n -------------  ");

   do {
      value = Read_ADC();
      result = (float)value*0.00489;
      lcd_gotoxy(2,3);
      printf(lcd_putc,"%4Lu -- %1.2fV   ",value,result);
      delay_ms(10);

   } while (TRUE);
}


Here are a few values measured with a V-meter and the values printed on the LCD:
V-meter - value - voltage on LCD
1) 5.00 - 1023 - 5.00
2) 3.43 - 819 - 4.00
3) 2.14 - 512 - 2.50
4) 0.86 - 204 - 1.00

The input voltage is set using a variable resistor (pot), with the middle pin connected to RA0.
Any ideea what can cause those differences?

P.S: the value 0.00489 is actually (5/1023). the CCSC compiler version is 4.032.

Thank you
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Thu Mar 26, 2009 7:45 am     Reply with quote

1- you never said what the 5V supply value is for the pic - and in the mode you are using it - THAT is your Vref - if it is off - so are the readings

2- why not post what you READ vs what you present to the adc ports ?
there are errors and then there are ! ERRORS !!!

the lack of agreement may be perfectly understandable once the magnitude of the difference and error value of the 5V on your PIC are both taken in to proper account
lsimaster



Joined: 19 Mar 2009
Posts: 25

View user's profile Send private message

check your clock
PostPosted: Thu Mar 26, 2009 8:13 am     Reply with quote

I think your problem has to do with the A/D clock you have selected. The Microchip data sheet has very specific guidelines on clock frequency and I don't think your 20MHz master clock and the Internal Clock are compatible for the A/D. I'm not familiar with this part but every other chip I have worked with is like this. The A/D uses switched capacitors for the successive approximation and if the clock is wrong these will have leakage errors.

You will probably have to set a divide ratio on the system (master) clock.

Also, the pot value can change the reading since your sample time is not set. Generally you should set the analog channel, program a wait time as specified in the data sheet, then start the conversion. In your case I don't see this happening so the sample time is probably too short and the value never gets fully transferred to the internal sample capacitor.
Guest








PostPosted: Thu Mar 26, 2009 9:07 am     Reply with quote

Well, regarding the A/D clock, i tried other setups but still the same (tried with div_8 and div_32) . I also added a delay after reading and still the same. Seems that the reference voltage is not 5V but 4.31V.
I tried to set RA3 as reference by connecting to the 5V supply. Still, the difference between what ADC says and what V-meter says is about 0.2V.
I managed to get the same values only if the supply voltage is about 5.2V but i dont know if this is the right solution since the real reference voltage is 5.2 now and I'm considering it to be 5V (in the formula). Still looking for the proper solution...
lsimaster



Joined: 19 Mar 2009
Posts: 25

View user's profile Send private message

proper solution
PostPosted: Thu Mar 26, 2009 9:23 am     Reply with quote

The proper solution is:
1. Make your signal ratiometric with the reference voltage. You can set the reference to be the device power or analog 3. Ratiometric means that the signal value varies with the reference voltage. A pot with the ref voltage at one end and common at the other is ratiometric.
2. Get the A/D clock frequency right.
3. The sample delay has to occur between selection of the channel and start of conversion. I don't know how the CCS functions handle this because I never use them. It is best to do this directly in the register so you are sure what is happening.
Ttelmah
Guest







PostPosted: Thu Mar 26, 2009 11:11 am     Reply with quote

The chances are that the problem is the impedance of your source.
Electrically, the ADC inside the PIC, is seen as a capacitor, in series with a significant resistance inside the PIC. When you select the ADC channel, this is attached to the PIN, and it takes time for the capacitor to charge to the 'real' voltage, through the internal resistance. The capacitor is disconnected during the reading. This is why there is a specification in the data sheet for maximum impedance.
The nature of the ADC, also means that it will tend to integrate whatever signal is on the pin, and this can give different reading from those seen on an DVM.
You are adding a slight further error, in using /1023, for your multiplier. The closest approximation, is /1024, plus 0.5*(Vref/1024). The 1023 value is actually reached slightly _before_ Vref is reached.
You need also to ensure very good smoothing on the supply, if this is being used as Vref. Otherwise noise on this _will_ introduce odd readings.
Remember also the PIC will be performing it's readings redlative to it's Vss (unless you select an external Vrefl), and noise between this pin, and the ground used by your analog source, also needs to be well controlled (ground plane, rather than bits of wire...).
Seriously, the ADC on the PIC, if given a good Vref, and a low impedance input, and driven carefully in the software, can give excellent results.

Best Wishes
Guest








PostPosted: Fri Mar 27, 2009 8:35 am     Reply with quote

Thank you all for your support. I finally solved the problem. It wasn't what I (and you) expected. Few days ago I've made some modifications on the circuit and I added a diode in series with the PIC. The measurements from the previous posts (with the V-meter) were before the diode... and the voltage drop on the diode is about 0.7V. Between Vdd and Vss were only 4.31V. Now it's ok... rised the input voltage to ~5.8 (5.0V on Vdd).
Anyway, now I understand how ADC works because I tested all possible options Smile)
Thanks again for you answers.
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