"And proved that since ... then the circuit at the frequencies of the required range will not work" Savchenko "Opening myself"
The reason for writing this opus was, as always happens with me, a confluence of circumstances. Reflecting on the features of the possible implementation of the interface from the microcontroller (MK) to WS2812, I stumbled, quite by accident, on something similar on the website of one company, which I will not call, because I intend to do it slightly (and maybe I don’t know much yet course of presentation) to scold. I give a hint - she sells Arduino boards and shields to them in our country, the name starts with the first letter of the Russian alphabet, ends with the same letter, and the product that promoted me to this post is located on the last position of the fourth page in the section "Boards and Modules ", On the page of this product you can find a device diagram and a link to the program, about which I would like to tell something interesting, especially for young engineers (well, I think so). I do not know how the other inhabitants of Habr, and I have such that I read the text and see that some word in it is wrong. That is, I immediately understand that it is wrong, but then it takes some time to get a grasp of it and understand what is wrong with it and where is the error. Unfortunately, this rule applies only to other people's texts, when I read my own, I often read not what was actually written, but what I was going to write (I really know what should be here). Well, at first glance at the diagram, it seemed to me wrong, and upon careful consideration, the first opinion was confirmed.
First, the general prerequisites for the development of this scheme and some theoretical propositions.
When developing various electronic devices, we often encounter the need to visualize the results of processing incoming information, both for the purpose of debugging and for implementing the necessary functionality. One of the most commonly used methods are the LED emitters of the optical range (LED blinking with all the teeth on the edge is one example), both in the form of individual elements and in the form of emitters assemblies, of which seven-segment indicators are very common (in fact, they are eight-segment time, but apparently it was not always like this).
Control of such a device is not a particularly difficult task, especially if you do not resort to multiplication, but direct control (that is, one leg per segment) presents 2 difficulties in implementation: first, the number of legs involved equals the number of segments, that is, four-digit indicator, we need 4 * 8 = 32 legs of the MK, which is not always possible, and secondly, if we solve the first problem, then we are in for trouble with the inflowing (flowing) current, 32 * (even if) 4 = 128 mA, which obviously exceed x Characteristics of conventional MK (the current on one leg is usually regulated (from 4 to 20 ma), but there is also a limit on the amount of simultaneously flowing currents (from 100 mA to 300 mA)) and although, most likely, MK will not fail and even will function in in such conditions, I would not recommend strongly going beyond the TU (currently they are called datasheets) (we will talk about this in detail later).
Therefore, various types of buffer circuits have become widespread, which allow, through various tricks, to reduce the number of MK legs involved and distribute the current of LEDs across several housings, significantly improving the operating conditions of both the MK and the buffers.
From the rich variety of buffer circuits, the authors of the aforementioned device selected shift registers of the type 74HC595, which are widely known among radio amateurs. These devices allow you to control eight outputs using three inputs (of course, not for free, the control algorithm is complicated, but we didn’t expect anything else), but, in addition, these devices can be switched on in series, which allows you to control using the same three legs theoretically an unlimited number of output signals (practically, of course, there are limitations, but more on that later).
Some details about registers in general and 595 in particularHere I will endure what is obvious to me, but it may not be quite clear to some of the readers.
The said chip is a shift register with an additional gated register at the output. Let's try to translate this phrase into Russian.
First of all, what is a register. This is a device that allows you to fix its outputs at some point in time, depending on the state of the inputs at that moment and later, i.e. until another such moment occurs, the outputs do not change regardless of the change in inputs. At the same time, the onset of such a moment is determined by some of the entrances that perform special functions (it was such a difficult expression that I had for the short word strobe).
')
What registers give us as such for solving the indicated problems is that we can put several registers, combine their information inputs, make a separate gate for each of them and, having spent N + n outputs of the MK, get N * n independent output signals, for our example with N = 8 and n = 4 will require 12 legs MK instead of 32. This scheme is easily scaled, but still for a number of MK even 12 outputs is an unacceptable luxury.
And here shift registers come to the rescue, which allow using just two legs to organize any number of outputs to a controlled device (indicator). The principle of the shift register is similar to a regular register (it consists of a set of single-pin registers, or flip-flops) and consists in fixing the output value at the time of the strobe, and the highlight is that the output of one trigger is the input of another (but also the output of the device whole) and the input signal is connected only to the first trigger. At the same time, we first feed in for the last trigger in the chain, fix it on the first trigger with a strobe, then send a signal for the penultimate trigger in the chain, fix it in the first trigger with the next gate, while what was recorded in the first trigger by the previous gate , is transferred to the second trigger of the chain, and if we continue the procedure, the required number of gates, then all the necessary information will be in the right triggers throughout the chain. It is easy to see that the scheme is scalable, that is, increasing the length of the chain of triggers does not generally lead to an increase in the number of necessary conclusions of the MC.
This scheme has one important drawback (in fact, there are not one, but it is he who is important to us) —in the process of promoting information, the outputs of all the triggers in the chain repeatedly change their meaning before receiving the data intended for them. If we record information quickly enough, and then leave it unchanged for a long time (compared to recording), then the actuator may not notice these fast changes of values, especially if it is rather slow. Unfortunately, LEDs do not belong to such slow devices, and you will be surprised if you conduct experiments to see how long the pulses lead to the observed flashes of light (with short interruptions everything is much better, you can hardly watch them, but this is a weak consolation ).
Therefore, the device 595 consists of two parts - a shift register of length 8 and the usual register 8 bits wide connected after it, the information in which is recorded at a special input. At the same time, we write to the shift register using a shift gate, but the information on the outputs does not change, but upon completion of the shift operations, we issue a census gate and the information on all outputs is updated synchronously and remains unchanged, allowing us to perform the following procedure for writing to the shift register.
So I got a description of the register, probably, I still exaggerate the degree of unpreparedness of the audience, but you can not open the spoiler. By the way, there will be a survey down there, do not be lazy to answer, please, this is really important for me as an author.
So, we get the opportunity with the help of just three legs of the MK to control an arbitrary number of outputs, intended, in our case, to turn on / off the LED indicator segments. That is, our gain is obvious, what is the loss, it definitely should be, because “DarNaBy”? The loss consists in a longer time required for the transmission of information, compared with the method of direct control, and it will be approximately N * n times. For relatively slow devices (and LEDs, undoubtedly, they are, as the end user is a person with his limited, and very, speed of information processing), such a loss is not so significant, but nevertheless it takes place, and forget about the limitations This or that method should not be followed if you are not a fan of walking a rake.
But, as you know, there is no limit to perfection, and the inquisitive thought of the developers began to look for ways to cut even more the number of necessary legs. To do this, we need to combine the functions performed by some of our three legs, or even all. We will combine at least one, I still have a bad idea of ​​the interface to the device from less than one signal (note, I wrote a signal, not wires - this is about wireless systems). What opportunities do we have in this direction? There are not so many of them, but only two — manipulation of the amplitude of the signal and manipulation of temporal parameters. In the direction of amplitude, we can not even look, the movement in this direction is very difficult, but with time you can play. For example, we can replace two signals - the data and the strobe by one; we accept the fact that the signal is present is equivalent to the arrival of the strobe, but the time parameters (duration, as an option) can carry information about the state of the data.
Many have followed this path, including the developers of the aforementioned device, the logic of their solution can be represented by the following description: we transmit a signal in the form of pulses of various durations along one line - short, longer and longer. One of them will mean the transfer of the unit, the other - zero, and the third - the completion of the transfer and fixation of information in the output register. The choice of a specific distribution of durations is determined by our desire (quite natural) to transmit information as quickly as possible, therefore the longest impulse signal (sign of transmission completion) is applicable to the longest impulse, the remaining two can be distributed arbitrarily, but, based on the operating logic 595, it is more convenient to record units apply a short pulse, so for zero, nothing but an average duration is left.
Now for the operation of our device, we need hardware solutions that can distinguish one signal from another (to select by time parameters). That is why we chose the duration, because the selector for this signal parameter is much easier to implement than for other parameters, for which a circuit consisting of a series-connected resistor and a capacitor to earth can be successfully applied, the so-called RC chain, also known as an integrating chain or low pass filter of the first order.
If we look at the scheme of the device being described, we will see two such chains on it, one of which is designed to distinguish between a short and genuine pulse (here lies an ambush, but more on that later), and the second - to separate the longer and longer pulse. We will talk about the calculation of the parameters of these chains.
The idea of ​​this type of selector is very simple - the capacitor does not allow the voltage across it to change abruptly, so when a pulse is applied to an integrating chain, the voltage at the end of this pulse will depend on its duration (according to a complicated formula, but this does not matter) and for the gate, the voltage on the capacitor at this moment can be considered as data, and recorded at the output of the register (shift). Hence the requirements for the duration of the pulse - a short pulse should create a voltage on the capacitor, perceived as a register input, as a high level, and a long one - as a low one.
Here we deal at once with a set of unknowns - the duration of two types of pulses, the amplitude of the pulses (it is included in the formula), the parameters of the chain, the high and low levels of the input levels. Let's start reducing their number. Immediately determine that the pulses take two values ​​in amplitude - high, equal to the supply voltage (U), and low, equal to the ground (0). Strictly speaking, this is completely unnecessary, but it turns out much easier and, believe me, the choice of other values ​​will not make life easier for us, but quite the opposite.
The following data are input levels, here the TU on a chip will help us, which claims that for 74HS595 any voltage exceeding 0.7 * U will be considered high, and any voltage not exceeding 0.3 * U will be considered low, here U is the supply voltage register and let it coincide with U for the pulse parameters, again so it is easier to do and easier to count. Note that the widespread opinion about the threshold value of 0.5 * U is not supported by the authors of TU.
A small distraction to the side, in your opinion, what is more: 4.2V or 3.2V? Do not rush to answer, think, because your completely natural answer will come with a contradiction with the opinion of the well-known company NXP, the TU of which on this device on page 7 shows the minimum value of 4.2V and typical 3.2B (personally, it seemed to me that the typical should be between minimum and maximum, and the maximum should be greater than the minimum, and here it does not work out in any way, well, live forever, learn forever). Fortunately, an equally respected company TI in its TU results in the minimum value of the input voltage of 4.2V, which we will be guided by. All these data are given for U = 6V, that is, 4.2 = 6 * 0.7 is performed.
Actually, let me take a light fairy in the direction of NXP (they do not read Habr anyway, and none of you will teach them that I scolded them, so I don’t risk anything), but what is the typical threshold value and why? did it give up? Do most chips switch at this value in the whole range? Do all chips switch at this value under normal conditions? I can come up with a dozen of similar definitions, but the point is that no one needs a typical value, well, except for common development, if we design a device that works without selecting components, which, by the way, is strictly prohibited by GOST, yes This abbreviation, apparently, in our time, many are neglected, and yet their stupid people invented.
Returning to our task, we identified 4 unknowns, two more remained - the duration of the pulses and the parameters of the chain. The latter is easy - the behavior of the integrating chain is described by the equation U (t) = Uo + (U-Uo) * (1-exp (-t / T)), where Uo is the voltage on the capacitor at the time of the voltage applied to the resistor, U-value applied voltage, T = R * C is the time constant of the chain, t-time.
Then the voltage at the moment of the end of a pulse with a duration Tx will be Ux = U0 + (U-Uo) * (1-exp (-Tx / T)).
If we set the value of Tx, then the time for which the voltage reaches such a value will be obtained by the formula
Tx = -T * ln (1- (Ux-Uo) / (U-Uo)), or if we substitute the selected values, then Tx = -T * ln (1- (Ux-U) / (0-U )) and it is obvious that Tx = -T * ln (Ux / U).
Substitute the specific values ​​and get Tx (Ux = 0.7 * U) = 0.35 * T and Tx (Ux = 0.3 * U) = 1.2 * T.
What practical conclusions can we draw from these two numbers? First of all, 1.2 / 0.35 = 3.43, which means that the pulse durations recognized by our selector should be at least 3.43 times different. We will strengthen this requirement by taking into account the possible variation of the components, which we assume for the resistor 10%, and for the capacitor 20% (the latter is not so good, usually the TU indicates -20% / + 50%, but we took capacitors with good performance), then we get 3.43 * 1.1 / 0.9 * 1.2 / 0.8 = 6.29, and this is the ratio of the pulse durations that will be guaranteed to be recognized for any (within the TU) variation of the circuit parameters. Of course, these are limiting values, and in practice it is very unlikely that parameters will be simultaneously rejected in one direction, but we must take into account the worst case. “Of course, all this can and should not be done if you are not interested in the final result” - Zhvanetsky.
We have a similar figure for the ratio of the time constants of two selectors, which must respond differently to one pulse, it must exceed 6.29 to meet the criterion of reliable operation. Now we can proceed to the selection of the remaining unknowns, namely the pulse duration and the parameters of the selectors.
To begin with, we will define an additional selection criterion - we are trying to design a circuit with maximum speed, therefore, if possible, the pulse durations should be minimal. As the duration of a short pulse (t1), we choose the pulse that is as small as possible due to hardware limitations (speed of operation of the MC and the chosen method of pulse formation). Following this, we determine the pulse duration more authentic (to) at 6.29 * t1 and, accordingly, the length of the long pulse (t2) is determined at 6.29 * 6.29 * t1 = 39.51 * t1.
Then the time constant of the first selector should be T1> = t1 / 0.35 / 0.9 / 0.8 = t1 * 3.96 and at the same time T1 <= t0 / 1.2 / 1.2 / 1.1 = t0 * 1.15 = t1 * 3.97, that is, the value T = 3.96 * t1 satisfies both criteria, well, it should be. Similarly, we obtain that the time constant of the second selector is T2 = 3.96 * t2 = 156.5 * t1. It remains to choose the value of t1 and the problem of choosing the pulse duration is solved completely.
But let's see if there are any questions left? It turns out that they have remained, namely the question of the intervals between pulses. Since we assumed in the calculations that the initial value of the voltage on the capacitor was U - the supply voltage, and at the end of the pulse it deviates significantly from this value, therefore we must take measures to return the voltage on the capacitor to the supply voltage.
If we guessed parallel to the resistor to turn on the diode (better Schottky and better with a small current-limiting resistor in parallel), then the voltage will increase during the pause between pulses with a significantly smaller time constant and this problem loses its relevance, although then we must take into account the forward voltage drop across diode when determining the initial value.
If we have not done this (well, it turned out), then we will need quite a significant interval between the pulses, and anyway, if we look at the formula for the behavior of voltage over time, we will never get the value of a high level, although in time we will approach it negligible value. Well, yes, the theory of limits has very little to do with real development, and we have to ask ourselves some accuracy that suits us. Calculations show that if we choose a time interval of 3 * (T1, T2) to restore the voltage, the voltage across the capacitor before the next pulse will be at least 0.95 * U, which is a good approximation.
Thus, we obtained all the necessary data for constructing the circuit and for implementing the program, the circuit serves the data, it is only necessary to determine t1, and all other parameters are obtained automatically. There is one more important question - how we will implement the time constants T1, T2, because they are determined by the relation T = R * C, which means that the required value can be obtained in many ways. Decisions are usually made on the basis of various factors, sometimes simply because of the presence of a particular denomination or its applicability elsewhere in the device, but we will approach this issue scientifically.
To begin with, we will consider a resistor on which restrictions are imposed from above - the current flowing through the resistor in the selector must be significantly (an order of magnitude or more) higher than the input current of the circuits of the devices connected to it, otherwise our initial assumption about the dependence of voltage on time is subject to adjustment. We turn to TU and see that the input current is 1 µA, then we get Ir = U` / R >> i = i1 * n, where U` is the minimum voltage at the end of the pulse, i1 is the input current of the device, n is the number of devices. Hence R << U` / i1 / n, substituting the values ​​of R << 0.3 * 3.3 / (1 * 10-6) / 4 = 247 Kom.
But there is also a limitation from below, besides the fact that a small resistor may require too large a capacity, namely the power allocated by the resistor. For it, the relation Pr = U * Ir = U * U / R <= Pmax is true, whence we get R> = U * U / Pmax = 3.3 * 3.3 / 0.125 = 87 ohm (for small-sized resistors). Not that it was a strong limitation, the requirements for a reasonable capacity would still force us to increase the resistor, but remembering it is never harmful.
Nevertheless, our calculations led us to the range 247Kom >> R> = 87 ohms, which once again confirms the empirical rule - if you are too lazy to count the resistor nominal, set 10Kom, you will not be mistaken. Well, given the nominal value of the resistor, the value of the capacitor is calculated in one action.
But here one danger lurks us - the capacitor's nominal value obtained by calculation may not be available (well, it is not in the standard series), so we should add another 10% detuning to the calculations of the ratios of pulse durations and time constants, leaving it to the inquisitive reader. And another consideration that is unlikely to manifest itself in a real circuit, but you need to remember about it - the selected capacitor rating must significantly (by an order of magnitude or more) exceed the input capacitance of the device, which according to TU is 10 pF, which leads to a lower bound C >> 10pF * 4 = 40 pF, otherwise we should take it into account in our calculations, and I would not recommend this categorically, since we only know its upper limit.
Now, when we know how to calculate the elements of such a scheme, let us pay attention to the concrete implementation with which everything began. There, a 3.3K resistor and a 2.2nF capacitor are used in the first selection chain, which determines t1 = 7.26 μsec, and for the second selector t2 = 72.6 μsec, which satisfies the criterion t2> 6.29 * t1. The values ​​of both resistors satisfy the conditions of choice, the capacitor also passes through the limitation from the bottom, everything is fine here. True, we have somewhat reduced the possible speed, but it is not so critical.
Next, we determine the pulse duration. The recording pulse duration unit should be t1 <= 0.38 * T1 = 2.54 microsec, the zero recording pulse width should be 0.35 * T2 = 25.4> = t0> = 1.2 * T1 = 8.71 microsec and the data recording pulse duration should be t3> = 1.2 * T2 = 87.12 µs. A pause after t1 should be more than 3 * T1 = 21 µs, a pause after t0 - more than 3 * T1 = 21 µs and a pause after t3 - more than 3 * T2 = 210 ps.
We naturally cannot see the duration of the impulses given on the diagram, so we turn to the text of the sketch available on the link on the product description page. And here we are in for some discrepancy, namely, we see the following meanings in the text of the program:
pulse t1 - the duration of the text cannot be determined; functions from standard libraries are used, but for speeds of 16 MHz the number of commands of 2.54 * 16 = 40 commands for writing 1 to the output register should be sufficient - satisfies the requirements;
pause after t1 - 30 microseconds> = 21 microseconds - meets the requirements;
t0 duration - 15 µs> = 8.71 µs, 15 µs = <25.4 µs - meets the requirements;
pause after t0 - 60 microseconds> = 21 microseconds - meets the requirements;
t2 duration — 60 microseconds> = 87.12 — does not satisfy the requirements;
pause after t2 - 300 microseconds> = 210 microseconds - meets the requirements.
That is, we see that the duration of the fixation pulse does not ensure reliable operation of the second selector and the operation of the device as a whole is not guaranteed. How can this be, because this device is proposed for implementation and such an error should be immediately detected? The explanation for this is possibly different - we calculated the limits for the worst cases, and in a typical case everything is good, because the information does not change so often, the update may be invisible if sometimes the device will still work, in fact, the pause after t1 should be not 21 microseconds, but 3 * 72 = 210 microseconds, therefore, when transmitting zeros, a decrease in the initial level is observed and, accordingly, the conditions for triggering are facilitated, and, finally, we (I) are just missing something somewhere.
To test some of these assumptions, we conduct an experiment on modeling this scheme in the Proteus program (by the way, I take the opportunity to recommend it, it is really quite good, although not without oddities) and we see that the experiment confirms the correctness of our calculations - with a duration of t2 of 60 ÎĽsec fixation data does not occur, and if there are zeros in the information flow before the fixation pulse, the pulse duration is sufficient to 75 microseconds, and not to 87 microseconds, as was estimated, and as it is, if there are units before the pulse, fix ation. However, 60 microseconds is never enough.
A careful study of the repository leads to another explanation - if we watch the release of the library in the archive, then it costs 100 microseconds, which fully meets the requirements, but if we look directly at the available files, we see 60 microseconds and the message that this is a two-month fix limitation. I'm not very good at GitHub, maybe that's the way it should be, but in my opinion, this is just a sign of negligence.
So, I showed that the program will not work stably with the data on the circuit of the selective chain, but the attentive reader will immediately notice that I could not detect this discrepancy by looking at the circuit (as I stated at the beginning of the post), without the program text, so even doing all the necessary calculations in mind. "I do not believe" - ​​attentive reader will exclaim and will be absolutely right. In addition to this discrepancy, which may not appear on specific instances of devices (there is, in addition to the maximum, a typical value, and everything will be fine with it), there is another drawback in the scheme, which is much more significant.
It consists in the fact that the fixation impulse, in addition to its main function, will also make an entry in the shift register, since the device will interpret its termination as a strobe. As a result, 0 will be written to the first digit of the chain, and all other data will be shifted and we will see something completely different from what we expected. This circumstance has nothing to do with pulse durations and is irremovable. This is what struck me.
, 8 , 7. , , , , . , , . ,
static void sendByte(uint8_t pin, byte data, byte n = 8)
with default parameter 8, and the output of the last section could be done with explicit parameter 7, but for some reason this was not done.
It should also be noted that such a decision still leaves us with no opportunity to ignite a point in the first indicator and it would be nice to indicate this directly somewhere.
Summarizing, it is safe to say that the program given by the developer is in the archive, that in the expanded version with the scheme given by the developer, it cannot and will not work properly. What minimal changes in the scheme could be made (obviously, no changes in the program will allow us to control the first digit of the first indicator) so that this program can function without errors? Fortunately, changes are possible. It is enough just to add an inverter to the circuit of the second selector, either as a device or as an inverting transistor circuit (in this case it is necessary to recalculate the duration, based on the parameters of the transistor), and the above problem will be successfully solved. At the same time, during the transmission of the pulse t2 (and not at the end), a rewrite signal will be generated that will record the correct information, and after the end of the pulse the device will receive a strobe, distorting the information in the register, but at the outputs, due to the characteristics of the device, it will not affect . I hope the developers did just that, they just forgot to update the circuit on their site (which again speaks of negligence) and the devices they produce continue to delight electronics lovers with their flawless work.
Nevertheless, I would like to strongly warn young engineers against such decisions, when primitive duration selectors, especially with significant time constants, work directly at the input of the logic element, and even more so at the clock input. The fact is that a long front entails a lot of inconvenience if you do not take special protection measures, ranging from picking up the noise of the whole environment at a point near the switching voltage and ending with potential damage to the device as a result of prolonged exposure to intermediate values ​​of the input voltage and the associated end-to-end current input cascade. Modern devices are practically not subject to this scourge, but in the years of my youth this happened.