Lab 03
Introduction
Clock
Registers
Activity 1: Create a simple counter
Customized Speed Counter
module Counter #(
parameter int OUTPUT_BITS = 8, // width of main counter
parameter int CLK_HZ = 100_000_000, // FPGA input clock frequency (100 MHz default)
parameter int TARGET_HZ = 4 // desired output frequency (Hz)
) (
input clk,
input rst, // active-low reset
input up_down, // 0 = count up, 1 = count down
output reg [OUTPUT_BITS-1:0] count
);
// Calculate number of input clock cycles per output increment
localparam int N_COUNTS = CLK_HZ / TARGET_HZ;
// Calculate number of bits needed for divider
localparam int N_BITS = $clog2(N_COUNTS);
// Clock divider counter
logic [N_BITS-1:0] clk_cnt;
always @(posedge clk) begin
// Main counter
if (!rst) begin
count <= 0;
clk_cnt <= 0;
end else begin
// Second counter
clk_cnt <= (clk_cnt == N_COUNTS - 1) ? 0 : (clk_cnt + 1);
if (clk_cnt == 0) count <= (up_down == 0) ? (count + 1) : (count - 1);
end
end
endmoduleActivity 2: Display the counter on seven-segment display
Taking advantage of persistence of vision

Putting everything together
RTL Code
Constraints
The fruits of our labour
Last updated