FPGA Project: A Seven-Segment Seconds Counter

Count seconds in binary-coded decimal (BCD), one 4-bit digit per decimal place, so each digit can go straight to a display. A tick every second adds one to the ones digit, and a digit that passes 9 returns to 0 and carries into the next. A decoder turns each digit into the seven segment lines. The Basys 3 shares one set of segment lines among four digits, so it lights them in turn, fast enough to look steady; the DE10-Lite has separate lines for each digit. The Arty A7 has no display, so its version shows the ones digit on its four LEDs.

How this was checked

The design was simulated in Icarus Verilog 12 and linted with Verilator 5.036 (-Wall, no warnings) for every board's top level. The Basys 3 and Arty A7 versions were synthesized, placed and routed in Vivado 2025.2, met timing, and produced a bitstream. The DE10-Lite version was simulated and linted but not compiled, because Intel Quartus was not used here; its pin assignments come from Terasic's DE10-Lite System Builder file. None of the bitstreams has been loaded onto a board yet. If you build it and something doesn't behave as described, please tell us through the contact page.

The three boards

BoardFPGAClockTools
Digilent Basys 3AMD Artix-7 XC7A35T100 MHz, pin W5Vivado
Digilent Arty A7-35AMD Artix-7 XC7A35T100 MHz, pin E3Vivado
Terasic DE10-LiteIntel MAX 10 10M5050 MHz, pin P11Quartus Prime Lite
The counter and displayA one-second tick drives a four-digit BCD counter; a multiplexer picks one digit at a time, a decoder turns it into segment lines, and the digit enables select which display is lit.1 s tickBCD count4 digitsDigit mux1 ms eachDecoder7 segments
On the Basys 3 each digit is lit for 1 ms in turn; the DE10-Lite skips the multiplexer and drives each digit directly.

The counter

seconds_counter.v
`timescale 1ns/1ps
// A four-digit BCD count of seconds, 0000 to 9999, with a synchronous reset.
module seconds_counter #(parameter CLK_HZ = 100_000_000) (
  input  wire        clk,
  input  wire        rst,
  output reg  [15:0] bcd            // {thousands, hundreds, tens, ones}
);
  localparam integer TW = $clog2(CLK_HZ);
  localparam [TW-1:0] LAST = CLK_HZ - 1;
  reg [TW-1:0] tick = 0;
  wire second = (tick == LAST);

  // The next BCD value: add one to the ones digit and ripple the carry.
  reg [15:0] next;
  reg        carry;
  integer d;
  always @(*) begin
    next  = bcd;
    carry = 1'b1;
    for (d = 0; d < 4; d = d + 1) begin
      if (carry) begin
        if (bcd[d*4 +: 4] == 4'd9) next[d*4 +: 4] = 4'd0;
        else begin next[d*4 +: 4] = bcd[d*4 +: 4] + 4'd1; carry = 1'b0; end
      end
    end
  end

  initial bcd = 16'h0000;
  always @(posedge clk) begin
    if (rst) begin
      tick <= 0;
      bcd  <= 16'h0000;
    end else begin
      tick <= second ? {TW{1'b0}} : tick + 1'b1;
      if (second) bcd <= next;
    end
  end
endmodule

The next value is worked out in a combinational block, digit by digit with a carry, and loaded only when the one-second tick fires. Keeping the carry logic combinational keeps the clocked block to nonblocking assignments, which is what Verilator's lint expects.

The decoder

hex7seg.v
`timescale 1ns/1ps
// One hex digit to seven segments {g,f,e,d,c,b,a}, active low (a lit
// segment is 0), which suits both the Basys 3 and the DE10-Lite displays.
module hex7seg (
  input  wire [3:0] digit,
  output reg  [6:0] seg_n
);
  always @(*) begin
    case (digit)
      4'h0: seg_n = 7'b1000000;
      4'h1: seg_n = 7'b1111001;
      4'h2: seg_n = 7'b0100100;
      4'h3: seg_n = 7'b0110000;
      4'h4: seg_n = 7'b0011001;
      4'h5: seg_n = 7'b0010010;
      4'h6: seg_n = 7'b0000010;
      4'h7: seg_n = 7'b1111000;
      4'h8: seg_n = 7'b0000000;
      4'h9: seg_n = 7'b0010000;
      4'hA: seg_n = 7'b0001000;
      4'hB: seg_n = 7'b0000011;
      4'hC: seg_n = 7'b1000110;
      4'hD: seg_n = 7'b0100001;
      4'hE: seg_n = 7'b0000110;
      default: seg_n = 7'b0001110;  // F
    endcase
  end
endmodule

Multiplexing on the Basys 3

top_counter_basys3.v
`timescale 1ns/1ps
// Basys 3: a seconds counter on the four multiplexed digits. The digits
// share one set of segment lines, so each is lit in turn for 1 ms
// (a 250 Hz refresh of the whole display), fast enough to look steady.
module top_counter_basys3 (
  input  wire       clk,       // 100 MHz, pin W5
  input  wire       btnC,      // reset, active high
  output wire [6:0] seg,       // {g..a}, active low
  output wire [3:0] an,        // digit enables, active low
  output wire       dp
);
  wire [15:0] bcd;
  seconds_counter #(.CLK_HZ(100_000_000)) u_cnt (.clk(clk), .rst(btnC), .bcd(bcd));
  reg [16:0] refresh = 0;      // 100,000 clocks = 1 ms per digit
  reg [1:0]  which = 0;
  always @(posedge clk) begin
    if (refresh == 17'd99_999) begin refresh <= 0; which <= which + 1'b1; end
    else refresh <= refresh + 1'b1;
  end
  hex7seg u_dec (.digit(bcd[which*4 +: 4]), .seg_n(seg));
  assign an = ~(4'b0001 << which);
  assign dp = 1'b1;
endmodule
Four digits, one segment busThe digit enables an[3:0] are active low and one digit is enabled at a time, 1 ms each; the segment bus carries that digit's pattern while it is enabled.1 ms2 msan[3:0]1110110110110111segonestenshund.thou.
Each digit is lit 1 ms out of every 4 ms, a 250 Hz refresh, so the eye sees all four at once.

Simulation and build results

With the tick set to every 4 clocks, the testbench saw 0009, then 0010, then 1000, and the wrap from 9999 to 0000; the decoder lit the right number of segments for each digit from 0 to 9. In Vivado the Basys 3 design used 42 LUTs and 62 flip-flops with 5.496 ns of slack at 100 MHz; the Arty A7 LED version used 23 LUTs and 39 flip-flops with 5.202 ns.

Common mistakes

  • Driving segments active high on a common-anode display: on both boards a lit segment is 0.
  • Refreshing too slowly, so the digits flicker; a few ms per digit is fine.
  • Counting in binary and converting to decimal for display, which needs a divider; counting in BCD avoids it.
  • Using the button directly as a reset without thinking about bounce; here it only resets, so bounce does no harm.

Check your understanding

  • Q1. What is the BCD value after 0999? Answer: 1000, with three carries.
  • Q2. How often is each digit refreshed? Answer: every 4 ms, 250 times a second.
  • Q3. Why does the DE10-Lite need no multiplexing? Answer: each of its six digits has its own eight segment lines.

Beginner FPGA projects

Related

Sources

Last verified

Simulated, linted and built on 17 November 2026; the files are in the site's repository under scripts/fpga.