Hacker
Professional
- Messages
- 1,044
- Reaction score
- 834
- Points
- 113
The content of the article
In the beginning there was a beat
A couple of months ago, we started to study digital circuitry from the base unit NOT. We will not change traditions this time either, especially since the block is actually not as simple as it seems at first glance. Let's take a couple and connect them in series.
Someone might say that this is a completely meaningless scheme, because the output signal is always equal to the input. Indeed, applying logical negation twice, we get the original statement. If you are not a "white" hacker, you are a "black" one. Not "not white" - still "white". It looks like a useless verbal balancing act, but everything changes if in the circuit above you try to close the input to the output.
Now, if we have a high logic level in the right half, it will always be low in the left half (and vice versa). In other words, the circuit acquires the property of bistability and takes on only one of two possible states. Just like a bit of memory - or a variable boolin C / C ++.
For clarity, you can assemble the circuit on a breadboard. Any inverter will do here - for example, the 74HC04B. These are six logic gates NOTin a DIP-14 package (three bits of information, Karl!). However, as you already understand, the same function can be implemented in several ways, so here our choice is almost unlimited.
A simple example (as in the picture above) allows us to make sure in practice that the microcircuit works as we expected. To blink the LEDs and switch states in such a memory cell, it is enough to touch the ends of the wires (from the ground or power supply) to the corresponding pins of the microcircuit. This may not sound very exciting in itself, but some thought along the way should lead to interesting ideas.
Some uncertainty
At some point, your inquisitive hacker mind probably wondered - what if you apply the same voltage to both halves at the same time ? And what state will our circuit be in after? In essence, this is akin to division by zero in mathematics (or programming) - a forbidden operation, the result of which is undefined. Paradoxically, even such a property can have practical applications! The engineers at Intel are well aware of the circuitry (who would doubt it) and the random number generator built into their processors works exactly on this principle.
Now the process of operation of our circuit resembles electromechanical computers of the mid-40s of the XX century, when the signals inside the device were switched using relays and switches. Now is the time to add new capabilities and move from inverters to multi-input elements.
Simple RS trigger
Let's make minimal changes to our circuit and use additional inputs NAND. Let's call them nRand nS( not RESETand, not SETaccordingly, their purpose will be clarified in the future).
Both inputs can take two values, in total there are four options to be analyzed. Let's start with the base case nR = 1and nS = 1. In this case, the outputs Qand nQlonger have any value. Please note that if Q = 1, then the nS = 1result of the operation NANDwill be a low level, that is nQ = 0. And vice versa, if Q = 0, then nQ = 1both outputs in our circuit really take opposite values. In other words, if one of the inputs of the gate NANDis in the state of logical unity, then the signal at the output is defined as the inverse of the second input - just like with inverters a little earlier! Thus, at nR = 1and, the nS = 1circuit retains its old state and the outputs are not updated.
Now let's consider option with nR = 1and nS = 0. Since NANDthere is at least one zero at the input of the top element , its output in any case will be equal to a logical unit. Hence, Q = 1and therefore nQ = 0. Similarly, for nR = 0and, nS = 1we can similarly infer that the state of the circuit will be completely opposite ( Q = 0and nQ = 1).
It remains to analyze the final variant, where both inputs are equal to zero at the same time. On an intuitive level, you can already assume that something is wrong here. Indeed, for, the nR = nS = 0result of the element NANDcannot be positive for any possible values of the additional input (I recommend checking against the truth table). Therefore, Q = nQ = 0this is the only case when our scheme "fails". In the future, we will be her best and will definitely get rid of this shortcoming.
But now is the time to stop and take a breath. The above was not the most trivial reasoning, and if you do not understand something, then this is completely normal. Sequential circuits are more difficult to understand, and that is why in the previous article we began our acquaintance with combinational logic.
Therefore, I advise you to re-read a few paragraphs above again. Moreover, this is a key scheme and later in the article many elements will be based on it. In general, the most correct way to understand any scheme once and for all is to take a pen and a piece of paper (or a stylus and a tablet) and consistently consider each of the possible options. I judge from personal experience - in your case, something else may work.
Practical use
I hope that now the principle of operation of the RS trigger is no longer a secret for you and you are already eager to apply the knowledge gained in practice. To do this, you need an IW4011 microcircuit, which contains four gates NAND(any foreign analog is also suitable).
Surely you are already familiar with such an unpleasant phenomenon in electronics as contact bounce. It manifests itself whenever sections of the circuit are switched mechanically: when the clock button is pressed, toggle switch, relay, and in many other cases. As a rule, they prefer to deal with bouncing in software: short-term changes in the signal (the moment of contact of the contacts) are simply ignored by the device.
This is a common and rather effective way: the implementation of a similar function can be found in Arduino (Bounce2 library). However, this method also has its drawbacks: signal processing has to be delayed for some time (on the order of several tens of milliseconds). In many cases, this does not become a big problem.
But what if the button click is handled in an interrupt? Or are we designing a keyboard for gamers and want the fastest response times? Yes, the "extra" milliseconds will certainly not bring you to the first lines in the ladders of popular games, but they will definitely provide some advantage. Let's try to apply an RS flip-flop here and draw such a circuit.
We use two gates NAND, that is, only "half" of the CD4011 microcircuit (K561LA7). Pay attention, this is not a standard tact button: a non-latching changeover contact works here, so exactly two outputs are switched at any time. The nice thing about these buttons is that when pressed, they emit a faint click, similar to the sound of switches in a mechanical keyboard.
Hardware solutions are good for their simplicity and reliability - the program can freeze or run out of available memory, while there are no such problems with triggers.
Advanced D-trigger
Having mastered setting and resetting the internal state in a simple scheme, let's move on to more interesting things. As in programming, we can now reuse pre-built logic blocks to effectively abstract away the increasing complexity of devices. It's like with code - once you have implemented the required function or algorithm, you can add them to the necessary places without worrying about the call stack and specific addresses in memory.
As you remember, the key inconvenience in the RS flip-flop was caused by the fact that when nR = nS = 0we received the same state on the direct ( Q) and seemingly inverse ( nQ) output. Let's try to get rid of this problem. To do this, we will explicitly block one of the signals if the other is active at this moment.
Now the state of the data input ( D) through a pair of gates NANDis applied to both inputs of the RS flip-flop, and the enable signal ( E) controls the moment in time when the state of the right half of the circuit can change.
You can go even further and combine the two latches into one flip-flop, which will be triggered only on the rising edge of the clock signal (transition from low to high). This is the most versatile scheme, since it allows you to instantly fix the state of all signals on the input lines, and only then proceed to the formation of the result using combinational logic.
Practical use
In the 4000 series, a pair of D-flip-flops is implemented by the CD4013 microcircuit (domestic counterpart - K561TM2). You can see the purpose of its conclusions in the diagram below. Please note that, in addition to the data inputs and the clock signal, there are contacts for asynchronous reset and setting the internal state of the trigger.
Asynchrony means that the event occurs instantly, without waiting for the next clock signal. Conversely, updates from the data line occur synchronously with changes on the line CLK.
Thus, by combining the CD4011 (561LA7) and CD4013 (561TM2) microcircuits, you can assemble a circuit with a clock button and an LED, which will remember its state. The first press will make the LED light, the second press will turn it off. At the same time, we don't even need a microcontroller and we won't write a single line of code!
Combination lock on registers
Now it's time for more serious things. I assembled a combination lock on the 561 series microcircuits on a breadboard. You already know some of them, we will analyze the other part in the process.
First of all, you should decide on the input. There are six buttons for the first six digits 1 - 6 (no longer fit) and a reset button. The signal from them is fed to the RS flip-flops, which helps to cope with contact bounce. Next, we have four K561TM2 microcircuits, this gives us eight bits of information about the state of the device.
Please note that the first group of D flip-flops are connected in series: the inverse output through the gate NANDblocks the clock signal of the next one. Thus, until the first digit of the key is correctly selected, the rest of the circuit will not respond to pressing. In addition, the inverse output of the trigger itself is fed to the same gate, so that as soon as the desired button is pressed for the first time, the block is turned off and then the internal state of the D-trigger does not change anymore.
A pair of D-triggers U3Aand deserves a separate consideration U6A. They share a common clock signal, and the data input of the second depends on the output of the first. Since initially all triggers are reset to zero, even after pressing the required button once, the output of the second will not change and the last of the triggers will still be blocked (in other words, zero will U3Amove from to U6A, but globally it does not change anything).
However, the first register of the pair will still change its state, since upon arrival of the clock signal, its data input contains a logical unit from the inverse output of the second. This means that at its output there will already be a high level and we need another clock signal (pressing the button) to push the "one" into the second register. Thus, we can conclude that one of the buttons must be pressed twice in a row, after which the last D-trigger is unlocked.
By the way, three-input valves NANDto enable the supply of a clock signal to the D-flip-flop are contained in the K561LA9 microcircuits (three pieces per case).
Of course, if you take such a scheme separately, there will be a significant flaw in it: you can pick up a key simply by going through all the buttons one by one. At some point in time, we will guess the first correct number, after which we will move on to the second, third, and so on.
Therefore, we need the ability to reset the state of all D flip-flops as soon as the allowed number of attempts has been exceeded. To do this, first of all, you need to organize the counting by registering the pressing of any digit. Therefore, I took a couple of four-way valves NOR (K561LE6).
A logical zero at their output is generated whenever any of the connected buttons is pressed. Let's combine the outputs of the gates using the NAND operation. In this case, the following rule will work: the inverter at the output of the logical element can be moved to all its inputs, but the type of the element itself will also change ( NORit goes into NANDand vice versa). Thus, NANDthe output can be considered as NOR, and the inverters at its inputs are next to the inverters at the outputs of the blocks NORand mutually annihilate. As a result, we get one large item OR, which is what our counter required.
By the way, the circuit of the counter itself looks like this.
It is built around three D flip-flops, so we have three bits and eight possible states. The inverse output of the first block is fed to the input, so that on the rising edge of the clock signal of the BIT0internal state, it goes from zero to one (and vice versa). In this case, the output is connected to the CLKnext one and every second switching leads to a change for BIT1.
Further, we only have to track the combination 101for five allowed attempts and send a reset signal if the last of the D-flip-flops storing our secret code is still not in the state of a logical one (the case of a correct combination).
The work of the entire circuit can be seen in the video. A successful code activates the timer on the KR1006VI1 microcircuit, which demonstrates animation on the LED scale.
- In the beginning there was a beat
- Some uncertainty
- Simple RS trigger
- Practical use
- Advanced D-trigger
- Practical use
- Combination lock on registers
In the beginning there was a beat
A couple of months ago, we started to study digital circuitry from the base unit NOT. We will not change traditions this time either, especially since the block is actually not as simple as it seems at first glance. Let's take a couple and connect them in series.

Someone might say that this is a completely meaningless scheme, because the output signal is always equal to the input. Indeed, applying logical negation twice, we get the original statement. If you are not a "white" hacker, you are a "black" one. Not "not white" - still "white". It looks like a useless verbal balancing act, but everything changes if in the circuit above you try to close the input to the output.

Now, if we have a high logic level in the right half, it will always be low in the left half (and vice versa). In other words, the circuit acquires the property of bistability and takes on only one of two possible states. Just like a bit of memory - or a variable boolin C / C ++.
For clarity, you can assemble the circuit on a breadboard. Any inverter will do here - for example, the 74HC04B. These are six logic gates NOTin a DIP-14 package (three bits of information, Karl!). However, as you already understand, the same function can be implemented in several ways, so here our choice is almost unlimited.
A simple example (as in the picture above) allows us to make sure in practice that the microcircuit works as we expected. To blink the LEDs and switch states in such a memory cell, it is enough to touch the ends of the wires (from the ground or power supply) to the corresponding pins of the microcircuit. This may not sound very exciting in itself, but some thought along the way should lead to interesting ideas.
Some uncertainty
At some point, your inquisitive hacker mind probably wondered - what if you apply the same voltage to both halves at the same time ? And what state will our circuit be in after? In essence, this is akin to division by zero in mathematics (or programming) - a forbidden operation, the result of which is undefined. Paradoxically, even such a property can have practical applications! The engineers at Intel are well aware of the circuitry (who would doubt it) and the random number generator built into their processors works exactly on this principle.
Now the process of operation of our circuit resembles electromechanical computers of the mid-40s of the XX century, when the signals inside the device were switched using relays and switches. Now is the time to add new capabilities and move from inverters to multi-input elements.
Simple RS trigger
Let's make minimal changes to our circuit and use additional inputs NAND. Let's call them nRand nS( not RESETand, not SETaccordingly, their purpose will be clarified in the future).

Both inputs can take two values, in total there are four options to be analyzed. Let's start with the base case nR = 1and nS = 1. In this case, the outputs Qand nQlonger have any value. Please note that if Q = 1, then the nS = 1result of the operation NANDwill be a low level, that is nQ = 0. And vice versa, if Q = 0, then nQ = 1both outputs in our circuit really take opposite values. In other words, if one of the inputs of the gate NANDis in the state of logical unity, then the signal at the output is defined as the inverse of the second input - just like with inverters a little earlier! Thus, at nR = 1and, the nS = 1circuit retains its old state and the outputs are not updated.
Now let's consider option with nR = 1and nS = 0. Since NANDthere is at least one zero at the input of the top element , its output in any case will be equal to a logical unit. Hence, Q = 1and therefore nQ = 0. Similarly, for nR = 0and, nS = 1we can similarly infer that the state of the circuit will be completely opposite ( Q = 0and nQ = 1).
It remains to analyze the final variant, where both inputs are equal to zero at the same time. On an intuitive level, you can already assume that something is wrong here. Indeed, for, the nR = nS = 0result of the element NANDcannot be positive for any possible values of the additional input (I recommend checking against the truth table). Therefore, Q = nQ = 0this is the only case when our scheme "fails". In the future, we will be her best and will definitely get rid of this shortcoming.
But now is the time to stop and take a breath. The above was not the most trivial reasoning, and if you do not understand something, then this is completely normal. Sequential circuits are more difficult to understand, and that is why in the previous article we began our acquaintance with combinational logic.
Therefore, I advise you to re-read a few paragraphs above again. Moreover, this is a key scheme and later in the article many elements will be based on it. In general, the most correct way to understand any scheme once and for all is to take a pen and a piece of paper (or a stylus and a tablet) and consistently consider each of the possible options. I judge from personal experience - in your case, something else may work.
Practical use
I hope that now the principle of operation of the RS trigger is no longer a secret for you and you are already eager to apply the knowledge gained in practice. To do this, you need an IW4011 microcircuit, which contains four gates NAND(any foreign analog is also suitable).
Surely you are already familiar with such an unpleasant phenomenon in electronics as contact bounce. It manifests itself whenever sections of the circuit are switched mechanically: when the clock button is pressed, toggle switch, relay, and in many other cases. As a rule, they prefer to deal with bouncing in software: short-term changes in the signal (the moment of contact of the contacts) are simply ignored by the device.
This is a common and rather effective way: the implementation of a similar function can be found in Arduino (Bounce2 library). However, this method also has its drawbacks: signal processing has to be delayed for some time (on the order of several tens of milliseconds). In many cases, this does not become a big problem.
But what if the button click is handled in an interrupt? Or are we designing a keyboard for gamers and want the fastest response times? Yes, the "extra" milliseconds will certainly not bring you to the first lines in the ladders of popular games, but they will definitely provide some advantage. Let's try to apply an RS flip-flop here and draw such a circuit.

We use two gates NAND, that is, only "half" of the CD4011 microcircuit (K561LA7). Pay attention, this is not a standard tact button: a non-latching changeover contact works here, so exactly two outputs are switched at any time. The nice thing about these buttons is that when pressed, they emit a faint click, similar to the sound of switches in a mechanical keyboard.
Hardware solutions are good for their simplicity and reliability - the program can freeze or run out of available memory, while there are no such problems with triggers.
Advanced D-trigger
Having mastered setting and resetting the internal state in a simple scheme, let's move on to more interesting things. As in programming, we can now reuse pre-built logic blocks to effectively abstract away the increasing complexity of devices. It's like with code - once you have implemented the required function or algorithm, you can add them to the necessary places without worrying about the call stack and specific addresses in memory.
As you remember, the key inconvenience in the RS flip-flop was caused by the fact that when nR = nS = 0we received the same state on the direct ( Q) and seemingly inverse ( nQ) output. Let's try to get rid of this problem. To do this, we will explicitly block one of the signals if the other is active at this moment.

Now the state of the data input ( D) through a pair of gates NANDis applied to both inputs of the RS flip-flop, and the enable signal ( E) controls the moment in time when the state of the right half of the circuit can change.

You can go even further and combine the two latches into one flip-flop, which will be triggered only on the rising edge of the clock signal (transition from low to high). This is the most versatile scheme, since it allows you to instantly fix the state of all signals on the input lines, and only then proceed to the formation of the result using combinational logic.

