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

AN0 and AN2 ADC different behavior on a PIC16F876A

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



Joined: 08 Sep 2006
Posts: 182

View user's profile Send private message Send e-mail

AN0 and AN2 ADC different behavior on a PIC16F876A
PostPosted: Mon Nov 06, 2006 4:04 am     Reply with quote

Oke I have some strange behavior on the ADC I am using...
I use the AN0 input AND the AN2 input.....
I am measuring a small voltage at this moment... around 1V...
But when I connect mine sensor at AN0 I measure the 1V... and read the correct ADC value.
When I do the same at AN2 I measure 2V.....?? And that's the ADC value the PIC returns.... So what is the difference between these ADC's???

Include the code....
Code:

#include "C:\MentorProjects\Warme_Doos_LCD\Software_2_CCS\WarmeDoos.h"
#include "C:\MentorProjects\Warme_Doos_LCD\Software_2_CCS\flex_lcd.c"
#include <math.h>
void main()
{
   float adc_waarde;
   float voltage;
   float voltagemV;
   setup_adc_ports(ALL_ANALOG );
   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(VREF_LOW|-2);
   
   lcd_init();
   // Clear the LCD.
   printf(lcd_putc, "\f");
   delay_ms(500);

while(TRUE)
      {
      int i;
      for(i=0;i<5;i++)
      {
      set_adc_channel(i);
      delay_ms(100);
      adc_waarde = read_adc();
      printf(lcd_putc,"Tmeas(%d)mV=",i);
      Delay_Ms(500);   
      }
   i =0;
   }
}

pmarie
Guest







PostPosted: Mon Nov 06, 2006 4:59 am     Reply with quote

HI,
I think that the error is here:
Code:
      adc_waarde = read_adc();
      printf(lcd_putc,"Tmeas(%d)mV=",
Rolling Eyes i);

you need to do this....
Code:
      adc_waarde = read_adc();
      printf(lcd_putc,"Tmeas(%d)mV=",adc_waarde);

regards
PhilippeM[/code]
Jody



Joined: 08 Sep 2006
Posts: 182

View user's profile Send private message Send e-mail

PostPosted: Mon Nov 06, 2006 5:25 am     Reply with quote

Hi,

Sorry I edited it wrong...

Code:

  adc_waarde = read_adc();
      printf(lcd_putc,"Tmeas(%d)mV=",adc_waarde);


That is exactly what I have.......

I use compiler version: V3.242...
Ttelmah
Guest







PostPosted: Mon Nov 06, 2006 3:43 pm     Reply with quote

First thing to understand, is that there is only one ADC in the PIC. There is a simple multiplexer, which allows this to be connected to one of a number of different pins, but the ADC itself is the same hardware on all the channels. Hence you need to be looking at various things.
First, the obvious one, checking that the signal voltage really is the same on each pin.
Then obviously looking at the code. Now you use a construction, that is dangerous (and not really supported) in CCS, of declaring a variable inside a function block (int i). This is permissable in normal 'C', but in CCS, to keep things simple, given there is no memory management as such, they elected to declare normal variables inside functions, by using the function 'name' as part of the variable name. When a variable is declared inside a block inside another function, they have no ability to handle this, so the variable gets treated as a global variable. In the code as shown, this should not cause a problem, but might do, if there is another 'i', declared in either of the two include files. It is a construction that I would avoid in CCS.
However the construction that leaps out at me, is the setup for the comparator Vref. First the obvious question, why are you touching this?. You are not using the comparator... However it is the value used for this, that is the 'killer'. You are taking the 'VREF_LOW' constant, and oring it with a -ve number. Coded in binary, this gives you a value of 0xFE, put into the comparator setup function. This value, will result in the Vref being output on pin A2, which is why this pin is behaving differently...
Values used for this function should only ever be +ve. 0 to 15.

Best Wishes
Jody



Joined: 08 Sep 2006
Posts: 182

View user's profile Send private message Send e-mail

PostPosted: Tue Nov 07, 2006 3:29 am     Reply with quote

YES that did it.....

I used the "automatic" generated fuses..... but still I has to read them!!!
Embarassed

Thanks for all the help!!

Jody
Guest








PostPosted: Mon Jun 11, 2007 2:49 pm     Reply with quote

I have a several questions regarding this topic.

when would you use the following in your code :

setup_VREF(VREF_HIGH | 6) //At VDD = 5V this gives you a voltage of 2.185V.

Can you please verify if I am correct:
Does this set the Reference Voltage of all of ADC in the PIC controller to 2.185V. So when an analog voltage source connected to , lets say channel 0 of the ADC, this voltage will be compared to the VREF of the Pin RA3. Depending on the desired resolution that is set by the programmer, it will output a 8 or 10 bit when this channel is Read.


If this is correct, then how will I know if the input voltage of channel 0 is 1.02 or 1.10 based on the resolution (8 or 10 bits).


Can you please clarify these few concepts, It has been a very long time since I have worked with ADC. thanks


--Srig
Ttelmah
Guest







PostPosted: Tue Jun 12, 2007 3:11 am     Reply with quote

You need to read the data sheet, and then look at some of the Microchip application notes.

The internal Vref, is for the _comparator_, not the ADC. It happens to use some of the same pins (which is why it was affecting the results for the previous poster), but is not connected to the ADC. To physically use this for the ADC, you would have to output the Vref value on pin A2, and then manually connect this (with a wire...), to the Vref input for the ADC.

However there is then a problem. The Vref output, is a simple resistor ladder, and as such only supports a relatively high impedance load. The Vef input for the ADC, requires a fairly low drive impedance, if the value isnot going to change during the operation. Hence to use the Vref like this, you really need to add an external buffer amplifier.

Your setup function for the Vref generator, will not output the Vref value, so it'll _only_ be used by the comparator, and nothing else. This is what the constant Vref_A2 controls.

Now, with 'Vref High' selected, the Vref voltage, is from 0.25Vcc, to 0.75Vcc, in steps of /32 of the voltage range. So, with '6' selected, you will get 6/32*5 + 1.25v = 2.1875v.

Best Wishes
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