当前位置:   article > 正文

【FPGA】verilog流水灯实验_verilog拨码开关控制led

verilog拨码开关控制led

用三个拨码开关去控制7个LED灯,具体要求:
拨下000七个灯全灭;
拨下001只亮第一个灯,按1s的频率闪烁;
拨下010前两个灯以1s的频率依次闪烁,即LED0亮1s,切换到LED1亮1s,然后切换到LED0亮1s。

拨下111七个灯以1s的频率依次闪烁,即LED0亮1s,切换到LED1亮1s,切换到LED2亮1s……切换LED6亮1s,切换到LED0亮1s。

实现代码:

module LED_run #(
//    parameter [26:0] change_time = 27'h5f5_e0ff
	parameter [26:0] change_time = 27'h5
)
(
	input clk_p,
	input clk_n,
	input reset,
	input [2:0] switch,
	output [6:0] LED
);

//使用了差分时钟
    IBUFDS #(
      .DIFF_TERM("FALSE"),       // Differential Termination
      .IBUF_LOW_PWR("TRUE"),     // Low power="TRUE", Highest performance="FALSE" 
      .IOSTANDARD("DEFAULT")     // Specify the input I/O standard
   ) 
    IBUFDS_inst (
      .O(clk),  // Buffer output
      .I(clk_p),  // Diff_p buffer input (connect directly to top-level port)
      .IB(clk_n) // Diff_n buffer input (connect directly to top-level port)
   ); 
	
	reg [26:0] timer;
	reg [6:0] LED_tmp;
	reg [2:0] switch_reg;
	
	always @(posedge clk or negedge reset) begin
		if(!reset)
			timer <= 27'h0;
		else if(timer == change_time | switch != switch_reg)
			timer <= 27'h0;
		else
			timer <= timer + 1;
	end
	
	always @(posedge clk or negedge reset) begin
		if (!reset)
			LED_tmp <= 7'h00;
		else if(switch != switch_reg) begin
		  LED_tmp <= ((switch == 3'b000)? 7'h00:7'h01);
		end
		else if(timer == change_time) begin
			case(switch)
				3'b000:		LED_tmp <= LED_tmp;
				3'b001:		LED_tmp <= {LED_tmp[6:5], ~LED_tmp[0]};
				3'b010:		LED_tmp <= {LED_tmp[6:2], LED_tmp[0], LED_tmp[1]};
				3'b011:		LED_tmp <= {LED_tmp[6:3], LED_tmp[1:0], LED_tmp[2]};
				3'b100:		LED_tmp <= {LED_tmp[6:4], LED_tmp[2:0], LED_tmp[3]};
				3'b101:		LED_tmp <= {LED_tmp[6:5], LED_tmp[3:0], LED_tmp[4]};
				3'b110:		LED_tmp <= {LED_tmp[6], LED_tmp[4:0], LED_tmp[5]};
				3'b111:		LED_tmp <= {LED_tmp[5:0], LED_tmp[6]};
				default:    LED_tmp <= 7'h00;
			endcase
		end
    end
	
	always @(posedge clk or negedge reset) begin
        if(!reset)
            switch_reg <= 3'b000;
        else
            switch_reg <= switch;
	end
	
	assign LED = LED_tmp;

endmodule
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

测试代码:

`timescale 1ns/1ps

`define clk_period 10

module LED_tb;

	reg Clk_p;
	reg Clk_n;
	reg rst_tb;
	reg [2:0] switch_tb;
	wire	[6:0] LED_tb;
//	reg [26:0] sec = 27'h5f5_e0ff;
    reg [26:0] sec = 27'h5;
	
	LED_run LED_run1(
		.clk_p(Clk_p),
		.clk_n(Clk_n),
		.rst_n(rst_tb),
		.switch(switch_tb),
		.LED(LED_tb)
	);
	
	initial begin
		Clk_p = 0;
		Clk_n = 1;
		rst_tb = 1'b0;
		switch_tb = 3'b000;
		#50;
		rst_tb = 1'b1;
		#50;
		switch_tb = 3'b000;
		#(sec * 10);
		switch_tb = 3'b001;
		#(sec * 25);
		switch_tb = 3'b010;
		#(sec * 25);
		switch_tb = 3'b011;
		#(sec * 45);
		switch_tb = 3'b100;
		#(sec * 65);
		switch_tb = 3'b101;
		#(sec * 85);
		switch_tb = 3'b110;
		#(sec * 105);
		switch_tb = 3'b111;
		#(sec * 125);
		switch_tb = 3'b000;
		#(sec * 145);
		$stop;
	end
	
	always #(`clk_period/2) Clk_p = ~Clk_p;
	always #(`clk_period/2) Clk_n = ~Clk_n;
endmodule
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

仿真波形:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/726097
推荐阅读
相关标签
  

闽ICP备14008679号