|
|
View previous topic :: View next topic |
Author |
Message |
jayanthd
Joined: 06 Dec 2012 Posts: 47 Location: Banned - pirate
|
How to setup Vref in PIC16F887 |
Posted: Fri Mar 22, 2013 12:06 am |
|
|
Hello
I searched the forum but couldn't find a working method to setup adc vref for PIC16F887.
My setup code is like this
Code: |
setup_adc_ports(sAN0|sAN1|sAN2|sAN3|sAN4|sAN5|sAN6|sAN7|sAN8|sAN9|sAN10|sAN11|sAN12|sAN13 | VREF_VREF);
setup_adc(ADC_CLOCK_DIV_2);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not //supported by the PICWizard
|
PIN AN2 is Vref- and AN3 is Vref+ I want to set Vref- as 0V and Vref+ as 2V |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Fri Mar 22, 2013 2:03 am |
|
|
So, you have to connect 0v to pin AN2, and 2V to pin AN3.
The chip can't do this for you. Bits of wire, and using a source _other_ than the internal Vref.....
The internal 'Vref', is _not_ designed to feed the ADC. It does not have a low enough output impedance to drive the ADC, and is not even remotely accurate anyway. It is a simple resistor divider 'tree' designed to give rough voltages to the comparator. Some of the later chips do have a bandgap voltage reference, and on these there is an option to route them to the ADC. On the 887, no. If you look, the Vref module, is specifically called in the data sheet 'CVref' - for _comparator_ voltage reference.
As a comment, also look at element AD06 in the data sheet. All PIC ADC's have a _minimum_ Vref they will give their specified accuracy with (AD06A), but the 887, is unusual in actually specifying the minimum it can use. 2v is below this....
Best Wishes |
|
|
jayanthd
Joined: 06 Dec 2012 Posts: 47 Location: Banned - pirate
|
|
Posted: Fri Mar 22, 2013 3:19 am |
|
|
@Ttelmah
I have done PIC16F887 based ADC projects in Hi Tech C, C18, XC, and mikroC compilers using Vref- of 0V and Vref+ of 2V. I need to give Vref+ of 2V because LM35 max output for 150 deg C is 1.5V. So, I have to give some 1.8V or 2V as Vref+ so that for 2V I get raw adc value of 1023 and for 1.5V some 767.25.
My projects work fine but I just don't know how to set vref+ and Vref- in CCS C code. I need just the adc setup code for that. I have connected An2 to ground and AN3 gets 2V from a voltage divider which consists of 780 ohms and 520 ohms resistors.
Why this code is giving error?
Code: | setup_adc_ports(ALL_ANALOG, AN3_VREF); |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Sat Mar 23, 2013 9:44 am |
|
|
Because the syntax is wrong...
If You actually want to use the Vss pin as Vref minus, then the syntax is:
Code: |
setup_adc_ports(sAN0|sAN1|sAN2|sAN3|sAN4|sAN5|sAN6|sAN7|sAN8|sAN9|sAN10|sAN11|sAN12|sAN13 | VSS_VREF);
|
However, seriously, have you ever sat down and run something like a high quality data logger on the analog input, and plotted it's results against the values you read?.
You will be lucky to actually get 8bit accuracy/linearity with 2v.
On many chips the loss below the specified 'minimum' for 10bit accuracy is pretty linear, and at (say) 3/4 of this voltage you will still get something like 9.5 bits of accuracy, but on this chip is goes terrifyingly non linear if you go much below the rating....
As a separate comment, you do understand you should only ever enable the ADC channels you actually are going to use. Every input selected to the multiplexer potentially introduces a little more noise to the signal that is being sampled. Floating inputs set to the multiplexer also destroy accuracy.
Best Wishes |
|
|
|
|
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
|