

SYNCHRONOUS: 4BIT COUNTER MODULO16 D FLIPFLOP
FourBit Modulo16 DFlipflop CounterAs we might say that the heart of a living person beats as time passes, even so a counter is a logic circuit that counts as time passes. The interactive circuit above is a fourbit counter that is designed to count from zero (0000) to fifteen (1111) as time passes. When it reaches fifteen, it loops back and starts again at zero; and so on forever (i.e. as long as time exists). Since our counter counts from the low value zero to the high value fifteen, we say it is an upcounter. Furthermore, we say it is a modulosixteen counter because there are sixteen numbers between zero and fifteen inclusive.
Designing counters using Data Latches can be a simple and straightforward process. For example, if you have n data latches, then you can design a counter that can count from 0 to 2^{n}  1: This means if you want to count from zero to fifteen, then you need to solve for n in the equation 2^{n} 1 = 15; which solves as n (= log 16 / log 2) = 4; which means you need 4 data latches.
DESIGN AND SYNTHESISGenerally, the problem statement will read: Design a circuit to count from zero to fifteen. Include a switch to enable pausing the circuit. As it is always a good idea to try to visualize what the question is asking, we will start our design by drawing a diagram using what we know. We know the following: if the current count is zero (0), then the next count must be one (1); the next count after one must be two (10); then three (11); then four (100); then so on until the count reaches fifteen (1111). Another detail we know is that we need to include a switch to enable freezing/pausing the circuit between counts. We can draw this sequence of numbers as shown in Figure 2 below. Figure 2 is formally known as a state diagram because each ellipse represents one of the states that the circuit can ever possibly be in, and each arrow shows how the circuit moves from state to state depending on whether the enable switch (E) is 1 or 0. The colored arrow shows that after the circuit has reached the number fifteen (1111), it will restart at zero (0000) and continue on from there.
Although a state diagram is very easy to understand, in order to use the state diagram to build a circuit, we need to transform the diagram into a table because it is easier to write the Boolean expressions from a table description of the problem (kind of like truth tables). We show the state transition table in Table 1 below. Notice that when E = 0, the circuit will not change state; only when E = 1 does the counting continue.
Just as we would in a truth table, we must label the bits of the state transition table so as to be able to derive Boolean expressions from the table. So we recreate the table as Table 2, with the bits labeled. We use lower cases for the present states and upper cases for the next states.
Once we have determined the state transition table of a design problem, the natural next step is to derive the switching expressions for the circuit. Before we do, however, we typically use a shortcut: decide on which type of flipflops to use. The explanation for why this shortcut is useful follows. First and foremost, you must realize that a counter is a sequential logic circuit; which means the output of the circuit depends not just on the input but also on the past behavior of the circuit. For example, if the present state of our fourbit counter is 1010, then we know that the previous state must have been 1001 and that the next state will be 1011; in fact, from Figure 1 or the interactive circuit above, you should see that if all you know of a counter is the value of the input signals Clk and E, then you have no idea what the next state is going to be. Now that we have established a counter as a sequencer, the next timesaving observation is to realize that all sequential circuits inevitably comprise flipflops and latches; otherwise sequencers could not store information. And since the set of flipflops and latches is very small (i.e. RS, D, JK, T), we could as well pick which flipflops we wish to use from the outset and design around that type of flipflop. Of course, the choice of flipflop is not so arbitrary when you recall that T flipflops are built with JK flipflops, which are built with D flipflops, which are built with RS flipflops, which in turn are the most basic useful storage elements (this list of relations is not exhaustive, but it is representative). In general, we tend to design with D flipflops. For instance, VHDL, Verilog, and all other major integrated circuit design software use D flipflops to build sequencers. There are many reasons D flipflops are preferred; one is because they simply clock the input signal without changing it (i.e. for all D flipflops, Q = D as time passes). So for example, if Y is the input to and y is the output of a D flipflop, then y will be equal to Y as time passes. You will develop a greater appreciation for this fact as we formulate the Boolean expressions for our fourbit counter. Having chosen to use Dflipflops to synthesize our fourbit counter, all we really have left to do is find the flipflop input equations. The input equations are actually the equations for the next states W, X, Y, and Z. We refer to them as input equations because W, X, Y, and Z are the inputs to the D flipflops (see Figure 1). These input equations are combinational circuits and so are easy to formulate: each next state variable is a function of E, w, x, y, and z. For output W:
For output X:
For output Y:
For output Z:
Rewriting the four input equations together, we get:
At first glance there seems to be no pattern but madness in these equations. After rearranging the equations, however, you should see how you can easily extend the techniques you learn here to create counters of any size whatsoever. So let's rearrange the expressions, starting with the least significant bit.
For Z:
For Y:
For X:
For W:
So the equations from least significant bit to most significant bit are:
Do you see the pattern yet? If we use D0, D1, D2, D3, …, D_{n} instead of Z, Y, X, W, …, ETC. as the next state signals, and we use Q0, Q1, Q2, Q3, …, Q_{n} instead of z, y, x, w, …, etc. as the present state signals; then the pattern for the input equations would be as follows:
At this point we can build the circuit. But before we do, let’s address the influence of time on the operation of the circuit.
At the beginning we said that a counter will run through a predetermined sequence of numbers continuously as long as time exists. What this means in practice is that the essential input to a counter circuit is a clock signal. In other words, a clock signal (Clk) must serve as the driving impetus moving the circuit from state to state. This clock signal is incorporated into the circuit through the clock input of the flipflop: all the flipflops are directly wired to the clock input switch Clk; the clock signal also serves to synchronize the flipflops. Now we add the circuit for Y, turning the circuit into a 2bit counter that counts from zero to three (2²1). We will add the X bit the same way. At this point you should see the pattern and how to extend the counter ad infinitum. This 3bit counter can count from zero to seven (2³1). Do you anticipate what block we will add to create a 4bit counter? A AND Gate, a XOR Gate, and a D Flipflop! Here is the 4bit circuit. Notice that in creating a 4bit counter, you have incidentally created a 1bit, 2bit, and 3bit counter along the way. What this means is you can connect blocks of counters to build larger counter. For this reason, the final circuit below includes an output carry bit. The carry bit allows designers to chain small counters to build larger counters. For example, a carry bit can be connected into the Enable input of another 4bit counter to form an 8bit counter. I am going to trust you to allow me to show you one final trick. Circuit 5 uses Dlatches instead of Dflipflops. Therefore, it is not really the same as the main interactive circuit at the top of the page. Can you guess the difference? For one thing Circuit 5 is cheaper to build, because latches are cheaper than flipflops. But that’s not the main concern. The problem with Circuit 5 is that if the Enable switch flickers OFF and ON while the clock signal is risen (i.e. ON), the counting values will change. That’s not supposed to happen. Therefore, unless you can guarantee that the Enable signal will not change while the clock signal is HIGH, use flipflops instead of latches. 
