hey everyone in this video I'd like to show you how to make a Fibonacci number generator and largest sim first if you're not familiar Fibonacci numbers are part of a sequence in which every term is equal to the sum of the previous two terms starting with the numbers 0 and 1 we add those together to get 1 then add 1 plus 1 to get 2 then 1 plus 2 is 3 2 plus 3 is 5 3 + 5 is 8 and so on I wrote a program in Python which generates Fibonacci numbers and I know
some of you might not be familiar with programming but that's okay too I really just wanted to have a series of steps that we can follow and then we can think about how you might implement those in hardware it's really quite a simple program and I'll just walk you through it step by step so first we have our function Pipo gen which takes in a single number in now the first thing we do in this function is we set three variables a B and C and we set them to the values 0 1 and 1
next we enter a loop which repeats n times and the first thing we do in the loop is we print the value of a which will first be 0 then we update C to be equal to a plus B then we update a to be equal to B and B to be equal to c essentially pushing the values up through the variables a b and c so first we'll print 0 then we'll take 0 plus 1 and put that into c which will be 1 then we say a equals B which will be 1 B
equals C which is 1 and then we do it again we print a which is now 1 with an update C to be a plus B which is now - now we push our variables up so B equals C or rather a equals B first which is 1 and then B equals C which is 2 and then we just do that again and again so this will then be 3 and we update a and we update B then this will be 5 update a and we update the and on and on it goes so let's
just run this program to make sure that it actually works alright we've got 0 1 1 2 3 5 8 all right looking good I think it works so how can we implement this in hardware well since our program uses three variables I'll be using three registers registers really are basically just variables they store data so let's label them just like in our program a B and C okay first we want to initialize our registers so that's easy enough for register a we will simply pulse its reset line all right I'll bring that out here
and we'll do stuff with that later however we're not going to actually do that for register C even though we do that in the program the first thing that we do with C is actually set it to something a plus B which will be guaranteed to be initialized to known values so all we have to do now is to set B to the number 1 and do that I'll be using a constant value basically we're hardwiring the number 1 into the circuit now we can't really plug in the number 1 in to register B directly
and why that is is because we will also be putting in C into B later since we want to push there the numbers up through the registers and doing this will create a conflict on the input line for B so instead of that we'll be doing it a little more indirectly through something called controlled buffers so normally if we attempted to drive conflicting signals onto the common line you will get an error however with the controlled buffer what it does is it actually decides whether to actually drive the signal onto its output line so when
it's bottom input here is low it is not allowing the signal through but when it goes high it does allow the signal through and so that way we can in a sense select among different inputs which ones we want to drive on to the output line for B's input line we're going to be using two buffers one of them is going to be selecting the input number one and the other will be allowing value of C to go through like this so that way when we're setting up our circuit we control this buffer so that
it can allow the number 1 to go into B but during the main loop of the circuit we actually enable this buffer to allow the value of C to go into B how we're going to be controlling these buffers is through a memory circuit called the SR flip-flop sometimes also called the SR latch and how it works I'll set it up real quick with a couple of buttons here it is that you can impulse when you pause this line it will remember to output the number 1 even when the original input has returned to zero
and if you pulse this line it remember to output 0 even if the original input has returned to zero so it simply set and reset you can set it to 1 you can set it to 0 now since we want to do when the circuit first starts up is initialized B to the number 1 we're going to be using the sort of inverse of the SR latches output and we'll feed that into this buffers control input so by default when the circuit turns on we will be driving the number 1 on to this line and
later we will set the SR latch so that we can drive the output from C on to B's input line all right let's take a look at our program again now in the main loop we are updating C with the value of a plus B therefore I'll be placing an adder circuit all it does really very simply as it takes in two inputs and it outputs the sum will feed that directly into see now registers do not update constantly with the value that is on their data input line I'll show you what I mean so
here I'm inputting you know some 32-bit data into my register but you notice that it's not actually updating with the value registers normally will only update with the value that's being drip to their input line when their clock input is risen from zero to one like this so what I'll be doing is I'll have line to all the different clock inputs for the different registers I'll take them all the way out to the left side here and then I will arrange them like this so what I'll be doing now is I'll be placing pull resistors
on each of these clock inputs and why that is is because we kind of aren't really driving a signal on to these lines and the way that the clock inputs work is they really only update the register when the signal rises from 0 to 1 however if we aren't sort of setting a default value for them with poor resistors we would be raising them from undefined to 1 and that is not guaranteed to work and before I forget let me bring out the inputs for our SR latch as well so when I bring up my
circuits control inputs like this it was really just for organization I've always found that having them out here like this lined up grants me easier access to control everything all right I forgot one last thing to do over here I need to make sure that register a can be updated with the value of register B so let's bring a line over here to register a alright I think now our circuit is ready to be controlled but how exactly can we do that well let's try doing it manually first I'll set up some buttons over here
and then we can manually control all of these lines the first thing we want to do when our circuit turns on is make sure that this SR latch is in its default state so let's find that line and we will pulse it by pressing this button okay next let's look at our program and see we want to reset a and set B to 0 and 1 so you reset a by pulsing this line alright and we will set B by pulsing its clock input and note that of course the value being driven on to B's
input line is the number 1 so we pulse this line and B has now been updated with the number 1 now we're going to step we're going to skip this step because we actually don't need to do that since the very next thing we'll be doing with C is updating it to a plus B so now let's pulse this line alright and notice that control on B's input has been switched to select C's output now we're going to update C with a plus B so we find C's clock input and we have that next we
want to move B into a equals B so we pulse A's clock update and we pulse right with poles yet B equals C so we find B's input my pulse step ok and then we just do it over and over again so let's see C and then a and B C a base which these three we're just going to do c a b c a b c a b over and over again and as you'll see we are generating Fibonacci numbers this way except that's very confusing isn't it if you're not familiar with hexadecimal numbers
this is how to write 13 in hexadecimal so to make things easier for us humans let's add a probe it will allow us to see what the value is written in normal decimal numbers so now we can semi automatically generate Fibonacci numbers but wouldn't it be really nice if there was just a circuit that could do all this stuff for us automatically well lucky there is let's get rid of all this junk or you will not be needing it to automatically control these lines I'm going to be using something called a demultiplexer let me just
show you real quickly what it does I'll set one up ecology multiplexer works as it takes in an input and it allows you to select from one of n outputs to drive it on so right now we're outputting on the first output but if I change the control input on the demultiplexer we can change where the output goes to so we can drive it on the first output we can do the second third or the fourth by just changing the de multiplexers control input or what's it called select that's what it's called I have replaced
the input pins on the de multiplexer select input with a device called the counter which simply counts by one every time it's clock input have been risen from zero to one this is going to be kind of the basic functionality behind the control circuit so let me just set up a few more things I'll get rid of this and I'll move this back a little bit in fact we're going to use something called the clock it's really self-explanatory it's a clock yeah it ticks on and off at a regular interval so what I'm going to
do now is add something called the inverter or the not gate and all it does simply is it takes in whatever input it's receiving and it outputs the opposite where I'm going to use this is let me move back here on the counter on our demultiplexer and why that is is we want the clocks or we want the counter to update to the next output line when the clock output is low otherwise it will send a signal and count up at the same time and we don't want that we want it to send a signal
then the count so it'll go like this another thing we want to do is add a sort of a gate on our clock signal to decide whether it actually goes through to our de multiplexing or not and why we do that will become apparent pretty soon but basically we're going to need two of these and we want only one of them to be active at a time and now how we're going to do this is we're going to use an SR latch and a NAND gate let me get it to the right size okay and
all the end gate does really it takes in two inputs and it basically just says if both of the inputs are on then the output shall be on otherwise the output shall be off or basically it says if this input and this input are both on and the output is on okay so we're going to plug in the lat the SR latch there and the clock signal here let's drag that out here for now okay so now when this SR latch is set on the clock signal we'll actually go through to the demultiplexer and when
it turns off well it won't so now we're going to want to use some controlled buffers these will allow us to drive the clock signals on to the control lines for our main circuit and why we're using controlled buffers instead of driving these lines directly is because that might create a conflict on these lines when we add more things later so let's find out which lines do we need we know that when our circuit turns on we want to set register a to zero by pulsing it's reset input so that's the first line and that
we also want to make sure that this SR latch is zero and that's the second to last line so I'll put two controlled buffers for now okay let's take a look at our program again we want to then load in one into B so that will be simply pulse it's some clock update input and that is the third line I'll put another buffer here and then finally to get things ready to enter our loop we will enable this SR latch we'll set it to 1 and that is done by pulsing the last line alright so
now let's connect our buffers onto these lines needed the first and the second to last all right plug that into there alright and then we needed the third line 1 2 3 and in the last line right there we go now we're going to plug in the buffers into the demultiplexer okay so the next thing we want is when we've sent out the last signal to enable the SR latch alright let's make sure that's right yep we want to disable control on this part of the control circuit by disabling the SR latch up here now
how we're going to do that is using something called a falling edge detector it's very simple all it does is when it detects that a line has gone from a high to low it will send out a very brief signal alright so how I built the falling edge detector is simply with a NAND gate and two not gates now to demonstrate how it works I'll just quickly add on an input and an output and then I will run the simulation step by step so you can see how the signals propagate so first let's bring the
input signal to high all right so the fog lights detector when this signal goes low like this what's going to happen is it goes through this inverter and it will input one on this input and it will input zero on this input however signals do not propagate instantly so there will be a sort of a lag as it goes through this inverter during which the inputs on the end gate will both be high for just one moment watch so now let's get rid of this and we'll get rid of that bring you over here and
now what we want to do is when the third clock cycle is done when it returns back to low we're going to disable this SR latch so let's take a quick look at how this is going to work we'll pretend that we have some button somewhere that enables this SR latch and then we press it and goes one two three and it doesn't reach the fourth output it turns off when it gets to the end of three okay so let's take a look at our program again at this point everything should be set up we
want to now enter the main loop of the program so we aren't going to really need any special circuitry for this since we already have our probe set up to read directly from register a let's see so now I need to pull the line to set C to be a plus B and then we pulse a line to shift B into a and seem to be all right so let's figure out what those lines are we need to first pulse this one so that's the third from the last and then we can pulse this one
and this one so second and third fourth second and third okay so going to place one buffer for updating the register see and two more for shifting the registers seeing two B & B into a so let me just plug these in real quick all right now let's build another one of these d multiplexer control circuits you all right so we've got this one set up now you might notice that this demultiplexer only has two outputs even though we're going to be driving three different lines and why we do that is actually because we don't
have to drive these two lines on separate clock cycles as in we can actually shift C into B and B into a at the same time so these two we can actually do at the same time on the same single clock cycle which means that this part of the circuit or this program really only needs to have in a sense two instructions two separate clock cycles for it okay so let's find out which lines do we need again first we want to update C so we need the third from last line so I'll plug that
into third last one two three all right then we will need the third and the second right okay the third and the second there we go these two will be on the same clock cycle and now let's bring this over a little bit closer connect them like that all right now I just need to connect this into our clock device what's going to enable this control circuit is going to be the signal from the falling edge detector on the first part of the control circuit the part that essentially says when this part is done so
we bring this down and enable that so now we're going to add our power button which can also serve as a reset button I'll put that right here and all it's going to do is let's see want to have it enable this all right so I'll bring that over there we want to make sure that this is set to zero all right well maybe I can make it look near that way okay and we want to make sure that this is turned off and this is also set to zero everything else part of resetting or
initializing will be handled by the control circuits alright let's give it a quick test run Shelly let's bring our probe over here and bring our Fibonacci numbers over here so we know what we're looking for alright we can move that closer why not okay now let's turn it on and see what happens all right there we go looks like it's working it's making Fela not see numbers people it's a machine which prints Fibonacci numbers okay so I hope you enjoyed that if you like what you saw feel free to share it with your friends if
you want to see more don't forget to hit the subscribe button and click the little bell icon with that I'll see you in the next one