Advertisement
regzarr

LSIC - 7 segment displays

Nov 25th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `timescale 1ns/1ps
  2.  
  3. module SevenSegmentController(
  4.     input [31:0] ms_count,  // Millisecond count input
  5.     output reg [6:0] hex0,  // 7-segment output for digit 0 (LSB)
  6.     output reg [6:0] hex1,  // 7-segment output for digit 1
  7.     output reg [6:0] hex2,  // 7-segment output for digit 2
  8.     output reg [6:0] hex3,  // 7-segment output for digit 3
  9.     output reg [6:0] hex4,  // 7-segment output for digit 4
  10.     output reg [6:0] hex5   // 7-segment output for digit 5 (MSB)
  11. );
  12.  
  13.     // Local variables for extracted digits
  14.     reg [3:0] digit[5:0];
  15.  
  16.     // 7-segment encoding (active low):
  17.     // a-g segments for digits 0-9
  18.     function [6:0] encode;
  19.         input [3:0] value;
  20.         case (value)
  21.             4'd0: encode = 7'b1000000; // 0
  22.             4'd1: encode = 7'b1111001; // 1
  23.             4'd2: encode = 7'b0100100; // 2
  24.             4'd3: encode = 7'b0110000; // 3
  25.             4'd4: encode = 7'b0011001; // 4
  26.             4'd5: encode = 7'b0010010; // 5
  27.             4'd6: encode = 7'b0000010; // 6
  28.             4'd7: encode = 7'b1111000; // 7
  29.             4'd8: encode = 7'b0000000; // 8
  30.             4'd9: encode = 7'b0010000; // 9
  31.             default: encode = 7'b1111111; // Blank
  32.         endcase
  33.     endfunction
  34.  
  35.     // Extract digits from ms_count
  36.     always @(*) begin
  37.         // Convert ms_count into individual digits (BCD extraction)
  38.         digit[0] = ms_count % 10;
  39.         digit[1] = (ms_count / 10) % 10;
  40.         digit[2] = (ms_count / 100) % 10;
  41.         digit[3] = (ms_count / 1000) % 10;
  42.         digit[4] = (ms_count / 10000) % 10;
  43.         digit[5] = (ms_count / 100000) % 10;
  44.  
  45.         // Map digits to corresponding 7-segment displays
  46.         hex0 = encode(digit[0]);
  47.         hex1 = encode(digit[1]);
  48.         hex2 = encode(digit[2]);
  49.         hex3 = encode(digit[3]);
  50.         hex4 = encode(digit[4]);
  51.         hex5 = encode(digit[5]);
  52.     end
  53.  
  54. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement