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

16F1459 3v3 ADC

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



Joined: 02 Feb 2010
Posts: 363

View user's profile Send private message

16F1459 3v3 ADC
PostPosted: Fri Oct 20, 2017 2:05 pm     Reply with quote

Hello All,

I want to ask you some questions for my current project with PIC16F1459.

Some info: pic16f1459 at 4MHz INTRC_IO mode, Vcc = 3.3 volts with LP2985.

Is it possible to use 10bit-ADC for measure battery (6V or 12V) voltage(How to setup ADC, can I use internal Vref)? I have 1% 30k, 10k divider.

Thanks for your attention!
Ttelmah



Joined: 11 Mar 2010
Posts: 19569

View user's profile Send private message

PostPosted: Fri Oct 20, 2017 2:20 pm     Reply with quote

No, and yes.

The ADC can only itself work with a voltage between 0v, and it's Vref. It can't handle a voltage above this.

You just have to use a bigger potential divider to reduce the voltage to be in this range.

Now with 30K/10K, you have 1/4 the input voltage, so 3v max. This implies you need to be using a Vref of at least 3v. So 'no' you can't use the internal Vref. The maximum Vref that can be produced by the internal reference with your supply is 2.048V (your ADC is rated to operate from this, many are not). You need to alter your division so your maximum voltage is below this. So 30K/6K for example would be good (giving 2v for the maximum input voltage).
kmp84



Joined: 02 Feb 2010
Posts: 363

View user's profile Send private message

PostPosted: Fri Oct 20, 2017 2:45 pm     Reply with quote

Hello Mr."Ttelmah",

I forgot to ask second option: Vss_Vcc as ref. voltage because LP2985 is LDO and 30k/10k divider?

Thanks,
Ttelmah



Joined: 11 Mar 2010
Posts: 19569

View user's profile Send private message

PostPosted: Fri Oct 20, 2017 2:59 pm     Reply with quote

Yes, provided you are confident your supply really is smooth during the actual sampling. You will find a separate Vref is always better, but the internal one is not that accurate (1%), and provided there is not too much noise in your circuit the supply can be used.
temtronic



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

View user's profile Send private message

PostPosted: Fri Oct 20, 2017 6:19 pm     Reply with quote

Also ANY change in the PICs power usage will affect the ADC reading when using VDD as the Vref.
One example... your code turns on a LED just before you read the ADC. Vdd will 'dip' or 'drop' slightly while the LED turns on, lowering Vref and the ADC result will be lower than you think it should be.
How 'deep' the drop will be, is generally based upon the current demand. The greater the demand, say 3 LEDs, the greater the drop. This can be minimized by large value caps on the Vdd rails but all regulators, even LDO ones need time to 'recover'.

Just something to remember when you see ADC results lower or 'off' from what you expected.

Jay
guy



Joined: 21 Oct 2005
Posts: 297

View user's profile Send private message Visit poster's website

PostPosted: Wed Oct 25, 2017 7:47 am     Reply with quote

Hi kmp84,
in my opinion the design is good if you are ok with the accuracy of the 3.3V power supply as reference.
Here are some more tips:
Use wide traces or wires for VCC,VSS.
Use slightly smaller resistors but with the same ratio. 30K ohm is more than the recommended max. impedance. Try 10K+3.3K 1% (almost the same ratio).
You can add a capacitor near the PIC analog input to reduce signal noise.
Use software oversampling and average - this is always a good idea.
Ttelmah



Joined: 11 Mar 2010
Posts: 19569

View user's profile Send private message

PostPosted: Wed Oct 25, 2017 1:35 pm     Reply with quote

His ADC allows a 10K input impedance. He is already below this (hint the impedance is not the value of the top dropper resistor).
guy



Joined: 21 Oct 2005
Posts: 297

View user's profile Send private message Visit poster's website

PostPosted: Wed Oct 25, 2017 11:36 pm     Reply with quote

Ttelmah please explain how to calculate the impedance in such case?
Ttelmah



Joined: 11 Mar 2010
Posts: 19569

View user's profile Send private message

PostPosted: Thu Oct 26, 2017 1:29 am     Reply with quote

Thevenin....

However 'think about it'. There is a 10KR resistor from the input to ground. You therefore know that the equivalent resistance at this point (which is the DC equivalent of 'impedance') _must_ be less than 10K....

Look at:
<https://electronics.stackexchange.com/questions/243277/output-impedance-of-voltage-divider>
guy



Joined: 21 Oct 2005
Posts: 297

View user's profile Send private message Visit poster's website

PostPosted: Thu Oct 26, 2017 3:17 am     Reply with quote

Thanks. I'm not arguing of course but my intuition says that if there is an ADC charge capacitor inside the PIC, the R-C network (and charge time) is defined by the series resistance between the signal source and PIC. This may not be 'impedance' but may be applicable when choosing the voltage divider. Am I right?
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Thu Oct 26, 2017 4:44 am     Reply with quote

