You are on page 1of 4

FPGA Lab 3

Steven Spohrer and Ryan Hensley


(Dated: February 19, 2018)
In this lab we focused on using and creating counters within the FPGA software. We learned how
to utilize binary counters in the Quartus IP catalog to do things such as create timers or convert
binary and hexadecimal to decimal. We also created our own counter using many simple gates and
D flip-flops (turned into T flip-flops). Lastly, we familiarized ourselves with the Quartus simulator
and realized how it is a powerful tool for debugging our circuits and checking our logic.

I. LAB 3 50.37MHz ± 0.56MHz. If we had measured for a longer


time, we probably could have gotten closer to 50MHz.
In this lab, we used synchronous logic to build digital Then we tested the simulator and ran the “Simula-
counters and we learned how to use the simulator to see tion/Run Functional Simulation” function. The output
the exact logic values held by different bits in different is formatted in the way that the 7-segment displays read,
parts of the circuit. with the first tick being 1001111 and next being 0010010.
For a more interesting simulation, we also had KEY[0] be
simulated to be pushed down for a small period of time.
During this time, the clock was shown to stop count-
A. Lab 3.1: Binary counter, simulator
ing, and held a value of 0 until we “released” KEY[0], at
which the counter resumed counting starting at 1. This is
1. Procedures shown in FIG. 2, with the highlighted column being the
second clock tick immediately after releasing KEY[0], so
In this first part of the lab, we created a 40 bit counter it displays 0010010.
from the megawizard to test the simulator functional- By watching the placed output pin, we can see the
ity of Quartus. The counter had sclr (set clear), clock, counter very simply displaying an increasing 1, 2, 3, 4,
and cin (count in) inputs, and a 40 bit data output, to ..., value in hexadecimal in FIG. 3. In FIG. 4, which is
which we connected a series of 7-segment displays, and a picture of the lowest five bits of OUT[39..0], it can be
connected key inputs to the sclr and cin. Because the seen that this is just an increasing binary number across
key inputs are always high unless pushed, we put a NOT the 40 bits of the output.
gate on the sclr. Next, to use the simulator, we created Next we looked at the value of the register bus directly,
an output node. This is not necessary for the opera- setup as seen in FIG. 5. Before running the simulation it
tion of the circuit, but it is an easy way to view the just says UUUUUUUU, but once the simulation is run,
counter output in the simulator. The other method has it properly displays the 40 bits of the output.
us search for a node in the memory which starts with Finally, we ran a Timing Simulation instead of a Func-
a long name starting with the name of our counter, like tional Simulation. The Timing Simulation simulates all
”mf count1:inst2—lpm...” (the registered group). This the various internal delays in the FPGA chip, and so
design can be seen in FIG. 1. the register output is slightly delayed from the clock
Upon opening the simulator, we used the node edges, and this was precisely shown in the graphs. We
finder to find both our created output node and the looked closely at the start and end times of a random
mf count1:inst2 instance of the output terminal in mem- count tick of the number 0002. Given the clock period of
ory, and then also the KEY[0] and KEY[1] inputs. Then 20ns, the 0002 tick came into the register from 33.26ns to
we set the view range to an end time of 10 us and the 53.26ns (exactly 20ns), but appeared across the output
clock period to 20 ns. Finally, we set all the data to dis- from 37.27ns to 57.27ns (a delay of 4.01ns). These delays
play in hexadecimal. To add onto the tests we ran in the are from the internal circuitry of the FPGA board.
simulation, we also tried the Timing Simulation in the
end.
B. Lab 3.2: Binary-coded decimal counter

2. Analysis and Discussion 1. Procedures

We first wanted to check that the clock was actually By utilizing the counter function from Lab 3.1 we were
50MHz as was stated. We used a stopwatch and let it able to construct a timer. However, we first needed
count up for an arbitrary amount of time, then stopped to introduce the “modulus” attribute into our counters.
it by turning off cin using the key input. In 27 ± 0.5 The modulus attribute makes it so whenever the counter
seconds, the counter reached a value of 5074dca2, which reaches a certain number, the counter will reset back to
is 1,349,835,938 in decimal. This gives a frequency of 0 and the carry-out output pin will be 1 for one tick.
2

