German-Chinese device and a bit of mathematics (part 2)
Evening came and Shehrezada continued her allowed speeches.
We continue the discussion on how we would make the MG328. Next in line is the measurement of capacitors, which should be more complicated than the measurement of resistors, but suddenly it turns out to be almost easier. But before turning to the consideration of capacitors proper, a few words about measurements in MK systems as such.
The reason for the subsequent text was the reading of the description of the device made by its creator (of course, in translation, and not in the original language, if English is almost native to me, I can not say the same thing about German in any version). And, frankly, he was slightly dumbfounded by some pearls related to accuracy (with all due respect to the author). Reading forums on this topic showed that a lack of understanding of the essence of the measurement process and the parameters that affect the measurement accuracy is characteristic of young engineers.
Now in universities do not read the course on measurements? Or is everyone walking along amicably? By the way, I noticed among my young colleagues (those who are continuing their studies or have recently completed their studies)), in my opinion, they have a not completely substantiated opinion that they know better which of the taught subjects will be useful in their future professional activities than the drafters of the training program. In my opinion, this is a dangerous fallacy, and practice (which, as the criterion of truth is known) is on my side. Of course, we, as students, often did not understand why we, future engineers, should read one or another absolutely unnecessary (from our point of view) subject. And if for some of them (objects) such an opinion turned out to be correct, in general, from the height of my current experience, I must declare with all responsibility - we were wrong. There is a wonderful saying, “There is no superfluous knowledge”, and it applies just to this situation. This will end the digression and return 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) in this matter.
To begin, consider the proposals of forum participants to improve the accuracy of this device (some of them are even implemented - though comparative results are not available, it would be fun to see). Since the applied MCs allow the use of several types of reference voltage for the ADC (supply voltage, internal source, external source), young engineers somewhere heard that the last two options provide more accurate data than the first, the most harmless suggestion is to change the type of support, since it is obvious to them that the use of power as a support is explained solely by the desire to save on external components. Completely on the other side of good and evil are proposals to share the supply voltage, Indeed, The supply voltage in this circuit is determined by a linear stabilizer and cannot be considered accurate (see stabilizer specifications). But even less accurate is the internal voltage of the support (see MK specifications), so its use requires a calibration procedure during manufacture (although the stability of the internal source is not bad). Therefore, most of the proposals boil down to the use of an external voltage reference source on a zener diode or on a voltage source (hello, 431).
Why are these sentences incorrect and can only worsen the accuracy of measurements? It's all about the nuances. To begin with, let me remind you of the basic formula for the operation of the ADC
N = Ux / U0 * Nmax (1),
where N is the received value, Ux is the measured voltage, Uo is the reference voltage, Nmax is the maximum value of the result (usually 2 ** the number of bits 1). Let us take into account that part of the supply voltage is used as the test voltage (this is determined by the methodology), which is written as Ux = K * Ucc, and K <= 1. For the case of power supply in the form of a support, expression (1) takes the form
N = Ucc * K / Ucc * Nmax = K * Nmax
and (provided that Nmax = const) we see that the conversion is accurate and does not depend on the accuracy of the power supply. But for the case with external or internal reference voltage, which is weakly related to the supply voltage (this is indeed the case under certain conditions), expression (1) takes the form
N = Ucc * K / Uo * Nmax = K * Nmax * (Ucc / U0 ),
from where it is easy to see that even if we take U0 = const, then 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 has fallen. So we have come where the road of good intentions leads.
Why did this happen? Because the use of an external highly stable support allows you to increase the absolute accuracy of the measurement relative to some external standard (which is stored in the chamber of weights and measures), but in our case this is completely unnecessary. We are interested in the ratio of two voltages, but its accuracy is deteriorating. If we could even switch the reference voltage to the circuit under study, the situation would not deteriorate with accuracy (although it did not improve), but we cannot do this (well, at least I certainly cannot 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 for all parameters), but for the bulk of measurements it will only get worse. In general, the situation resembles an old joke with the final phrase “Strange,
Well, the second part of the theoretical reasoning is connected with the passage from the description of the device (you have to give a big quote)
The dimension in mV can be obtained by summing the results of 22 readings, multiplying the sum by 2 and dividing by
9. By this method, the maximum value will be 1023 · 22 · 2/9 = 5001, which ideally corresponds to the desired dimension of the measured voltage values in mV .. In addition additionally, there was hope that an increase, from oversampling, of the ADC resolution can 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 it by 20, so that the result is equal to the original resolution of the ADC. That is, along this path, increasing the resolution of the ADC is impossible.
As they say, I read and cried, and after all, the author of the device is not at all a fool, where do such assumptions come from? For a start, the logic that shocks with its logic is that if you take the sum of 20 measurements and divide by 20, then the accuracy remains unchanged, but if you take the sum of 22 measurements and divide by 9, then yes, it can increase. Maybe, of course, but no more than if you take 20 values and divide by 10 (but for some reason this was not done in the original?), And even then under strictly defined conditions, which are mentioned in AVR121 and there is no certainty that these conditions are observed with us. But okay, this error is harmless, and he was not completely sure of it. But in any case - why do we need millivolts? In all the calculated ratios, the stress ratio is used, which gives us a pure value with the ADC (somewhere in the calculation chain it will be necessary to take into account 1000/1024 and that's it). But the proposal to take 22 instead of 20 deserves separate consideration. I have not looked at the code yet, but if we assume that the measurement takes 1 ms (or is carried out with such an interval), then 20 measurements are undoubtedly preferable for averaging than 22, since they allow weakening the influence of interference with a frequency of 1 / (20 * 1ms) = 50 Hz , which is decisive in the world around us. I would also understand 16, if around you 60 Hz (such as in the USA), besides it is easy to divide by 16, but 22 (!), Just to make it more convenient to count in millivolts, which we absolutely do not need - it's outside my understanding. Of course, these are all nit-picking and, most likely, the influence of network interference can be neglected and 20 measurements are taken from other considerations,
Well, okay, let's go. Now we can proceed directly to the topic of discussion - the measurement of capacitors. The capacitor has a determining parameter - the capacitance, and here we start. There are many other parameters, including ESR and loss tangent, which are measured by this device, but we will discuss this topic later (I don’t specifically read the description in those places that I haven’t invented yet). So, we are starting to measure capacitance. The basic idea is simple - we begin to charge the capacitor with a known voltage for a certain time, then we look at the voltage on it and by means of simple (but labor-consuming for MK) transformations we obtain the value of capacitance. The basic formula of the charging process is described by the expression (assuming an initial voltage of zero)
Ux (t) = Uo * (1-exp (-t / RoCx)),
где Uo — напряжение заряда, Ro — сопротивление, через которое заряд осуществляется, Cx — исследуемое напряжение, а t — (внезапно)время. Тогда в некоторый момент времени tx мы получаем
Ux=Uo*(1-exp(-tx/RoCx), откуда
Сx=tx/(Ro*ln(-U0/(Ux-U0))) (что то не могу я из Latex извлечь красивую формулу, подскажите в личку как, плиз).
Оставим пока за скобками сам процесс вычислений и проанализируем формулу с точки зрения точности, для чего насколько ее перепишем
Сx=tx/(Ro*ln(-1/(K-1))), где K = Ux/Uo
Считая Ro абсолютно точным, вы видим, что точность определяется точностью измерения временем с коэффициентом 1, а вот с точностью измерения отношений напряжений дело посложнее. Чувствительность по параметру K представляет собой выражение
1 / (ln (1-x) ** 2 * x-ln (1-x) ** 2), which is not very nice in analytics. Nevertheless, using Latex, we plot this expression and see that values of K greater than 0.97 are completely unacceptable (who would doubt it), but values below 0.35 are also not encouraging in terms of accuracy (here it’s not very clear, you need to think about it). Moreover, in this range the sensitivity is from 5 to 2, that is, we inevitably lose accuracy with respect to the accuracy of the ADC.
The measurement method itself involves two methods: either we charge the capacitor until a certain value is reached and measure the time for which it is done, or we charge the capacitor a certain time and measure the achieved voltage value.
The first method requires a comparator, but it can significantly simplify the calculation, if we take the comparison level at Ux = Uo * (1-1 / e), then Cx = tx / Ro is easy to calculate. Consider the range of measured capacitances with this option. With a given Ro, there is no upper limit 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 an acceptable time. If we set the maximum measurement time in tmax, then Cx <= tmax / Ro, which for the values of the resistances accepted in the device will be
Cx <= 1/680 = 1.5 * 10 ** - 3 (F / s), i.e. for 2 seconds - 3000 μf, etc. If the measured values are outside 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 clearly large, the voltage across it should not drop significantly) and determine the capacity by the formula
Cx = 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, there is also an argument in favor of MSP430), so we use piecewise linear approximation for calculations. Given that it is undesirable for us to go beyond the K value of 0.3, we get the upper value for the capacitance of 20,000 microfarads.
But the minimum value of the measured capacitance is determined by the desired measurement accuracy, which in this case is determined by the accuracy of the time measurement. Assuming that the absolute error of time measurement is equal to the frequency of the MK clock, and having given the required accuracy, we can obtain the lower boundary of the measured values
Cx> = tt * (1-q) / Ro,
where tt is the MK clock frequency period, q is the required accuracy. To achieve an accuracy of 1% at a frequency of 1 MHz and the received 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 decrease 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 win in the accuracy of determining the time by 2 times, but we must understand that we lose in the accuracy of determining K by 2 times, nevertheless, there will be a gain, since the errors are not multiplied, but added up. For smaller capacitors, you can come up with more complex algorithms using PWM modulation, but we don’t need it at all,
By the way, you should pay attention to one more point, namely the clock frequency error. Judging by the scheme, a built-in clock generator is used, which is characterized 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 circuit with the proposed algorithm will determine the capacitance in the range from 2.5pF to 20 mF with an accuracy of 5%. Based on the fact that usually capacitors have a spread of 20%, the problem should be considered solved.
Only registered users can participate in the survey. Please come in.
Presentation Level - Author's Guide
- 13.3% Excessive attention to obvious things, not children gathered here 4
- 46.6% Lack of attention to difficult moments, not all here are professionals in this field 14
- 50% Most of all, everything is clear and fully covered, respect and respect 15