赞
踩
1、根据以下描述功能用verilog编写一段代码,并用状态机来实现该功能。
(1)状态机:实现一个测试过程,该过程包括启动准备状态、启动测试、停止测试、查询测试结果、显示测试结果、测试结束返回初始化6个状态;用时间来控制该过程,90秒内完成该过程;
(2)描述状态跳转时间;
(3)编码实现。
2. 画出可以检测10010串的状态图, 并用verilog编程实现之。
代码如下 一共六个状态,为了方便操作,每个状态保持15s。每个状态对应的led状态各不相同。led的状态值为当前状态值。
module training_1( input wire clk, input wire rst_n, output reg [3:0]led ); localparam [2:0] S1 = 1; localparam [2:0] S2 = 2; localparam [2:0] S3 = 3; localparam [2:0] S4 = 4; localparam [2:0] S5 = 5; localparam [2:0] S6 = 6; parameter [29:0] MAX_COUNT = 750_000_000;//15s reg [29:0] count; reg [2:0] current_state; reg [2:0] next_state; //15s 计时器 以及 状态转移 always@(posedge clk or negedge rst_n) begin if(!rst_n) begin current_state <= S1; count <= 30'd1; end else if (count == MAX_COUNT) begin count <= 30'd1; end else begin count <= count + 30'd1; current_state <= next_state; end end // 状态判断 always @(posedge clk or negedge rst_n) begin if(!rst_n) begin next_state <= S1; end else begin case (current_state) S1: begin if(count == MAX_COUNT) begin next_state <= S2; end else begin next_state <= next_state; end end S2: begin if(count == MAX_COUNT) begin next_state <= S3; end else begin next_state <= next_state; end end S3: begin if(count == MAX_COUNT) begin next_state <= S4; end else begin next_state <= next_state; end end S4: begin if(count == MAX_COUNT) begin next_state <= S5; end else begin next_state <= next_state; end end S5: begin if(count == MAX_COUNT) begin next_state <= S6; end else begin next_state <= next_state; end end S6: begin if(count == MAX_COUNT) begin next_state <= S1; end else begin next_state <= next_state; end end default: next_state <= S1; endcase end end //状态输出 根据当前状态点亮不同的灯 always @(posedge clk or negedge rst_n) begin if (!rst_n) begin led <= 4'd0; end else begin case(current_state) S1: led <= 4'd1; S2: led <= 4'd2; S3: led <= 4'd3; S4: led <= 4'd4; S5: led <= 4'd5; S6: led <= 4'd6; default: led <= 4'd0; endcase end end endmodule
作为顶层文件,对training_1的调用。考虑到15s太长,时间间隔缩小到1.5s方便观察。
代码如下
`timescale 1ns/1ns // 时间单位/时间精度 module training_tb; parameter CYCLE = 20; reg clk; reg rst_n; wire [3:0]led; always #(CYCLE/2) clk = ~clk; //初始化 initial begin clk = 1'b1; rst_n = 1'b0; #CYCLE ; rst_n = 1'b1; #(CYCLE *75 * 6); $stop; end //实例化led1 training_1#(.MAX_COUNT(75)) inst_training( .clk (clk ), .rst_n (rst_n ), .led (led) ); endmodule endmodule
用于仿真,每个状态持续时间按比例缩小。代码如下
`timescale 1ns/1ns // 时间单位/时间精度 module training_tb; parameter CYCLE = 20; reg clk; reg rst_n; wire [3:0]led; always #(CYCLE/2) clk = ~clk; //初始化 initial begin clk = 1'b1; rst_n = 1'b0; #CYCLE ; rst_n = 1'b1; #(CYCLE *75 * 6); $stop; end //实例化 training_1#(.MAX_COUNT(75)) inst_training( .clk (clk ), .rst_n (rst_n ), .led (led) ); endmodule endmodule
用于绑定相关引脚
代码如下
package require ::quartus::project
set_location_assignment PIN_E1 -to clk
set_location_assignment PIN_M15 -to rst_n
set_location_assignment PIN_G15 -to led[0]
set_location_assignment PIN_F16 -to led[1]
set_location_assignment PIN_F15 -to led[2]
set_location_assignment PIN_D16 -to led[3]
点击 Assingment -> Device -> Device and Pin Option
把Programma 改为 regular后保存
点击 Assingment -> setting ->simulation -> Test Bench
把training_tb添加进去,然后保存
点击 Tools -> Tcl script
找到刚刚写好的tcl文件,点击run进行引脚的绑定
可以看到led的值符合预期效果
s0:初始状态,检测输入。检测到1进入下一状态,否则一直处于当前状态。
s1:目前状态为1,检测输入。检测到0进入下一状态,检测到1回到S0,否则一直处于当前状态。
s2:目前状态为10,检测输入。检测到0进入下一状态,检测到1回到S0,否则一直处于当前状态。
s3:目前状态为100,检测输入。检测到1进入下一状态,检测到0回到S0,否则一直处于当前状态。
s4:目前状态为1001,检测输入。检测到0灯亮,并回到S0,否则一直处于当前状态。
代码如下
module training_2 ( input wire clk, input wire rst_n, input wire [1:0]key, output reg [3:0]led ); localparam [2:0] S0 = 0; localparam [2:0] S1 = 1; localparam [2:0] S2 = 2; localparam [2:0] S3 = 3; localparam [2:0] S4 = 4; localparam [2:0] S5 = 5; reg [2:0] current_state; reg flag ; always @(posedge clk or negedge rst_n) begin if (!rst_n) begin current_state <= S0; flag <= 0; end else begin case(current_state) S0: begin if(key[1]) begin current_state <= S1; flag <= 0; end else if(key[0]) begin current_state <= S0; flag <= 0; end else begin current_state <=current_state; flag <= flag; end end S1: begin if(key[0]) begin current_state <= S2; flag <= 0; end else if(key[1]) begin current_state <= S0; flag <= 0; end else begin current_state <=current_state; flag <= flag; end end S2: begin if(key[0]) begin current_state <= S3; flag <= 0; end else if(key[1]) begin current_state <= S0; flag <= 0; end else begin current_state <=current_state; flag <= flag ; end end S3: begin if(key[1]) begin current_state <= S4; flag <= 0; end else if(key[0]) begin current_state <= S0; flag <= 0; end else begin current_state <=current_state; flag <= flag ;; end end S4: begin if(key[0]) begin current_state <= S0; flag <= 1; end else if(key[1]) begin current_state <= S0; flag <= 0; end else begin current_state <=current_state; flag <= flag; end end default:begin current_state <= S0; flag <= 0; end endcase end end //根据flag 决定灯是否亮 always @(posedge clk or negedge rst_n) begin if(!rst_n) begin led <= 4'd0; end else if (flag) begin led <= 4'b1111; end else if (!flag) begin led <= 4'd0; end else begin led <= led; end end endmodule
该模块用于按键消抖,代码如下
module key_debounce( input wire clk, input wire rst_n, input wire key, output reg flag,// 0抖动, 1抖动结束 output reg key_value//key抖动结束后的值 ); reg [19:0] delay_cnt;//1_000_000 reg key_reg;//key上一次的值 //20ms计数器 always @(posedge clk or negedge rst_n) begin if(!rst_n) begin key_reg <= 1; delay_cnt <= 0; end else begin key_reg <= key; //当key为1 key 为0 表示按下抖动,开始计时 if(key_reg != key ) begin delay_cnt <= 20'd1_000_000 ; //仿真 // delay_cnt <= 20'd10 ; end else begin if(delay_cnt > 0) delay_cnt <= delay_cnt - 20'd1; else delay_cnt <= 0; end end end //当计时完成,获取key的值 always @(posedge clk or negedge rst_n) begin if(!rst_n) begin flag <= 0; key_value <= 1; end else begin // 计时完成 处于稳定状态,进行赋值 if(delay_cnt == 1) begin flag <= 1; key_value <= key; end else begin flag <= 0; key_value <= key_value; end end end endmodule
把原来的training.v代码替换为如下代码
module training ( input wire clk, input wire rst_n, input wire [1:0]key, output wire [3:0]led ); //training_1#(.MAX_COUNT (75_000_000)) instance_t1 ( //. clk (clk), //. rst_n (rst_n), //. led (led) //); wire [1:0] flag; wire [1:0] key_value; key_debounce inst_key_debounce_key1( .clk (clk ), .rst_n (rst_n ), .key (key[0] ), .flag (flag[0] ), .key_value (key_value[0] ) ); key_debounce inst_key_debounce_key2( .clk (clk ), .rst_n (rst_n ), .key (key[1] ), .flag (flag[1] ), .key_value (key_value[1] ) ); training_2 instance_t2( . clk (clk), . rst_n (rst_n), . key ({key_value[0] && flag[0],key_value[1] && flag[1]}), . led (led) ); endmodule
仿真文件模拟按键状态
`timescale 1ns/1ns // 时间单位/时间精度 module training_tb; parameter CYCLE = 20; reg clk; reg rst_n; reg [1:0]key; wire [3:0]led; always #(CYCLE/2) clk = ~clk; //初始化 initial begin clk = 1'b1; rst_n = 1'b0; key =2'b00; #CYCLE ; rst_n = 1'b1; //1 #CYCLE key = 2'b10; #CYCLE key = 2'b00; //0 #CYCLE key = 2'b01; #CYCLE key = 2'b00; //0 #CYCLE key = 2'b01; #CYCLE key = 2'b00; //1 #CYCLE key = 2'b10; #CYCLE key = 2'b00; //0 #CYCLE key = 2'b01; #CYCLE key = 2'b00; #100; $stop; end 实例化 //training_1#(.MAX_COUNT(75)) inst_training( //.clk (clk ), //.rst_n (rst_n ), // //.led (led) //); //实例化 training_2 inst_training( .clk (clk ), .rst_n (rst_n ), .key (key ), .led (led) ); endmodule
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。