一、实验任务
1、根据以下描述功能用verilog编写一段代码,并用状态机来实现该功能。
(1)状态机:实现一个测试过程,该过程包括启动准备状态、启动测试、停止测试、查询测试结果、显示测试结果、测试结束返回初始化6个状态;用时间来控制该过程,90秒内完成该过程;
(2)描述状态跳转时间;
(3)编码实现。
二、实验过程
(一)新建工程
- 选择项目路径,并给项目命名
- 点击next
- Family选择“ cycloneIVE”,芯片选择“EP4CE11529C7”
- 点击next
- 点击finish
(二)编写代码
- 设计计时器模块Verilog HDL文件
//15s脉冲信号
module time_count(
input wire clk, //时钟,50MHZ
input wire rst_n, //复位信号,下降沿有效,negative
output wire sec_15//15s输出一个脉冲信号
);
parameter MAX_NUM = 30'd749_999_999;//记最大数15s,750_000_000次
reg [29:0] cnt_15;//计数寄存器
reg sec_15_r;
//0.5s计时器
always@(posedge clk or negedge rst_n)begin
if(!rst_n)begin
cnt_15 <= 25'd0;
end
else if(cnt_15 == MAX_NUM)begin
cnt_15 <= 25'd0;
end
else begin
cnt_15 <= cnt_15 + 1'd1;
end
end
//0.5s脉冲信号
always@(posedge clk or negedge rst_n)begin
if(!rst_n)begin
sec_15_r <= 1'b0;
end
else if(cnt_15 == MAX_NUM)begin
sec_15_r <= 1'b1;
end
else begin
sec_15_r <= 1'b0;
end
end
assign sec_15 = sec_15_r;//当右边改变,立马赋值给左边 assign和always并行
endmodule
- 设计顶层模块Verilog HDL文件
module fsm(
input clk,
input rst_n,
input wire sec_15
);
reg [2:0] cstate; //现态
reg [2:0] nstate; //次态
//状态划分
localparam state_ready = 0; //启动准备状态
localparam state_start = 1; //启动测试
localparam state_stop = 2; //停止测试
localparam state_query = 3; //查询测试结果
localparam state_display = 4; //显示测试结果
localparam state_initialize = 5; //初始化
//第一段:现态跟随次态,时序逻辑,非阻塞赋值
always@(posedge clk or negedge rst_n)begin
if(!rst_n)
cstate <= state_initialize;//复位键被按下,当前状态设置为初始化
else
cstate <= nstate; //下一次状态赋值给当前状态
end
//第二段:组合逻辑,阻塞赋值
always@(*)begin
if(!rst_n)
begin
nstate = state_initialize;
end
else
case(cstate)
state_initialize: begin
if(sec_15 == 1'b1) //该状态持续时间为1s,1s后,下一次状态更改为led1亮
nstate = state_ready;
else
nstate = state_initialize;
end
state_ready: begin
if(sec_15 == 1'b1)
nstate = state_start;
else
nstate = state_ready;
end
state_start: begin
if(sec_15 == 1'b1)
nstate = state_stop;
else
nstate = state_start;
end
state_stop: begin
if(sec_15 == 1'b1)
nstate = state_query;
else
nstate = state_stop;
end
state_query: begin
if(sec_15 == 1'b1)
nstate = state_display;
else
nstate = state_query;
end
state_display: begin
if(sec_15 == 1'b1)
nstate = state_initialize;
else
nstate = state_display;
end
default: ;
endcase
end
endmodule
- 设计顶层模块Verilog HDL文件
module top_fsm(
input wire clk,
input wire rst_n
);
wire sec_15;//将两个模块的信号连接起来
//例化计时器模块
time_count inst_time_count(
.clk (clk ),//时钟,50MHZ
.rst_n (rst_n ),//复位信号,下降沿有效,negative
.sec_15 (sec_15) //15s输出一个脉冲信号
);
//例化状态机模块
fsm inst_fsm(
.clk (clk ),//时钟,50MHZ
.rst_n (rst_n ),//复位信号,下降沿有效,negative
.sec_15 (sec_15) //15s脉冲信号
);
endmodule
编译:
RTL门级电路图:
参考
https://blog.youkuaiyun.com/qq_45659777/article/details/124539082?spm=1001.2014.3001.5502