1、根据以下描述功能用verilog编写一段代码,并用状态机来实现该功能。
(1)状态机:实现一个测试过程,该过程包括启动准备状态、启动测试、停止测试、查询测试结果、显示测试结果、测试结束返回初始化6个状态;用时间来控制该过程,90秒内完成该过程;
(2)描述状态跳转时间;
(3)编码实现
2、画出可以检测10010串的状态图, 并用verilog编程实现之。
略
计数器模块
一共6个状态,每15s进行切换
module time_cnt(
input wire clk,
input wire rst_n,
output wire flag
);
parameter MAX_NUM = 30'd749_999_999;//记最大数15s,750_000_000次
reg [29:0] cnt;//计数寄存器
reg flag_r;
//0.5s计时器
always@(posedge clk or negedge rst_n)begin
if(!rst_n)begin
cnt <= 29'd0;
end
else if(cnt == MAX_NUM)begin
cnt <= 29'd0;
end
else begin
cnt <= cnt + 1'd1;
end
end
//0.5s脉冲信号
always@(posedge clk or negedge rst_n)begin
if(!rst_n)begin
flag_r <= 1'b0;
end
else if(cnt == MAX_NUM)begin
flag_r <= 1'b1;
end
else begin
flag_r <= 1'b0;
end
end
assign flag = flag_r;
endmodule
状态机模块
共有6种状态,按顺序切换
module fsm(
input clk,
input rst_n,
input wire flag
);
reg [2:0] cstate;//当前状态
reg [2:0] nstate;//下一状态
//状态划分
localparam ready = 0;
localparam start = 1;
localparam stop = 2;
localparam query = 3;
localparam display = 4;
localparam initialize = 5;
//初始化
always@(posedge clk or negedge rst_n)begin
if(!rst_n)
cstate <= initialize;//复位键被按下,当前状态设置为初始化
else
cstate <= nstate; //下一次状态赋值给当前状态
end
//状态选择
always@(*)begin
if(!rst_n)
begin
nstate = initialize;//初始化
end
else
case(cstate)
initialize: begin
if(flag == 1'b1) //该状态持续时间为1s,1s后,下一次状态更改为led1亮
nstate = ready;
else
nstate = initialize;
end
ready: begin
if(flag == 1'b1)
nstate = start;
else
nstate = ready;
end
start: begin
if(flag == 1'b1)
nstate = stop;
else
nstate = start;
end
stop: begin
if(flag == 1'b1)
nstate = query;
else
nstate = stop;
end
query: begin
if(flag == 1'b1)
nstate = display;
else
nstate = query;
end
display: begin
if(flag == 1'b1)
nstate = initialize;
else
nstate = display;
end
default: ;
endcase
end
endmodule
顶层模块
module fsm_top(
input wire clk,
input wire rst_n
);
wire flag;
//例化计时器模块
time_cnt inst_time_cnt(
.clk (clk ),
.rst_n (rst_n ),
.flag (flag)
);
//例化状态机模块
fsm inst_fsm(
.clk (clk ),
.rst_n (rst_n ),
.flag (flag)
);
endmodule
略
按键消抖模块
module key_debounce(
input wire clk,
input wire rst_n,
input wire key,
output reg flag, //判断抖动是否消除的标志信号,0为抖动,1为抖动结束
output reg key_value //消抖后稳定的按键值给到蜂鸣器模块和LED模块
);
//定义20ms延迟计数器,0.2s,1_000_000次
reg [19:0] delay_cnt;
//寄存依次key的值用来判断按键是否消抖成功
reg key_reg;
//按下按键20ms延时计数器
always@(posedge clk or negedge rst_n)begin
if(!rst_n)
begin
key_reg <= 1'b1; //复位信号,设置按键无效
delay_cnt <= 1'b0; //计数器设置为0
end
else
begin
key_reg <= key;
if(key_reg ^ key) //当这一次key值和上一次key值不一样,证明正在抖动
delay_cnt <= 20'd1_000_000; //延迟时间20ms
else if(delay_cnt > 0)
delay_cnt <= delay_cnt - 1; //没有抖动,开始20ms倒计时
else
delay_cnt <= 1'b0;
end
end
//根据延时计数器获取按键状态以及按键值
always@(posedge clk or negedge rst_n)begin
if(!rst_n)
begin
flag <= 1'b0; //复位信号,设置信号标志为抖动
key_value <= 1'b1; //设置抽样值为1
end
else
begin
if(delay_cnt == 20'd1) //倒计时1_000_000到1
begin
flag <= 1'b1;
key_value <= key; //稳定20ms后将key值给到key_value
end
else
begin
flag <= 1'b0;
key_value <= key_value; //20ms内先不取样
end
end
end
endmodule
状态机模块
module fsm_10010(
input clk,
input rst_n,
input [1:0] key,
);
parameter MAX_NUM = 24'd9_999_999;
parameter T = 4'd10;
reg [2:0] cstate; //现态
//状态划分
localparam state_0 = 0;
localparam state_1 = 1;
localparam state_2 = 2;
localparam state_3 = 3;
localparam state_4 = 4;
localparam state_5 = 5;
reg [3:0] cnt = 0;//计时器赋初值为0
reg [23:0] cnt_02 = 0;
reg time_flag;//计时标志,1:开始计时,0:结束计时
//0.2计数器模块
always@(posedge clk or negedge rst_n)begin
if(!rst_n)
cnt_02 <= 1'b0; //按下复位键,清零
else if(cnt_02 == MAX_NUM)
cnt_02 <= 1'b0;
else
cnt_02 <= cnt_02 + 1'b1;
end
//2s计数器模块
always@(posedge clk or negedge rst_n)begin
if(!rst_n)
cnt <= 1'b0;//按下复位键,清零
else if(time_flag == 1'b1)begin//开始计时
if(cnt_02 == MAX_NUM)begin
if(cnt < T)begin
cnt <= cnt + 1'b1;
end
else begin
cnt <= 1'b0;//cnt计数达到最大就清空
end
end
else begin
cnt <= cnt;//其余时间保持
end
end
else begin
cnt <= 1'b0;//不计时,cnt清空
end
end
//状态切换模块
always@(posedge clk or negedge rst_n)begin
if(!rst_n)
begin
cstate <= state_0;
end
else
case(cstate)
state_0: begin
if(key[1] == 1'b1) //该状态持续时间为1s,1s后,下一次状态更改为led1亮
cstate <= state_1;
else if(key[0] == 1'b1)
cstate <= state_0;
else
cstate <= state_0;
end
state_1: begin
if(key[0] == 1'b1)
cstate <= state_2;
else if(key[1] == 1'b1)
cstate <= state_0;
else
cstate <= state_1;
end
state_2: begin
if(key[0] == 1'b1)
cstate <= state_3;
else if(key[1] == 1'b1)
cstate <= state_0;
else
cstate <= state_2;
end
state_3: begin
if(key[1] == 1'b1)
cstate <= state_4;
else if(key[0] == 1'b1)
cstate <= state_0;
else
cstate <= state_3;
end
state_4: begin
if(key[0] == 1'b1)begin
cstate <= state_5;
time_flag <= 1'b1;//开始计时
end
else if(key[1] == 1'b1)
cstate <= state_0;
else
cstate <= state_4;
end
state_5: begin
if(cnt == T)begin
cstate <= state_0;
time_flag <= 1'b0;//结束计时
end
else
cstate <= state_5;
end
default: ;
endcase
end
endmodule
顶层模块
module fsm10010_top(
input wire clk,
input wire rst_n,
input wire [1:0] key
);
wire [1:0] flag;
wire [1:0] key_value;
//例化按键KEY1
key_debounce inst_key_debounce(
.clk (clk),
.rst_n (rst_n),
.key (key[0]),
.flag (flag[0]),
.key_value(key_value[0])
);
//例化按键KEY2
key_debounce inst_key_debounce1(
.clk (clk),
.rst_n (rst_n ),
.key (key[1]),
.flag (flag[1]),
.key_value(key_value[1])
);
//例化状态机
fsm10010 inst_fsm(
.clk (clk),
.rst_n(rst_n),
.key ({~key_value[1]&&flag[1],~key_value[0]&&flag[0]})
);
endmodule
此次FPGA实验通过状态机实现状态转换,并可对10010串进行检测,由此可衍生出更多功能,如密码锁、售货机等。