Shop OBEX P1 Docs P2 Docs Learn Events
MCP3202 + math question — Parallax Forums

MCP3202 + math question

Matthew JonesMatthew Jones Posts: 14
edited 2012-04-29 10:37 in Propeller 1
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

Comments

  • LeonLeon Posts: 7,620
    edited 2012-04-28 10:16
    Your voltage divider might have the wrong output impedance, although I doubt if it would produce that large an error.
  • Matthew JonesMatthew Jones Posts: 14
    edited 2012-04-28 10:45
    No, I can measure the voltage divider, although the meter is going to vary from divider and the math for the divider the gap is to big.

    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
  • kwinnkwinn Posts: 8,697
    edited 2012-04-28 11:46
    The simplest approach would be to do a 3 point calibration to calculate the value of VoltDiv (voltage divider). Record the value of (DOC * 3.3) / 4096 for inputs of 34, 55, and 80 volts and use that to calculate the value needed for voltage divider. The 3 results should be very close if the circuit is linear and it will compensate for slight variations in component values and loading by the adc chip input.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-04-28 12:23
    It's probably truncation error. But first, (520K + 20K) / 20K = 27, not 28. So try this:
    Vin := DOC * const(27 * 3300) / 4095

    That will give you the input voltage in mV.

    -Phil
  • kwinnkwinn Posts: 8,697
    edited 2012-04-28 12:28
    BTW, you are charging a small (~20pF) sample and hold capacitor in the adc through your voltage divider resistors so you have to take that into account. If the resistors are too high in value you may not fully charge that capacitor in the 1.5 clock periods the internal switch is closed. See the schematic for the details.
  • Matthew JonesMatthew Jones Posts: 14
    edited 2012-04-28 12:30
    No I am sorry I put the wrong value in the original post. It 560k, x28.

    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 Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-04-28 12:58
    No I am sorry I put the wrong value in the original post. It 560k, x28.
    In that case your constant is (560K + 20K) / 20K = 29, not 28.

    -Phil
  • Matthew JonesMatthew Jones Posts: 14
    edited 2012-04-28 13:03
    Oh wow! Thats a simple mistake. LOL

    Thanks Phil
    Matt
  • kwinnkwinn Posts: 8,697
    edited 2012-04-28 13:05
    @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?
  • Matthew JonesMatthew Jones Posts: 14
    edited 2012-04-28 13:17
    Heck I really don't know unless its the " _XINFREQ = 5_000_000". I don't see any other means to set a clk frequancy.

    Thats what I am looking for Is the RC time constant?

    Matt
  • Mark_TMark_T Posts: 1,981
    edited 2012-04-28 13:50
    kwinn wrote: »
    @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?

    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)
  • kwinnkwinn Posts: 8,697
    edited 2012-04-28 14:32
    Mark_T wrote: »
    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 Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-04-28 14:41
    I think the R1 || R2 argument is correct. For one, you're both charging and discharging the input cap. It discharges through both resistors in parallel. Second, don't forget that the voltage that's charging the cap through the input resistor is quite high. It's not like it's a 0 - 3.3V source. One way to think of it is that the charge current will be the same as if it were a 0 - 3.3V input through a smaller resistor.

    -Phil
  • Matthew JonesMatthew Jones Posts: 14
    edited 2012-04-28 15:16
    Thanks All
    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
  • kwinnkwinn Posts: 8,697
    edited 2012-04-28 15:17
    Sorry Phil I have to disagree with you. Lets ignore the mcp internal switch resistance of 1K and consider what happens immediately after the sample switch closes.

    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.
  • kwinnkwinn Posts: 8,697
    edited 2012-04-28 16:04
    Heck I really don't know unless its the " _XINFREQ = 5_000_000". I don't see any other means to set a clk frequancy.

    Thats what I am looking for Is the RC time constant?

    Matt

    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.
  • Matthew JonesMatthew Jones Posts: 14
    edited 2012-04-28 16:52
    How do I find that frequency going to the MCP for the clock. I am "Averaging" it 4 times a second for 1000 samples, but I know that is not what your talking about. I cannot find anything in the bit of code I used, and its little too small to get my probe on. Didn't guess to have to probe the PCB.

    I'll try them tests.

    Thanks
    Matt
  • kwinnkwinn Posts: 8,697
    edited 2012-04-28 17:47
    What we are looking for is the frequency of the pulses that clock the data into and out of the mcp3202. If you can post the “bit of code from Chip Gracey 3202x” you are using and the frquency info for the prop ( usually “clkmode = xtal1 + pll16x” and “ _xinfreq = 5_000_000” ) we can determine the rate for the mcp.
  • Matthew JonesMatthew Jones Posts: 14
    edited 2012-04-28 18:50
    I had trouble attaching the file so I posted it HERE. I think right click and save as or just open it in the browser.

    I use "_clkmode = xtal1 + pll16x" and " _xinfreq = 5_000_000".

    Thanks
    Matt
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-04-28 19:38
    kwinn,

    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:

    attachment.php?attachmentid=92087&d=1335667019

    -Phil
    898 x 891 - 33K
  • kwinnkwinn Posts: 8,697
    edited 2012-04-28 22:07
    Phil, I agree, both of those circuits have equivalent impedances however neither of those circuits are equivalent to the circuit in post #6.
  • kwinnkwinn Posts: 8,697
    edited 2012-04-28 22:25
    @Matt

    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
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-04-29 00:13
    kwinn wrote:
    Phil, I agree, both of those circuits have equivalent impedances however neither of those circuits are equivalent to the circuit in post #6.
    Wow, you're a hard one to convince! :) Okay, I've modeled the S/H circuit in Spice now:

    attachment.php?attachmentid=92095&d=1335683562

    The charge curves for the caps are still identical. QED

    -Phil
    647 x 635 - 58K
  • Matthew JonesMatthew Jones Posts: 14
    edited 2012-04-29 03:48
    kwinn wrote: »
    @Matt

    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

    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
  • Mark_TMark_T Posts: 1,981
    edited 2012-04-29 03:57
    kwinn wrote: »
    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.

    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"...
  • kwinnkwinn Posts: 8,697
    edited 2012-04-29 10:37
    @ Phil & Mark_T

    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.
Sign In or Register to comment.