📜 ⬆️ ⬇️

German-Chinese device and a bit of mathematics (part 2)

Evening came and Shehrezada continued the allowed speeches


Let's continue the discussion on how we would make the MG328 device. The next step is the measurement of capacitors, which should be more difficult than the measurement of resistors, but suddenly it turns out to be almost simpler. But before proceeding to the consideration of the actual capacitors, a few words about measurements in the MK systems as such.

The reason for the subsequent text was the reading of the device description made by its creator (of course in translation, not in the original language, if technical English is almost native for me, I can’t say the same about German in any form). And, to be honest, I was a little taken aback by some pearls related to accuracy (with all due respect to the author). Reading the forums on this topic showed that a lack of understanding of the essence of the measurement process and parameters affecting the measurement accuracy is characteristic of young engineers.
Now in high schools do not read a course on measurements? Or do they all skip it together? By the way, I noticed from my young colleagues (continuing education or recently graduating from him)), in my opinion, not quite reasonable opinion that they know better which of the taught subjects will be useful to them in their future professional activity than compilers of the training program. In my opinion, this is a dangerous delusion and practice (which, as is known, the criterion of truth) is on my side. Of course, when we were students, we often did not understand why we, future engineers, were reading this or that absolutely unnecessary (from our point of view) subject. And if for some of them (subjects) such an opinion turned out to be correct, by and large, from the height of our experience today, I must state with full responsibility - we were wrong. There is a remarkable saying “There is no extra knowledge,” and it applies to this situation. This concludes the lyrical digression and returns to the accuracy of measurements in MK systems under various conditions.