First, we connected our 50 MHz clock to the count in the 1-bit are 1, and the 3-bit similarly only alternates
pin on a 20-bit, modulus-500,000 counter. This creates only if all of the 2-bit, 1-bit, and 0-bits are 1.
one carry out pulse every 1/100 of a second, since the We started with a modulus-50M counter with an input
clock ticks 50 million times a second and the counter of 50MHz. This will output one c out tick every second.
sends one carry out pulse once every 500 thousand This single uptick enters a circuit involving four D flip-
counts. flops all turned into trigger flip-flops using XOR gates.
Next, we utilized a series of 16-bit, modulus-10 coun- To simulate the binary counting logic described in the
ters to display our timer in decimal numbers. Our above paragraph, each of the last three T flip-flops also
carry out of the main modulus-500,000 counter is con- starts with an AND gate. The first T flip-flop starts with
nected to the carry in pin of our first modulus-10 counter. cout and qOut[0] going into the XOR gate. If the out-
Thus, every 1/100 of a second the first modulus-10 put of the flip-flop is 0, then the XOR gate will activate
counter will go up one digit until it reaches 10, in which with the incoming cout high pulse, and the clock tick will
case it resets back to 0 and sends one carry out pulse. change the output from 0 to 1. In the time before the
The number the first modulus-10 counter has currently next cout pulse, the XOR gate will always be outputting
counted to (the q output) is input into the HEX2 hex- a 1 from the 1 of the qOut[0], until the pulse, at which
adecimal display. The carry out of the first modulus-10 time both inputs to the XOR will be 1, output will be 0,
counter is input into the carry in of the second modulus- and the flip-flop will change to a 0 output until the next
10 counter, thus making the second modulus-10 counter clock cycle.
count up one every 1/10 of a second. The outputs of the For the next flip-flops, we added an AND gate before
second counter to the hexadecimal displays and the third where cout used to be. The requirement here is that
counter mirrors those of the first counter to the hexadec- both cout and qOut[0] must be 1 for qOut[1] to switch.
imal displays and the second counter. This process of If that is true, then the AND gate will give a 1 to the
stacking modulus-10 counters is repeated until there is 6 XOR gate, and the logic in the above paragraph precedes
modulus-10 counters total, allowing us to track up to the just as before.
1000 seconds place on our timer. To convert to decimal, we combine the four qOut[3..0]
Lastly, we connected the the 50 MHz clock to all of bits into a divide-by-10 module. The output is named
our counters and connected a not-gated input VCC con- hexOut10[3..0] and the remainder is fed into a second
trolled by KEY[0] (the right-most push button) into the identical divider, for which the name is hexOut1[3..0].
setclear input pin of each counter. These two hexOut outputs are fed into two 7segment
converters for hex display output at HEX0 and HEX1.
2. Analysis and Discussion

2. Analysis and Discussion


After completing the process described in the ”Pro-
cedures” section, our final circuit appeared as in FIG.
6. Once the circuit was programmed into our FPGA The final circuit appears in FIG. 7. Note that “boucn-
board, the timer worked as expected. The HEX[2] dis- ter” instance in the screenshot was not used. The circuit
play seemed to be going up once every 1/100 of a second, worked as expected. Although the original instructions
the HEX[4] display was going up once a second, and we never told us to display in decimal, we wanted to display
used our phone timer to check that the HEX[7] display it that way, but displaying the binary output would have
went up after 16 minutes and 40 seconds (1000 seconds). been easily possible by just skipping the dividers. The
Pushing KEY[0] cleared the counter so it went back to end output just proceeded up from 0, going to 1, 2, 3,
0000.00 seconds. Pushing KEY[1] paused the timer on etc, until 15, at which the next tick reset it back from
its current value. 1111 to 0000. Pushing KEY[0] did not reset the output
hexadecimal display, it only reset the 50,000,000 counter.
Thus, holding it down temporarily halted the counting,
C. Lab 3.3: Counter from elementary gates as did repeatedly pressing faster than once every second.
Similarly, holding KEY[1] temporarily halted the count-
1. Procedures
ing also. If we wanted KEY[0] to reset the output display,
we would have fed KEY[0] into the reset input of all the
flip-flops.
For the final part of the lab, we built a binary counter
rather than a decimal counter. Internally, it counts up in
binary, and then we added onto the end of it a converter
to decimal. The internal logic is built from analyzing the II. CONCLUSION
patterns in the counting up of binary. Specifically, in the
sequence 0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, Over the course of Lab 3 we utilized and created many
1000, the 0-bit alternates every tick, the 1-bit alternates if different counters. We learned how counters can create
the 0-bit is 1, the 2-bit alternates only if the 0-bit AND timers and how using the modulus attribute of counters
3