guy wrote:
...if there is an ADC charge capacitor inside the PIC, the R-C network (and charge time) is defined by the series resistance between the signal source and PIC. This may not be 'impedance' but may be applicable when choosing the voltage divider. Am I right?


Yes. There is a little bit more to it than that. Microchip's PIC datasheets give a lot of details on this, and all those that I've seen have a model for the ADC input and a fully worked example of charging of the hold capacitor, aka the acquisition time, i.e. how long you should wait between selecting an ADC channel and doing a conversion. This time varies somewhat from PIC to PIC, but is generally in the region of 5-15us on 16s and 18s (24s/33s/dsPICs have much faster ADCs and considerably lower Taq times) For the poster's PIC16F1459, in the datasheet section 16.4 deals with all this. Figure 16.4 show the input equivalent circuit/model and Equation 16-1 is the worked example for Taq, resulting in 8.1us for this PIC family.

Where bandwidth permits, as well as reducing the effect of noise, having a capacitor on the analogue signal can significantly reduce Taq for that channel as the impedance of the capacitor is generally much lower than that of the resistor network, or put another way, the external capacitor can easily and quickly dump current into the ADC input.

The impedance of the ADC driving CCT is recommended to be 10k maximum mainly as otherwise leakage inside the PIC reduces accuracy, as well as increasing Taq.

Note too that for this PIC family Taq for its internal temperature sensor is quoted as 200us, much longer than for external voltage sources.

Going back to the original question: measuring battery voltage deosn't generally need high accuracy. Using Vdd as the reference is not great, but for battery voltages is generally okay. The 30k/10k resistor chain is just about okay for impedance, provided the code waits for at least 8.1us (I'd use 10us) after selecting the input, or between conversions if only using one channel. The 1% resistors mean that even if Vdd was perfectly and noiselessly 5V the result could be well over 1% out. This could, in theory be calibrated out, as its a systematic, i.e. repeatable error, but it's probably pointless. With 1% resistors, 7 bit conversions would be fine, if the ADC did that, in practice the error would be less than one LSB. So using either 8 or 10 bit conversions would be fine. Just don't expect 8 or 10 bit accuracy - it's simply not going to happen.

With many modern battry types battery voltage is a very poor indicator of battry charge state. Li-ion batteries, for example, have a practically constant voltage for over 90% of their charge range, only dropping when they are in the last few percent of their range: by the time the voltage dips noticably it's too late: the battery is essentially flat. For these battery types charge/coulomb counting is the way to go (i.e. integrating charge/discharge current).

Another point to remember is that any voltage divider to measure battery voltage will drain the battery. 30k + 10K will draw 300uA continuously from a 12V battery.
kmp84



Joined: 02 Feb 2010
Posts: 363

View user's profile Send private message

PostPosted: Thu Nov 16, 2017 4:25 pm     Reply with quote

Hello All,

I have faced the problems with draining battery through voltage divider and flat voltage discharging characteristics of Li-Ion batteries :( . If I change resistors with higher (for example 300k/100k) how to resolve the problem with input ADC impedance? About "charge/coulomb counting" is not easy in my case. (I'm sending data with radio modem with time interval one min. or port-pin change.)

Any suggestion will be helpful for me!

Тhanks in Аdvance!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 16, 2017 6:08 pm     Reply with quote

This circuit was used to read the battery voltage on a board that ran off
a 9v battery. The voltage divider is only turned on for a short time to
measure the battery voltage. So, there is no continuous large drain on
the battery due to the voltage divider. Also, smaller resistors can be used
for the voltage divider, giving the A/D input the low impedance that it needs.
Code:
                    +9vBat
                       |
                       |
                       /   +9vBat   U1
                       \      |   _______
                  3.9K /      | 1|       |8
                       \      |--|S1  D1A|----|
                       /         |       |    |
                       |        2|       |7   |
                    Q1 |---------|G1  D1B|----|
                      /          |_______|    |
PIC       3.9K    B |/ C          IRF7306     /       
i/o  >---/\/\/\-----|             P-Chan.     \
pin                 |\ E          FET         / 3.9K
              2N3904  \           SOIC-8      \
"1" on Base            |                      /
turns Q1 on            |                      |      Battery level
and puts GND         -----                    |----> To PIC A/D pin
on U1 gate.           ---     +9v on gate     |
This turns             -      turns FET off   /
U1 on,                                        \
                              GND on gate     / 2.2K
                              turns FED on    \
                                              /
                                              |
                                              |
                                            -----
                                             ---
                                              -

More info:
http://www.ccsinfo.com/forum/viewtopic.php?t=23186&start=4
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 16, 2017 6:17 pm     Reply with quote

Alternate (better) method:
How to use the internal FVR and A/D to measure the Vdd voltage (with code):
http://www.ccsinfo.com/forum/viewtopic.php?t=54993
kmp84



Joined: 02 Feb 2010
Posts: 363

View user's profile Send private message

PostPosted: Fri Nov 17, 2017 7:58 am     Reply with quote

Hello,

Thanks for your advice mr. PCM programmer! I'll use it.
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