I consider it my duty to clarify (of course, just a fraction of it) to this question.
To begin, consider the proposals of the forum participants to improve the accuracy of this device (some of them are even implemented - although comparative results are not available, it would be fun to see). Since the applied MCs allow using several types of reference voltage for the ADC (supply voltage, internal source, external source) and young engineers have heard somewhere that the last 2 options provide more accurate data than the first, most innocuous proposal - to change the type of support, because it is obvious to them that the use of food as a support is due solely to the desire to save on external components. Absolutely on the other side of good and evil are proposals to divide the supply voltage. Indeed, the supply voltage in this circuit is determined by a linear stabilizer and cannot be considered accurate (see the characteristics of the stabilizer). But even less accurate is the internal voltage of the support (see the characteristics of the MC), so its use requires a calibration procedure during manufacture (although the stability of the internal source is not bad). Therefore, most of the proposals come down to the use of an external source of reference voltage on the Zener diode or on the voltage source (hello, 431).
Why are these sentences wrong and can only worsen the accuracy of measurements? It's all about the nuances. To begin, let me remind you of the main formula of the ADC
N = Ux / U0 * Nmax (1),
where N is the obtained value, Ux is the measured voltage, Uo is the reference voltage, Nmax is the maximum result value (as a rule, 2 ** the number of digits is 1). Let us take into account that a part of the supply voltage is used as the voltage to be tested (this is determined by the method), which we write as Ux = K * Ucc, with K <= 1. For the case of food in the form of a support, the expression (1) takes the form
N = Ucc * K / Ucc * Nmax = K * Nmax
and (provided that Nmax = const) we see that the conversion is exact and does not depend on the accuracy of the power setting. But for the case of external or internal reference voltage, which is weakly related to the supply voltage (this is true under certain conditions), the expression (1) takes the form
N = Ucc * K / Uo * Nmax = K * Nmax * (Ucc / U0),
whence it is easy to see that even if we accept U0 = const, the code depends on the accuracy of the Ucc task, which we wanted to get rid of (funny, isn't it?), that is, the measurement accuracy fell. So we came to where the road leads good intentions.
Why did this happen? Because the use of an external highly stable support makes it possible to increase the absolute accuracy of measurement relative to some external standard (which is stored in the chamber of measures and weights), but in our case it is absolutely not necessary. We are interested in the ratio of two voltages, but the accuracy of its measurement deteriorates. If we could also switch the reference voltage to the circuit under study, then the situation would not worsen with accuracy (although it did not improve), but we cannot do it (well, at least I can’t do it without external elements). That's when we measure the parameters of the diodes (and we will do it), the external support will improve the results (and not only for all parameters), but for the main mass of measurements it will only be worse. In general, the situation resembles an old anecdote with the final phrase “It’s strange, just yesterday Givi was pulled out of the well just the same way”.
')
Well, the second part of the theoretical reasoning is connected with the passage from the description of the device (it is necessary to quote a big quote)
The dimension in mV can be obtained by summing up the results of 22 readings, the amount multiplied by 2 and divided by
9. With this method, the maximum value would be 1023 · 22 · 2/9 = 5001, which ideally corresponds to the desired dimension of the measured voltage values ​​in mV. In addition, there was an additional hope that the increase, from oversampling, of the resolution of the ADC can help improve the voltage read from the ADC as described in AVR121 [5]. In the original version, the ReadADC function accumulates the result of 20 ADC measurements and then divides into 20, so the result is equal to the original resolution of the ADC. That is, in this way, increasing the resolution of the ADC is impossible.

As they say, read and cry, and because the author of the device is not a fool, where such assumptions? To begin with, its logic is shocked by the assumption that if we take the sum of 20 measurements and divide by 20, then the accuracy remains the same, but if we take the sum of 22 measurements and divide by 9, then it is yes, it can increase. Of course, it can, but no more than if you take 20 values ​​and divide by 10 (but for some reason this is not done in the original?), And then under strictly defined conditions, which is said in AVR121 and there is no certainty that these conditions are observed here. But okay this is a harmless error, and he was not completely sure about it. But in any case - why do we need millivolts? In all the calculated ratios, the voltage ratio is used, which gives us a net value with the ADC (somewhere in the chain of calculations, 1000/1024 will have to be taken into account and that's all). But the proposal to take 22 instead of 20 deserves separate consideration. I have not yet looked at the code, but if we assume that the measurement takes 1 ms (or is performed at such an interval), then 20 measurements are undoubtedly preferable for averaging than 22, because they allow to reduce the effect of interference 1 / (20 * 1ms) = 50 Hz , which is decisive in the world around us. I would understand 16 if around you 60 Hz (like in the USA), besides, it is easy to divide by 16, but 22 (!), Just to make it easier to recalculate in millivolts, which we absolutely do not need - this is beyond my understanding. Of course, this is all niggling and, most likely, the influence of network interference can be neglected and 20 measurements are taken from other considerations, but that’s how it is from the bay-windshield and what to change is strong.

Well, okay, passed. Now you can go to the actual topic of discussion - the measurement of capacitors. The capacitor has a determining parameter - the capacity, and here we will begin. There are many other parameters, including ESR and the loss tangent, which this device seems to measure, but discuss this topic later (I do not specifically read the description in those places that I myself have not yet invented). So we begin to measure capacity. The basic idea is simple - we start charging a capacitor with a known voltage for a certain time, then we look at the voltage on it and, through simple (but laborious for MK) transformations, we obtain the capacitance value. The basic formula of the charging process is described by the expression (assuming the initial voltage is zero)
Ux (t) = Uo * (1-exp (-t / RoCx)),
where Uo is the charge voltage, Ro is the resistance through which the charge is carried out, Cx is the voltage being investigated, and t is (suddenly) time. Then at some point in time tx we get
Ux = Uo * (1-exp (-tx / RoCx), from where
Cx = tx / (Ro * ln (-U0 / (Ux-U0))) (I can't get something out of Latex to get a beautiful formula, tell me how, pliz).
For the time being, we leave behind the brackets the process of calculations and analyze the formula from the point of view of accuracy, for which reason we will rewrite it
x = tx / (Ro * ln (-1 / (K-1))), where K = Ux / Uo
Considering Ro to be absolutely accurate, you can see that accuracy is determined by the accuracy of time measurement with a factor of 1, but with the accuracy of measuring the relationship of voltages it is more difficult. Sensitivity parameter K is an expression
1 / (ln (1-x) ** 2 * x-ln (1-x) ** 2), which is not very pleasant in analytics. Nevertheless, with the help of Latex, we plot this expression and see that K values ​​greater than 0.97 are completely unacceptable (who would doubt), but values ​​below 0.35 are also not pleasing in terms of accuracy (it’s not very clear here, you need to think). Moreover, even in this range, the sensitivity ranges from 5 to 2, that is, we inevitably lose accuracy with respect to the accuracy of the ADC.
The method of measurement itself involves two methods - either we charge the capacitor until a certain value is reached and measure the time during which it is done, or we charge the capacitor for a certain time and measure the achieved voltage value.
The first method requires a comparator, but it allows you to significantly simplify the calculations, if we accept the comparison level in Ux = Uo * (1-1 / e), then Cx = tx / Ro is easy to calculate. Consider the range of measured containers with this option. At a given Ro, there is no limit from above at first glance - any capacitor will sooner or later charge (if we forget about leakage currents), but there is a desire to get results in a reasonable time. If we set the maximum measurement time in tmax, then Cx <= tmax / Ro, which for the values ​​of resistance adopted in the instrument will be
Cx <= 1/680 = 1.5 * 10 ** - 3 (F / s), that is, for 2 seconds - 3000 mcf, etc. If the measured values ​​go beyond this limit, then the second method can be applied, that is, if the capacitor has not charged to the required value in the allotted time, we can measure the obtained value (since the capacitor is obviously large, the voltage on it should not drop significantly during the measurement) and determine the capacity of the formula
x = to / (Ro * ln (-1 / (K-1))), where K = Ux / Uo.
It is unlikely that we could implement a direct calculation using this formula on an 8-bit MK with a small program memory (by the way, also an argument in favor of MSP430), so we can use piecewise-linear approximation for calculations. Given that it is undesirable for us to go beyond the value of K in 0.3, we obtain the upper value for a capacitance of 20,000 microfarads.

But the minimum value of the measured capacity is determined by the desired accuracy of measurement, which in this case is determined by the accuracy of time measurement. Assuming that the absolute error of time measurement is equal to the frequency of the clock generator MK, and given the required accuracy, you can get the lower limit of the measured values
Cx> = tt * (1-q) / Ro,
where tt is the period of the clock frequency MK, q is the required accuracy. To achieve an accuracy of 1% at a frequency of 1 MHz and the adopted resistors, we obtain Cx> = 1e-6 * (1-0.01) / 470e3 = 212 pF. If we take the frequency of 8 MHz and turn on 2 resistors in series, then we get Cx> = 0.125e-6 (1-0 / 01) / 470e3 / 2 = 13pF. For an accuracy of 5%, the minimum value will be reduced to 2.5 pF, which is quite acceptable in a practical sense. Pay attention to the inclusion of two resistors in series. In this case, we gain 2 times the accuracy of the time determination, but we must understand that we lose 2 times the accuracy of the definition of K, however, there will be a gain, since the errors do not multiply, but add up. For smaller capacitors, you can come up with more complex algorithms using PWM modulation, but we don’t need this at all, anyway, for such small values, the error will exceed any acceptable numbers.

By the way, you should pay attention to one more thing, namely the clock frequency error. Judging by the scheme, a built-in clock generator is used, which is distinguished by a large spread of values ​​(tens of percent without calibration, units with it) and no stability both in voltage and temperature. Therefore, the use of an external quartz resonator is, in my opinion, an urgent need from the point of view of ensuring the accuracy of measurements.
So the general conclusion is that this device scheme with the proposed algorithm will allow determining the capacitance in the range from 2.5 pF to 20 mF with an accuracy of 5%. Based on the fact that usually capacitors have a variation of 20%, the problem should be considered solved.

Source: https://habr.com/ru/post/273875/


All Articles