• 已绑定课时
  • 工程师可点评

实验报告 · 19 | 项目设计:动态数码管(5-2.0版)

Ken
24 1 2
工程师评优秀1

实验报告 · 19 | 项目设计:动态数码管(5-2.0版)

课程:初级 | FPGA工程师基础启航(含板卡+证书) · 第二节 Verilog HDL语法篇

发布自课程云笔记 · FPGA 实验证据骨架

動態數碼管暫存代碼

`timescale 1ns / 1ps

module SMG_add(
input       sysclk,
input       rst_n,
output      CLK,
output      STB,
output      DIN
);

wire        req;
wire        done;
wire[13:0]  number;

data_ctrl #(.DELAY(26'd50_000))//_000))
data_ctrl_u(
.sysclk(sysclk),
.rst_n(rst_n),
.done(done),
.req(req),
.number(number)
);

SMD_DT SMD_DT_u(
.sysclk(sysclk),
.rst_n(rst_n),
.number(number),
.req(req),
.CLK(CLK),
.DIN(DIN),
.STB(STB),
.done(done)
);
endmodule
`timescale 1ns / 1ps

module SMD_DT(
input           sysclk,
input           rst_n,
input [13:0]    number,
input           req,
output reg      CLK,
output reg      DIN,
output reg      STB,
output         done
);

parameter DELAY     = 7'd50;
parameter IDLE      = 3'b000;
parameter CMD_1     = 3'b001;
parameter CMD_2     = 3'b011;
parameter ADDR_DATA = 3'b110;
parameter CMD_3     = 3'b111;

parameter CMD1_VALUE = 8'b00_0000_10; // 6 digit and 8 segments control
parameter CMD2_VALUE = 8'b01_00_00_00; // Normal, Auto address increase, data write mode
parameter CMD3_VALUE = 8'b10_00_1_111; // PWM setting 14/16

reg [2:0] c_state;  // 3-bit state register for FSM
reg [2:0] n_state;  // 3-bit next state register for FSM
reg [6:0] cnt_clk;  // 6-bit counter for clock delay based on DELAY parameter
reg [3:0] cnt_bit;  // 3-bit counter for bit transmission
reg [3:0] cnt_byte; // 4-bit counter for byte transmission
reg [7:0] data_reg;

