MCP3202 + math question
Matthew Jones
Posts: 14
I have been working with 3208 for some time and found rather easy to work with. But for this project it wasn't needed. So I switched to the 3202. I had some issues but most were resolved. I used a bit of code from Chip Gracey 3202x that I found in one of the posts I read through. I may go back and go over a few other objects from the object exchange when I get the time.
My question regarding the output of the ADC itself.
I have it set up to run 3.3 volt(Vref) and read 2 pins ranging from 34 volt to 80 volt via a voltage divider of r1@560k r2@20k. The data sheet say to come to the Digital output code (DOC) the chip does the following.
DOC = (4096*Vin) / 3.3(Vref)
And what I need is the "Vin" portion. So I do the following
(DOC * 3.3) / 4096 * 28(Voltage divider)
But my number are coming a couple of volts low according to my scope and Fluke.
I can adjust in the code based on this if that what I am stuck with but I would rather not although the ratios are right I cannot find a valid method for adjusting them. Has anyone had any issues like this? I would grateful for any ideas on the matter.
Thanks
Matt
My question regarding the output of the ADC itself.
I have it set up to run 3.3 volt(Vref) and read 2 pins ranging from 34 volt to 80 volt via a voltage divider of r1@560k r2@20k. The data sheet say to come to the Digital output code (DOC) the chip does the following.
DOC = (4096*Vin) / 3.3(Vref)
And what I need is the "Vin" portion. So I do the following
(DOC * 3.3) / 4096 * 28(Voltage divider)
But my number are coming a couple of volts low according to my scope and Fluke.
I can adjust in the code based on this if that what I am stuck with but I would rather not although the ratios are right I cannot find a valid method for adjusting them. Has anyone had any issues like this? I would grateful for any ideas on the matter.
Thanks
Matt
Comments
I was wondering about current allowed on the input pins of the 3202. I have 137+- uA. Might be too small but I cannot find a spec in the data sheet. Any Ideas?
MCP3202
Thanks
Matt
That will give you the input voltage in mV.
-Phil
I think I might be able to come up with a percentage as suggested above that will fix it.
The biggest thing, that I am still wondering is the current levels that should be used. I was trying to figure out the time to fill the internal capacitors according to the time to read the ADC and see if I have enough current.
I am trying real hard to micro manage every ounce of power in the system thats why I went high on my resistors. I wish I could find a value. Any case examples of a currently used setup would help.
Thanks
Matt
-Phil
Thanks Phil
Matt
A 560K resistor charging a 20 pF capacitor has an RC time constant of 11.2 uSec. Based on that the adc clock rate should be less than 20 KHz to allow the sample capacitor to charge. What is the clock rate?
Thats what I am looking for Is the RC time constant?
Matt
Alas that's not relevant here as the voltage divider that's charging the capacitor has an output impedance of 19.3k and the time constant for 20pF will be 0.39us. Output impedance of a voltage divider is R1 || R2. However the you will lose some accuracy with an input resistance of 19.3k, according to the datasheet you would be better with more like 5k input impedance, suggesting a voltage divider of 5k6 and 150k might be slightly better (although at expense of higher quiescent power drain)
True, the output impedance is 19.3K, however the capacitor has to be charged through the 560K resistor from the applied input voltage. Having a 20K resistor in parallel with the capacitor makes the charging time longer not shorter since some of the current that could have gone to charging the capacitor goes through the resistor instead.
If you put a small capacitor in parallel with the 20K resistor it could speed up the charging of the mcp sample capacitor at the cost of reducing the speed at which the adc responds to changes in the input voltage.
-Phil
I'll try to lower the resistance until I get the number up. Its a trick some of this digital stuff. Its OK you get a number outright but when you gotta "Best guess" a few things it kind tough, at least for rookie like me.
Matt
The input voltage is 80V
The sample capacitor is totally discharged so has 0V on it
The sample switch closes and the capacitor starts to charge
At this point there is 80V across the 560K resistor so the current will be 80/560000 or 142.857 uA.
Once the capacitor voltage starts to increase so does the voltage across the 20K resistor, and it will shunt some of the current to ground rather than having it go to charging the capacitor.
Once the capacitor charges to 3.3V the current through the 560K and 20K will have dropped to 80/580000 or 137.93 uA.
Sorry Matt, I meant the clock frequency that is going to the mcp3202 not the prop clock frequency. A simple method of determining if the error is caused by the time it takes to charge the sample capacitor in the mcp3202 would be to put a 0.1 uF or 0.01 uF capacitor across the 20K resistor and apply a dc voltage to the 560K input resistor. If that gives the correct reading the problem is caused by the sampling time issue. If not the problem is with the resistor values or calculation values.
Another simple check is to measure the input voltage and compare it to the measured output voltage going to the mcp pin. The ratio should be equal to the multiplier (ie * 28) in your calculation. Resistors do vary slightly from their nominal values.
I'll try them tests.
Thanks
Matt
I use "_clkmode = xtal1 + pll16x" and " _xinfreq = 5_000_000".
Thanks
Matt
I hate to disagree, but R1 || R2 really is correct for the impedance of a divider. Here's an example, using Spice. In this example, Node A is driven with a 5V square wave through 900-ohm resistor (i.e. 1K || 9K) to charge and discharge a 0.1uF capacitor. Node B is driven with a 50V square wave through a 9K - 1K divider to charge and discharge a 0.1uF capacitor. The two waveforms for Node A and Node B are identical, demonstrating the impedance equivalence of the divider and the parallel-valued series resistor. Here's the screenshot:
-Phil
I have added some nop instructions to the attached mcp object to see if slowing down the clock helps. Let me know if the reading is higher with this object compared to the original one.
It looks like the mcp3202 object outputs a clock pulse every 850 to 1225 nSec depending on how long the two rdlong instructions in the loop take. That would be a clock frequency between 1.176470 and 0.816326 Mhz.
According to the mcp3202 data sheet the input signal is sampled for a period equal to 1.5 clocks, which would be a period of time between 1.275 and 1.8375 uS. This is not enough time for the sampling capacitor to be fully charged.
The data sheet also states a throughput rate of 100Ksps at Vdd = Vref = 5.0V and 50Ksps at Vdd = Vref = 2.7V. I am assuming the throughput is somewhere between 50 and 100Ksps at 3.3V. I have not had time to work out the rest of the timing but I wonder if this driver may be pushing the mcp3202 faster than it can it can handle at 3.3V
The charge curves for the caps are still identical. QED
-Phil
Hey kwinn
That works great. It brought me within a couple of hundredth compared to the meter. Good enough for the women I date.
I'll probably still go back and lower the resistance a bit, I may need to speed things back at some point time.
Thanks so much ya'll
Matt
If you want a better intuition about this consider the case of just a 560k resistor - here the time constant is 11.2us but the capacitor will be charging up all the way to 80V. With the divider the same initial current is flowing but the cap only has to charge to 2.76V - for the same current as the other case the dV/dt of the capacitor will be the same, but it has 1/29 as far to go, so does it in 1/29 of the time.
Search "Thevenin equivalence"...
Ok, ok, I surrender. You were right. Sigh. At least I was on the right track as to what was causing the inaccurate readings as post 25 indicates.