can convert binary and hexadecimal numbers into dec-


imal numbers. Lastly, we created our own counter by
employing simple logic gates and a series of D flip-flops.
During Lab 3, we also discovered how useful of a tool the
Quartus simulator can be for checking and debugging cir-
cuits.

III. IMAGES APPENDIX

FIG. 2. Lab 3.1: This shows the simulator after depressing


KEY[0], which is connected through a NOT gate to the sclr
(set clear) input, and the first hexadecimal display. This will
reset the counter. Upon releasing KEY[0], it can be seen
that the next tick of the clock causes HEX0[7..0] to change
from 0000,0001 (a zero in the hexadecimal display coding) to
1001,1110 (one), then to the highlighted column 0010,0100
(two).

FIG. 1. Lab 3.1: The basic circuit layout of the counter,


including the placed output pin OUT[39..0]. This output is
not necessary in any way to the function of the circuit, but
gives the user an easy way to view q[39..0] in the Quartus
Simulator. Also note that the NOT gate between KEY[0] and
sclr, which is there because the KEY inputs are set to default
high, which would mean that the counter was set to always
clear each clock cycle unless KEY[0] was asserted. In 27 ±
0.5 seconds, this counter reached a value of 5074dca2, which
is 1,349,835,938 in decimal. This confirms that the clock set
by CLOCK 50 input is indeed 50MHz. Two more 7segment
displays below the border of the screenshot are needed to
properly display this number.

FIG. 3. Lab 3.1: The simulator showing the counting up of


OUT[39..0] pin at every clock rising edge, 0000, 0001, 0002,
...

FIG. 4. Lab 3.1: The bottom five bits of the 40 bit OUT[39..0]
pin, which are shown to just count upwards in binary, 00000,
00001, 00010, 00011, 00100, ...
4

FIG. 5. Lab 3.1: The simulator window showing the addition


of the register address output. Although now it only displays
UUUUUUUUUU, it looks like FIG. 3 and 4.

FIG. 7. Lab 3.3: The multi-stage output of the binary


counter. The modulus-50,000,000 outputted a single high tick
every second, which was fed into a circuit of AND gates, XOR
gates, and D flip-flops. This second part of the circuit com-
pletes the binary counter, with qOut[3..0] representing a 4-bit
binary number, increasing by 1 every second. The d10 mod-
ules next converted the 4-bit binary number into a 2-digit
decimal number which counts from 0 to 15. This is finally fed
into the 7segment display module for display on the hexadec-
imal display.

FIG. 6. Lab 3.2: An input of 1 controlled by KEY[1] is


counted in the modulus-500000 counter for every tick of the
50 MHz clock controlling all the counters in the circuit. The
carry out of the modulus-500000 counter (which outputs a
pulse every 1/100 of second) is input into a series of modulus-
10 counters that each input the previous counter’s carry out
so that their q outputs represent a decimal timer. Each of
their 4-bit q outputs are displayed on the hexadecimal dis-
plays HEX[2]-Hex[7] so that a timer with 1/100 of a second
precision and range to the 1000s seconds place is displayed on
the FPGA board.

You might also like