1.移位实现流水灯
module led_run1(
Clk,
Reset_n,
Led
);
input Clk;
input Reset_n;
output reg[7:0]Led;
reg [24:0]counter;
always@(posedge Clk or negedge Reset_n)
if(!Reset_n)
counter <= 0;
// else if(counter == 25'd24999999)
else if(counter == 25'd24999)
counter <= 0;
else
counter <= counter + 1'b1;
always@(posedge Clk or negedge Reset_n)
if(!Reset_n)
Led <= 8'b0000_0001;
// else if(counter == 25'd24999999)begin
else if(counter == 25'd24999)begin
Led <= {Led[6:0],Led[7]};
end
else
Led <= Led;
endmodule
关键代码:
Led <= {Led[6:0],Led[7]}
2.38译码器实现流水灯
首先确定电路结构与连线
电路功能:每过半秒钟Q的输出清零
代码结构:
计数器半秒钟——触发器Q的输出——decoder的例化
然后需要把decoder模块的拿进来连线例化
module led_run(
Clk,
Reset_n,
Led
);
input Clk;
input Reset_n;
output reg[7:0]Led;
//计数半秒钟
reg [24:0]counter;
parameter MCNT = 25000000-1;
always@(posedge Clk or negedge Reset_n)
if(!Reset_n)
counter <= 0;
else if(counter == MCNT)
// else if(counter == 25'd24999)
counter <= 0;
else
counter <= counter + 1'b1;
//decoder的三位输入
reg [2:0] counter2;
always@(posedge Clk or negedge Reset_n)
if(!Reset_n)
counter2 <= 0;
else if(counter == MCNT)
counter2 <= counter2 + 1'b1;
//例化decoder
decoder_3_8 decoder_3_8_inst0(
.a(counter2[2]),
.b(counter2[1]),
.c(counter2[0]),
.out(Led)
);
endmodule
参数化decoeder模块的实例化
parameter WIDTH = 4;
input a;
input b;
input c;
output [WIDTH - 1:0] out;
reg [WIDTH - 1:0]out;
decoder_3_8
#(.WIDTH(8))
decoder_3_8_inst0
(
.a(counter2[2]),
.b(counter2[1]),
.c(counter2[0]),
.out(Led)
);
仿真时可以改变参数
led_run
#(
.MCNT(2500-1)
)
led_run_inst0(
.Clk(Clk),
.Reset_n(Reset_n),
.Led(Led)
);