# D flip-flop

So in the last video, we built this D latch
Which has the enable input as well as the d input and whenever the enable input is high then
the output here the Q output is just equal to whatever the d input is and then there's also an
Inverted Q output or a complimentary or negative
Q output that's always just the opposite of what what q is
And then if the enable is not high then it doesn't matter what happens with D input?
So this is the D latch I've made a slight
Modification from the video from the circuit we had in the last video which is that on the inputs I've added these leds
And so if we're not pushing the button then this this is going to be
very close to zero volts
you know it will sink a little bit of current through the led but not enough to light the led up and
then when we push the
the button then we're connecting 5 volts to the input and so the input goes [from]  volts to 5 volts and then
We also have 5 volts that can flow through the led in the [end] this resistor
Just limits the current through the led so we don't burn up the led and so I just added these leds so that
We can see what the inputs are so to get a better sense of how this works we can look at a timing diagram
Which is this here which shows the values of the inputs the enable input and the d input over over time?
And so it shows them going high and [low] and we can see
We can kind of think through what's going to happen to the Q output
And I've left off the the inverted Q output because I think it's just kind of distracting, but in fact I'll remove this led
But you know just keep in mind that the you know the d latch it also has an inverted output
That's just always going to be the opposite of whatever q is
So remember the way this works is whenever the enable input is high
The Q output is just going to follow whatever the d input is
So just for these periods of time where the enable is high the queue is just going to be what the d is
So the queue here is going to be just what the d input is
and then the rest of the time when the cute when the
The enable is low the Q [output] is just going to stay whatever it is. So if it starts out off for example
It's just going to stay off here. It's off and so it's just going to stay off and
Then he and then here it's just going to stay off and this case is going to stay on and in this case
We'll stay off
So it doesn't matter you know during these periods what the enable or what the d?
Input is doing as long as the enable is off. It's just going to stay latched where wherever it is
So if we want we can [just] verify that this works the way that we did we think it should
Yeah, and it looks [like] it works the way we would expect so that's great and hopefully makes sense to you how that works but?
often times in more complex digital logic circuits
We've got lots and lots of different latches that are latching different values all over the place
And it's important to keep all of that in sync
and so typically there is a clock of some sort that is that are controlling the
Operation of all the different components and keeping everything synchronized
And it's important what you want is you want to be able to latch the values at?
a particular point in that clock cycle
Not necessarily just transparently passing through the values anytime
That clock signal is on and so to do this we'll look at a variation of the the latch which is the flip-flop
so with the d latch [anytime] it's enabled the input D is going to be output at Q and
So if the d input changes while the the latch is enabled, then you'll see a change at the output
But oftentimes what's more useful is to think of the instead of having an enable input having a clock input?
And then only outputting a change at that
Just at the point where the clock transitions from low to high and so this clock is really more of kind of a heartbeat
That says at these specific points in time
That's when I want the the latch to latch in or change its value and so for example at this point here where the latch?
Transitions from low to high that means we want to take this value right here at D
And we want that as our output, so that's a high output
so if we were already low, then we [would] transition [to] high at that [point] there and
Then we want to not do anything so even though the d input changes while the clock is still high
We don't care we all we care about is when the next clock pulse is which the next pulse from low to high
and then we want to take this value [and]
That happens to be high and so in this case the latch won't switch
And so it'll stay high here
And then we say we don't care about anything that happens even though it goes low, and this is still high here
we don't do anything because all we're doing is we're looking for that next transition from low to high [that] next clock pulse and
that happens here where d is low and
At that point in time, we'll switch
[we'll] switch the latch off and then again. We don't care what happens here. You know it goes [hi] again. Whatever, we don't care
we're looking for that next clock pulse where it goes from low to high and
In that case the d input is high and so at that [time]
We will switch the the latch back on
[and] in this case. There's no more transition
So we just stay high and so you'll notice [this] is [a] very different
Output than we got with with this scenario where we're using the enable
But it turns out. This is actually far more common. [it's] far more common to want to only switch at a particular instant in time
So question is how do we build a circuit that does this?
well one way would be is if we could build a circuit that could take this clock input and turn it into
Essentially something that would be useful as an enable input for normal D latch
So to kind of give an example of what of what I'm talking [about] there is
You know imagine we could have we can take this clock signal and get a signal out and put it into some circuit
And then get a circuit get a signal out that had these really quick pulses
Just at this point where the clock transitions from low to high
so if we could build a circuit that could do this then we could use this new
signal here at the top as an enable for just the classic D latch that we've been looking at so far and
As long as this pulse is is really really narrow then effectively. We're getting an Instantaneous or or as close to instant aces
we can kind of read out of what d is and
Transitioning just at that point and so if you look at this, this is kind of like your enable in a regular D latch
and then if you have that enable and that d you put that into a regular D latch then this is the output you'll get
And hopefully hopefully that makes sense and so you can see if we could somehow take this clock signal and detect these rising edges
With a circuit that [we'll] call an edge detector and detect those edges and then and then turn that into a pulse like [this]
Then we can build what's called the d flip-flop?
Which is a circuit that will take you know that will start with this clock and only transition at these points
Where the clock transitions from low to high?
So now the question is how can we build an edge detector circuit that you know takes this clock signal and now puts this this?
these pulses
Well, there's a couple ways to do [it] and one is is this
sort of interesting looking circuit here
And you might be wondering well, you know what does this do we've got
one input and one output and so you know the idea is that somehow a
Rising Edge here is going to result in a pulse over here, but you might be wondering well
We've got this inverter going into a nand gate [as] we have an input here going through an inverter
and
You know one side's going through the inverter the other side isn't so if you had you know let's say you had a zero over
here
Well that's zero you know if you have a zero coming in to zero over here
It's going to be a 1 over here, and then you know the and gate and gates going to be a zero
It's a you know
It's only going to be a 1 if both inputs are ones, and you might wonder well
How does how do you get to both inputs being one?
I mean if
if this is a zero then this is going to be a 1 if this is a 1 then this is going to be a
zero because we're inverting this and so this you would think this an gate would never turn on and
You'd almost be right, but what happens is if this signal is a zero?
you know we have this situation here where we have a zero coming in and
A one here into the and gates going to be off, we're gonna have a 0 coming out
but when this input
transitions from a zero to a one
This instead of being a zero is going to be a 1 and
Then you would say well
Then this would be a zero because we're inverting but the inverter actually takes a small amount of time to switch [States]
so when this input changes from zero to one
We've got a 1 here immediately
But the inverter doesn't switch off for you know a few nanoseconds. You know very short amount of time but but enough time
That for a very brief period of time we've got a 1 and a 1 coming into [the] [damn] gate which turns the end gate
on
But then of course very quickly the inverter [switches] off this one turns to a zero and now we've got a [one] [in] a [zero]
And then of course the and gate turns off, and so it turns off almost immediately
But now we have this pulse and we get this pulse [anytime] this input
Transitions
Low-to-High we'll get a little pulse that comes out, and that's exactly what we what we wanted here in order to detect this
rising Edge on this clock signal
so that's one way of
Building an edge detector, and I would actually encourage you to try building this yourself
[there's] a simpler way though that I'm that I'm going to do because you know this is this is going to require [a] nand gate
And and you know an inverter
potentially multiple inverters you might find if you try to build this you need to stack up three or five of these inverters in a
Row just to get enough of a delay so [that] you get a big enough pulse
But you know you can give it a try
But a much easier way to do it that doesn't require adding more chips here is to use a resistor and a capacitor
and so we can use a little circuit like [this] where we go into a capacitor [and]
Then you know through through that capacitor, and then we have a resistor here
That just goes to that goes to ground
And so you're wondering well what what does this do?
Well in this case if you've got this the signal that comes in that transitions from low to high like this
When it's at zero, we've got you know zero volts across this capacitor and the capacitor is discharged
You know because the ground here is going to be at zero. So you know you've got zero volts over here
So that's not not very exciting, but then when it turns on the capacitor
Is going to start to charge and as it charges?
There's current flowing into it and of course if you've got current flowing through here
You're going to have a higher voltage here across this resistor
And so as soon as as soon as this turns on this is going to jump up to five volts
You know so I mean this is a five volt transition here
But very quickly that capacitor is going to charge it's going to charge through this resistor and as the capacitor charges
less and less current is going to flow into [it] until
The capacitor becomes fully charged and when the capacitor is fully charged then no current is going to going to flow through here
And we're going to be back down at zero, and so what happens is this turns on very quickly?
And then it slowly or maybe not so slowly depending on on how fast this is
but it discharged or excuse me the charges the capacitor charges and so that the voltage over here drops until
You know once the capacitor is fully charged you can have five volts on this side zero volts on this side. [nope] flows and
So you can see it is a pretty simple circuit to take you know this rising edge and turn it into a quick pulse
Because then this can stay high and this will just this will just be low
But you get this nice quick pulse
Right at that rising Edge. Which is which is what we want so question is how how quick is this pulse?
And you know I'm not going to go into driving all the math, but you you can calculate the width of this pulse
By just multiplying the value of the capacitor and the resistor if you multiply the value of the capacitor and resistor
It'll tell you essentially where
You know the point or how much time it will take for this capacitor to get about two-thirds of the way charged
Which will give you a pretty good sense [of] how?
Of how short this gap is
So let's build this I've got a resistor [in] a capacitor here. This is a
And then this is a 1k
Resistor and so if we want to figure out this this time constant to figure out how how big this pulse is
We just multiply these together, but of course we first we've got to get them into standard. You know Farad's and in homes, so
[0.1] Micro Farad's is going to be 0.1 times 10 to the
minus 6 and
Then 1 1 k of course is just 10 to the third and so if we take 0.1
Times  to the minus  multiplied by 10 to the third we get 0.1 times
10 to [the] minus third
Which is going to be equal to?
0.1
Milliseconds because [if] you're multiplying Farad's times kilo Ohms in this sort of circuit you end up with seconds so 0.1 milliseconds is
How long this this gap will be so for our purposes we [can] say that you know K 0.1
Milliseconds is you know pretty pretty much Instantaneous. Which is what we're going for here. We're looking for you know when this clock transitions
We just sort of take an Instantaneous read of this d input so as long as in our circuit this [d] is in
transitioning anywhere near
You know every 0.1 milliseconds
Then we'll be fine and of course in this case. We've got a button here
I'm not pushing it any faster than that, so we'll be fine
So let's go ahead and and build this so right now. I've just got this little jumper here between the
enable button and the you know enable part of the circuit so I'm going to take that jumper out and
We will put this capacitor in
So that's this capacitor so from the button
We're going to get this pulse
And then of course we need this 1k resistor here going to to ground so for the capacitor to charge through
So let's give it a try and see if it works the way we would expect
And it looks like it works. Just the way that we would expect it to which is great and
you [know] definitely different than different than the the D latch and
So that's the d flip-flop and if you look at the d flip-flop?
it's basically very similar to the D latch in fact it's
Identical except that instead of the enabled you have some sort of pulse detector and in this case
I'm using a resistor and capacitor
[you] could use this you know this weird and gate inverter thing if you wanted to
But something that's going to detect the pulse for that clock pulse
and so instead of having an enable input in a d input you have a clock input and a d input and in the
Sort of block diagram for that the clock input is usually indicated with a little triangle here that tells you you know
That this is in triggered
And so that's the d flip-flop, and it's probably the most common way of storing data in a digital logic circuit