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

Measuring Vdd with PIC12F1822

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



Joined: 30 Jul 2007
Posts: 112
Location: Moscow, Russia

View user's profile Send private message

Measuring Vdd with PIC12F1822
PostPosted: Thu Mar 24, 2016 7:28 am     Reply with quote

I can't understand, how to configure PIC12F1822 for measuring own Vdd with FVR? It is possible only with pin RA1? Where this pin must be connected?
Ttelmah



Joined: 11 Mar 2010
Posts: 19516

View user's profile Send private message

PostPosted: Thu Mar 24, 2016 8:09 am     Reply with quote

You do it 'backwards'.
You don't use a pin at all.

You set the ADC to use Vss and Vdd as it's references, and then read the FVR, with the ADC.

So, setup the ADC with VSS_VDD, Then set the adc_channel to 'FVR_CHANNEL'. Then do an ADC reading.

Now if you program the FVR to 2.048v, then if you get a reading of (say) 635, then you know Vdd is 1024/635 * 2.048 = 3.3v.

Generally, if you are using this to check battery status, then don't bother with actually calculating a value (uses a lot of space), instead set just 'limit numbers' for (say) 20%, 50% etc.. battery levels. So if perhaps you were taking 3v as 10%, you can calculate in advance that the ADC reading for this would be 699. With some knowledge of your battery, you can have just a table of 10%, 20% etc., values, and have almost no maths involved.
40inD



Joined: 30 Jul 2007
Posts: 112
Location: Moscow, Russia

View user's profile Send private message

PostPosted: Thu Mar 24, 2016 8:31 am     Reply with quote

Thanks.
So, RA1 is free for use?
Ttelmah



Joined: 11 Mar 2010
Posts: 19516

View user's profile Send private message

PostPosted: Thu Mar 24, 2016 8:54 am     Reply with quote

Yes.

RA1, is not involved at all. What it is used for on the ADC, is if you want to use an external voltage reference to feed the ADC, this becomes the input pin for this reference.

Look at Fig 16-1 in the data sheet. You will see that the ADC can have as it's Vref, Vdd, The FVR, or AN1. Using Vdd, this pin is not used.

The same diagram also shows how you have the four 'normal' ADC channel inputs, or the options of reading from the FVR, DAC, or temp indicator.

The other way you could read the voltage would be to use the FVR to feed the ADC, and read the voltage using any ADC pin _off a resistive divider_. However the problem with this is that the divider would then draw power.
The whole point of the 'reverse' approach, is it removes the need for such a divider, and removes the need for a pin.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 24, 2016 10:08 am     Reply with quote

Here is an example of using the FVR to determine the Vdd voltage.
This was used with a 16LF1847 running from a lithium battery at 3.6v initially.
Code:

// This routine returns the Vdd voltage.
// The formula is:

// Vdd = (1.024 * 1023) / adc_result;
// The first part can be pre-calculated as 1048.
// For an adc result of 316, this would give Vdd = 3.30 volts.
// We don't want to return a float, so we return
// the floating point result * 100, and convert to int16.
// So we will return 330.

// Experiments show this routine to return a value that
// is slightly low.  This routine will return 358 or 359
// for an actual Vdd voltage of 3.60v on the voltmeter. 
// But then again, maybe the B&K meter is reading
// slightly high ?

//----------------------------
// This version of the routine requires #device ADC=10
// just below the #include line for the PIC in the main file.

#define NUMERATOR (int32)(1.024 * 1023 * 100) // = 104755

int16 read_PIC_Vdd_voltage(void)
{
int16 adc_result;
int16 retval;

setup_vref(VREF_ON | VREF_ADC_1v024);
delay_ms(100);  // How long is this delay ?

setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_16);  // For 8 MHz PIC osc frequency
set_adc_channel(FVR_CHANNEL);
delay_us(10);   // 5 us minimum
 
adc_result = read_adc();

retval = (NUMERATOR + (adc_result / 2)) / adc_result;

setup_vref(VREF_OFF);
setup_adc(ADC_OFF);

return(retval);
}

 
asmboy



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

View user's profile Send private message AIM Address

PostPosted: Thu Mar 24, 2016 11:47 am     Reply with quote

