赞
踩
八个LED灯以每个0.5s的频率闪烁
- //八个LED灯以每个0.5s的频率闪烁
- //移位法
- module led_run1(
- Clk,
- Reset_n,
- Led
- );
- input Clk;
- input Reset_n;
- output reg [7:0]Led;
-
- reg [24:0]counter;//计数器计数0.5s
-
- always@(posedge Clk or negedge Reset_n)
- if(!Reset_n)
- counter <= 0;
- // else if(counter == 24_999_999)
- else if(counter == 24_999)
- counter <= 0;
- else
- counter <= counter + 1'b1;
-
- always@(posedge Clk or negedge Reset_n)
- if(!Reset_n)
- Led <= 8'b0000_0001;
- //else if(counter == 24_999_999)
- else if(counter == 24_999)
- begin
- if(Led == 8'b1000_0000)
- Led <= 8'b0000_0001; //当LED移到最左一位1000_0000时,手动将其设置为0000_0001
- else
- Led <= Led << 1;
- end
- else
- Led <= Led;
-
- endmodule
-
-
- `timescale 1ns/1ns
-
- module tb_led_run_1;
-
- reg Clk;
- reg Reset_n;
- wire [7:0] Led;
-
- led_run1 led_run1(
- .Clk(Clk),
- .Reset_n(Reset_n),
- .Led(Led)
- );
-
- initial Clk=1;
- always #10 Clk=!Clk;
-
- initial begin
- Reset_n=0;
- #150;
- Reset_n=1;
- #40000000;
- $stop;
- end
- endmodule
八个LED灯以每个0.5s的频率闪烁
循环移位
- //八个LED灯以每个0.5s的频率闪烁
- //移位法 循环移位
- module led_run2(
- Clk,
- Reset_n,
- Led
- );
- input Clk;
- input Reset_n;
- output reg [7:0]Led;
-
- reg [24:0]counter;//计数器计数0.5s
-
- always@(posedge Clk or negedge Reset_n)
- if(!Reset_n)
- counter <= 0;
- // else if(counter == 24_999_999)
- else if(counter == 24_999)
- counter <= 0;
- else
- counter <= counter + 1'b1;
-
- always@(posedge Clk or negedge Reset_n)
- if(!Reset_n)
- Led <= 8'b0000_0001;
- //else if(counter == 24_999_999)
- else if(counter == 24_999)
- Led <= {Led[6:0],Led[7]};
- //每当符合一个移位条件,用第七位的值代替第六位,第六位的值代替第五位,依次类推,实现循环移位
- else
- Led <= Led;
-
- endmodule
- `timescale 1ns/1ns
-
- module tb_led_run_2;
-
- reg Clk;
- reg Reset_n;
- wire [7:0] Led;
-
- led_run1 led_run1(
- .Clk(Clk),
- .Reset_n(Reset_n),
- .Led(Led)
- );
-
- initial Clk=1;
- always #10 Clk=!Clk;
-
- initial begin
- Reset_n=0;
- #150;
- Reset_n=1;
- #40000000;
- $stop;
- end
- endmodule
八个LED灯以每个0.5s的频率闪烁
调用模块
-
-
- //三八译码器代码
- module decoder_3_8(
- a,
- b,
- c,
- out
- );
- input a;
- input b;
- input c;
- output reg [7:0]out;
- //reg [7:0] out;
-
- // {a,b,c}变成一个三位的信号,这种操作叫位拼接
- // 位拼接,其中可用常量
- // wire [3:0]d;
- // assign d = {a,1'b0,b,c}
-
- //以alwayse块描述的信号,被赋值对象必须定义为reg类型
- always@(*)begin
- case({a,b,c})
- 3'b000 : out = 8'b0000_0001;
- 3'b001 : out = 8'b0000_0010;
- 3'b010 : out = 8'b0000_0100;
- 3'b011 : out = 8'b0000_1000;
- 3'b100 : out = 8'b0001_0000;
- 3'b101 : out = 8'b0010_0000;
- 3'b110 : out = 8'b0100_0000;
- 3'b111 : out = 8'b1000_0000;
- endcase
- end
-
- endmodule
-
- //跑马灯设计
-
- module led_run3(
- Clk,
- Reset_n,
- Led
- );
- input Clk;
- input Reset_n;
- output [7:0]Led;
- //因为Led由底层模块三八译码器驱动,已在底层模块设置为reg型,所以不能在该顶层模块再设置
-
- parameter MCNT = 25'd24_999_999;
-
- reg [24:0]counter;//计数器计数0.5s
-
-
- always@(posedge Clk or negedge Reset_n)
- if(!Reset_n)
- counter <= 0;
- else if(counter == MCNT)
- counter <= 0;
- else
- counter <= counter + 1'b1;
-
- reg [2:0] counter2;
- always@(posedge Clk or negedge Reset_n)
- if(!Reset_n)
- counter2 <= 0;
- // else if(counter2 == 7)
- // counter2 <= 0;
- // 因为计数器共八位,计数到7后自动溢出,所以 else if 可不写
- else
- counter2 <= counter2 + 1'b1;
-
- //例化方式与tb中一样
- decoder_3_8 decoder_3_8(
- .a(counter2[2]),
- .b(counter2[1]),
- .c(counter2[0]),
- .out(Led)
- );
-
- endmodule
- `timescale 1ns/1ns
-
- module tb_led_run_3;
-
- reg Clk;
- reg Reset_n;
- wire [7:0] Led;
-
- led_run3
- #(
- .MCNT(2499)
- )
- led_run3_ints0(
- .Clk(Clk),
- .Reset_n(Reset_n),
- .Led(Led)
- );
- defparam led_run3_ints0.MCNT = 24999;
-
- initial Clk=1;
- always #10 Clk=!Clk;
-
- initial begin
- Reset_n=0;
- #150;
- Reset_n=1;
- #40000000;
- $stop;
- end
- endmodule
端口位宽也可用参数化设计
parameter WIDTH = 4;
output [ WIDTH - 1 : 0 ] out;
reg [ WIDTH - 1 : 0 ] out;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。