Practical use
In the 4000 series, a pair of D-flip-flops is implemented by the CD4013 microcircuit (domestic counterpart - K561TM2). You can see the purpose of its conclusions in the diagram below. Please note that, in addition to the data inputs and the clock signal, there are contacts for asynchronous reset and setting the internal state of the trigger.

Asynchrony means that the event occurs instantly, without waiting for the next clock signal. Conversely, updates from the data line occur synchronously with changes on the line CLK.
Thus, by combining the CD4011 (561LA7) and CD4013 (561TM2) microcircuits, you can assemble a circuit with a clock button and an LED, which will remember its state. The first press will make the LED light, the second press will turn it off. At the same time, we don't even need a microcontroller and we won't write a single line of code!

Combination lock on registers
Now it's time for more serious things. I assembled a combination lock on the 561 series microcircuits on a breadboard. You already know some of them, we will analyze the other part in the process.

First of all, you should decide on the input. There are six buttons for the first six digits 1 - 6 (no longer fit) and a reset button. The signal from them is fed to the RS flip-flops, which helps to cope with contact bounce. Next, we have four K561TM2 microcircuits, this gives us eight bits of information about the state of the device.

Please note that the first group of D flip-flops are connected in series: the inverse output through the gate NANDblocks the clock signal of the next one. Thus, until the first digit of the key is correctly selected, the rest of the circuit will not respond to pressing. In addition, the inverse output of the trigger itself is fed to the same gate, so that as soon as the desired button is pressed for the first time, the block is turned off and then the internal state of the D-trigger does not change anymore.
A pair of D-triggers U3Aand deserves a separate consideration U6A. They share a common clock signal, and the data input of the second depends on the output of the first. Since initially all triggers are reset to zero, even after pressing the required button once, the output of the second will not change and the last of the triggers will still be blocked (in other words, zero will U3Amove from to U6A, but globally it does not change anything).
However, the first register of the pair will still change its state, since upon arrival of the clock signal, its data input contains a logical unit from the inverse output of the second. This means that at its output there will already be a high level and we need another clock signal (pressing the button) to push the "one" into the second register. Thus, we can conclude that one of the buttons must be pressed twice in a row, after which the last D-trigger is unlocked.
By the way, three-input valves NANDto enable the supply of a clock signal to the D-flip-flop are contained in the K561LA9 microcircuits (three pieces per case).
Of course, if you take such a scheme separately, there will be a significant flaw in it: you can pick up a key simply by going through all the buttons one by one. At some point in time, we will guess the first correct number, after which we will move on to the second, third, and so on.
Therefore, we need the ability to reset the state of all D flip-flops as soon as the allowed number of attempts has been exceeded. To do this, first of all, you need to organize the counting by registering the pressing of any digit. Therefore, I took a couple of four-way valves NOR (K561LE6).
A logical zero at their output is generated whenever any of the connected buttons is pressed. Let's combine the outputs of the gates using the NAND operation. In this case, the following rule will work: the inverter at the output of the logical element can be moved to all its inputs, but the type of the element itself will also change ( NORit goes into NANDand vice versa). Thus, NANDthe output can be considered as NOR, and the inverters at its inputs are next to the inverters at the outputs of the blocks NORand mutually annihilate. As a result, we get one large item OR, which is what our counter required.
By the way, the circuit of the counter itself looks like this.

It is built around three D flip-flops, so we have three bits and eight possible states. The inverse output of the first block is fed to the input, so that on the rising edge of the clock signal of the BIT0internal state, it goes from zero to one (and vice versa). In this case, the output is connected to the CLKnext one and every second switching leads to a change for BIT1.
Further, we only have to track the combination 101for five allowed attempts and send a reset signal if the last of the D-flip-flops storing our secret code is still not in the state of a logical one (the case of a correct combination).
The work of the entire circuit can be seen in the video. A successful code activates the timer on the KR1006VI1 microcircuit, which demonstrates animation on the LED scale.