About a year ago i finished up a design for a two channel precision biologic incubator control with 6 LM34DH sensors selected by an 8 channel DG408 analog mux, with low offset DC amplifiers and a PIC 18f2423 / 12bit a/d. I ended up using a stiff - low dropout regulator for the pic at 5.0v +/- .2v - and use it as the A/D reference too. Calibration was done by reading an LM4040CIM3-3.0 on an uncommitted mux input- and then correcting the LM34 data - based on what the pic SAY's the true 3.0V reads. works perfectly. Method does a constant check of tracking adjustment - based on the LM4040 -and sure beats using cal pots ;-))

Last edited by asmboy on Thu Mar 24, 2016 7:05 pm; edited 2 times in total
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Mar 24, 2016 3:24 pm     Reply with quote

One thing to remember is that battery voltage is NOT a true, good indicator of battery capacity or 'life'. You should always do a 'load test', validate results, consult battery datasheets, etc. based upon true loading of the battery. It's a mix of science and 'balck magic' to 'dial in the numbers'.


Jay
asmallri



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

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

PostPosted: Thu Mar 24, 2016 5:55 pm     Reply with quote

asmboy wrote:
About a year ago i finished up a two channel design for a two channel precision biologic incubator control with 6 LM34DH sensors selected by an 8 channel DG408 analog mux, with low offset DC amplifiers and a PIC 18f2423 / 12bit a/d. I ended up using a stiff - low dropout regulator for the pic at 5.0v +/- .2v - and use it as the A/D reference too. Calibration was done by reading an LM4040CIM3-3.0 on an uncommitted mux input- and then correcting the LM34 data - based on what the pic SAY's the true 3.0V reads. work. constant check of tracking adjustment - based on the LM4040 beats using cal pots ;-))


I am missing something, why didn't you just feed the LM4040 into the reference input of the A/D?
_________________
Regards, Andrew

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



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

View user's profile Send private message AIM Address

PostPosted: Thu Mar 24, 2016 7:28 pm     Reply with quote

i started the project just as you say. 3v ref textbook input. amplifying the sensors dc output. But noise performance needed improvement, A LOT-- for an incubator that had to meet a long term stable spec of 37degC +/- .1deg
Not a school kid project.

i ditched the client supplied lm35 ( centigrade out deg =10mv ) sensors for lm34 which has 10 mv per deg F - and set the system display to present in centigrade. More millivolts per degree C ........

i needed the max resolution on the reading with my 12bits - but not allocated as you think - in the delivery version: the same 3v ref was used as an offset to a gain amp with neg value clipper such that only 200mv of the temp sensor delta - based at .85v - were offset at an amplified value across a span of 0 to 5 v on the pic input.

i made the controller pure click-bang for any temp below 85F or above 115F
( solid ON or SOLID OFF) the window that was left was perfect for my PID tuning in the servo mode.

the low offset low noise op-amp circuit converts a span of .85v -1.15 volts into a 0-5v signal where count0=85deg F and count4095=115deg F

using more analog gain and switching to the extant 5V PSU and the method i described led to the lowest system noise and best accuracy when compared to best practices absolute calibration validation. The reference is read along with each data point for both display and servo input -- and continuous correction is done - essentially on every reading set. in the end - one unexpected result was that the LM117 powering the pic was damned stable under load!!
Based on experiment - this combination provided better noise performance than trying to resolve 200 mv on top of 850 mv over a 3 volt span.
thus the most stable ( low jitter in lock ) PID servo -

noise can be as important as RMS accuracy - this was project that needed both.


Last edited by asmboy on Fri Mar 25, 2016 9:21 am; edited 1 time in total
40inD



Joined: 30 Jul 2007
Posts: 112
Location: Moscow, Russia

View user's profile Send private message

PostPosted: Fri Mar 25, 2016 12:13 am     Reply with quote

temtronic wrote:
One thing to remember is that battery voltage is NOT a true, good indicator of battery capacity or 'life'. You should always do a 'load test', validate results, consult battery datasheets, etc. based upon true loading of the battery. It's a mix of science and 'balck magic' to 'dial in the numbers'.
Jay

In my case PIC just need to send value readed while full scheme working to host controller. Further processing - is it's headache.
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