//======================================================
wire [3:0] uni, ten, hun, tho;
reg [3:0] data_num;
reg [7:0] data_sel; // Select light by data_num
// Divide digits
assign uni = number % 4'd10;
assign ten = (number / 4'd10) % 4'd10;
assign hun = (number / 7'd100) % 4'd10;
assign tho = (number / 7'd1000);

// Data_num: {Addr + Data} * 4
always @(posedge sysclk)
begin
if (!rst_n)
begin
data_num <= 4'd0;
end
else
begin
if (c_state == ADDR_DATA)
begin
case (cnt_byte)
4'd1: data_num <= uni;
4'd3: data_num <= ten;
4'd5: data_num <= hun;
4'd7: data_num <= tho;
default: data_num <= 4'd0;
endcase
end
end
end

// Select light
always @(*)
begin
if (!rst_n)
begin
data_sel = 8'd0;
end
else
begin
case (data_num)
4'd0: data_sel = 8'b0011_1111;
4'd1: data_sel = 8'b0000_0110;
4'd2: data_sel = 8'b0101_1011;
4'd3: data_sel = 8'b0100_1111;
4'd4: data_sel = 8'b0110_0110;
4'd5: data_sel = 8'b0110_1101;
4'd6: data_sel = 8'b0111_1101;
4'd7: data_sel = 8'b0000_0111;
4'd8: data_sel = 8'b0111_1111;
4'd9: data_sel = 8'b0110_1111;
default: data_sel = 8'b0011_1111;
endcase
end
end
//======================================================
assign done = ((c_state == CMD_3) && (cnt_clk == DELAY - 1'b1) && (cnt_bit == 4'd10)) ? 1'b1 : 1'b0;

// FSM
always @(posedge sysclk)
begin
if (!rst_n)
begin
c_state <= IDLE;
end
else
begin
c_state <= n_state;
end
end

always @(*)
begin
if (!rst_n)
begin
n_state = IDLE;
end
else
begin
case (c_state)
IDLE:
begin
if (req)
begin
n_state = CMD_1;
end
else
begin
n_state = IDLE;
end
end
CMD_1:
begin
if ((cnt_clk == DELAY - 1'b1) && (cnt_bit == 4'd10))
n_state = CMD_2;
else
n_state = CMD_1;
end
CMD_2:
begin
if ((cnt_clk == DELAY - 1'b1) && (cnt_bit == 4'd10))
n_state = CMD_3;
else
n_state = CMD_2;
end
ADDR_DATA:
begin
if ((cnt_clk == DELAY - 1'b1) && (cnt_bit == 4'd10))
n_state = CMD_3;
else
n_state = ADDR_DATA;
end
CMD_3:
begin
if ((cnt_clk == DELAY - 1'b1) && (cnt_bit == 4'd10))
n_state = IDLE;
else
n_state = CMD_3;
end
default: n_state = IDLE;
endcase
end
end

// cnt_clk
always @(posedge sysclk)
begin
if (!rst_n)
begin
cnt_clk <= 7'd0;
end
else
begin
if (c_state != IDLE)
begin
if (cnt_clk == DELAY - 1'b1)
cnt_clk <= 7'd0;
else
cnt_clk <= cnt_clk + 1'b1;
end
else
begin
cnt_clk <= 7'd0;
end
end
end

// cnt_bit
always @(posedge sysclk)
begin
if (!rst_n)
begin
cnt_bit <= 4'd0;
end
else
begin
case (c_state)
CMD_1, CMD_2, CMD_3, ADDR_DATA:
begin
if (cnt_clk == DELAY - 1'b1)
begin
if (cnt_bit == 4'd10)
begin
cnt_bit <= 4'd0;
end
else
begin
cnt_bit <= cnt_bit + 1'b1;
end
end
else
begin
cnt_bit <= cnt_bit;
end
end
default: cnt_bit <= 4'd0;
endcase
end
end

// cnt_byte
always @(posedge sysclk)
begin
if (!rst_n)
begin
cnt_byte <= 4'd0;
end
else
begin
if (c_state == ADDR_DATA)
begin
if (cnt_clk == DELAY - 1'b1)
begin
if (cnt_bit == 4'd10)
begin
cnt_byte <= cnt_byte + 1'b1;
end
else
begin
cnt_byte <= cnt_byte;
end
end
else
begin
cnt_byte <= cnt_byte;
end
end
else
begin
cnt_byte <= 4'd0;
end
end
end

// CLK
always @(posedge sysclk)
begin
if (!rst_n)
begin
CLK <= 1'b1;
end
else
begin
if (c_state != IDLE)
begin
if ((cnt_clk == 7'd0) && (cnt_bit <= 4'd7))
begin
CLK <= 1'b0;
end
else if ((cnt_clk == DELAY / 2'd2) && (cnt_bit <= 4'd7))
begin
CLK <= 1'b1;
end
else if ((cnt_clk == DELAY - 1'b1) && (cnt_bit > 4'd7))
begin
CLK <= 1'b1;
end
end
else
begin
CLK <= 1'b1;
end
end
end

// STB
always @(posedge sysclk)
begin
if (!rst_n)
begin
STB <= 1'b1;
end
else
begin
case (c_state)
CMD_1, CMD_2, CMD_3:
begin
if ((cnt_clk == 7'd0) && (cnt_bit == 4'd0))
begin
STB <= 1'b0;
end
else if ((cnt_clk == DELAY - 1'b1) && (cnt_bit > 4'd7))
begin
STB <= 1'b1;
end
else
begin
STB <= STB;
end
end
ADDR_DATA:
begin
if ((cnt_clk == 7'd0) && (cnt_bit == 4'd0) && (cnt_byte == 4'd0))
begin
STB <= 1'b0;
end
else if ((cnt_clk == DELAY - 1'b1) && (cnt_bit > 4'd7) && (cnt_byte == 4'd7))
begin
STB <= 1'b1;
end
else
begin
STB <= STB;
end
end
default: STB <= 1'b1;
endcase
end
end

// DIN
always @(posedge sysclk)
begin
if (!rst_n)
begin
DIN <= 1'b1;
end
else
begin
if (c_state != IDLE)
begin
if (cnt_bit <= 4'd7)
begin
DIN <= data_reg[cnt_bit];
end
else
begin
DIN <= DIN;
end
end
else
begin
DIN <= 1'b1;
end
end
end

// data_reg
always @(posedge sysclk)
begin
if (!rst_n)
begin
data_reg <= 8'd0;
end
else
begin
case (c_state)
CMD_1: data_reg <= CMD1_VALUE;
CMD_2: data_reg <= CMD2_VALUE;
CMD_3: data_reg <= CMD3_VALUE;
ADDR_DATA:
begin
case (cnt_byte)
4'd0: data_reg <= 8'b10_00_0000; // Addr: 00H
4'd2: data_reg <= 8'b10_00_0010; // Addr: 02H
4'd4: data_reg <= 8'b10_00_0100; // Addr: 04H
4'd6: data_reg <= 8'b10_00_0110; // Addr: 06H
4'd1, 4'd3, 4'd5, 4'd7: data_reg <= data_sel;
default: data_reg <= 8'd0;
endcase
end
default: data_reg <= 8'd0;
endcase
end
end
endmodule
`timescale 1ns / 1ps

module data_ctrl #(
parameter DELAY = 26'd50_000//_000
)(
input               sysclk,
input               rst_n,
input               done,
output              req,
output reg[13:0]    number
);

reg [25:0] cnt;

assign req = ((cnt == 26'd0) || done) ? 1 : 0;

always @(posedge sysclk)
begin
if (!rst_n)
begin
cnt <= 26'd0;
end
else
begin
if (cnt == DELAY - 1'b1)
begin
cnt <= 26'd0;
end
else
begin
cnt <= cnt + 1'b1;
end
end
end

always @(posedge sysclk)
begin
if (!rst_n)
begin
number <= 14'd0;
end
else
begin
if (cnt == DELAY - 1'b1)
begin
if (number == 14'd9999)
begin
number <= 14'd0;
end
else
begin
number <= number + 1'b1;
end
end
else
begin
number <= number;
end
end
end
endmodule

本节要验证什么?预期现象是什么?

用工具栏插入波形图、仿真界面或示波器照片

粘贴关键仿真输出或报错原文

板卡现象、按键/数码管/接口实测结果

结论、坑点与下一步修改

学习上下文

产出状态

评优秀1 位工程师
工程师已阅24 位工程师