目录
Led实验进化六部曲
0、Led灯按照亮0.25秒,灭0.75秒的状态循环亮灭
1、Led灯按照亮0.25秒,灭0.5秒,亮0.75秒,灭1秒的状态循环亮灭
2、Led灯按照指定的模式亮灭,模式未知且由用户随机指定。以0.25秒为一个变化周期,8个变化状态为一个循环
3、Led灯按照指定的模式亮灭,模式未知且由用户随机指定。8个变化状态为一个循环,每个变化状态的时间值可以根据不同的应用场景选择
4、多个LED灯按照设置的模式各自在一个变化循环内独立亮灭变化
5、每隔10ms让Led灯的一个8状态循环执行一次(每个状态的变化时间值小一点,方便测试,比如设置为10ns)
0
仿真波形
设计文件程序
module counter_led_zero(
input Clk,
input Reset_n,
output reg Led
);
reg [25:0]counter;
parameter MCNT = 25'd50000000;
always @(posedge Clk or negedge Reset_n) begin
if(!Reset_n)
counter <= 0;
else if(counter == MCNT - 1)
counter <= 0;
else
counter <= counter + 1'd1;
end
always @(posedge Clk or negedge Reset_n) begin
if(!Reset_n)
Led <= 0;
else if(counter == (MCNT*3)/4 - 1)
Led <= 1;
else if(counter == MCNT - 1)
Led <= 0;
end
endmodule
仿真文件程序
`timescale 1ns / 1ns
module counter_led_zero_tb();
reg Clk;
reg Reset_n;
wire Led;
counter_led_zero counter_led_zero_test(
.Clk(Clk),
.Reset_n(Reset_n),
.Led(Led)
);
defparam counter_led_zero_test.MCNT = 25'd50000;
initial Clk <= 1;
always #10 Clk <= !Clk;
initial begin
Reset_n <= 0;
#200;
Reset_n <= 1;
#2000000;
end
endmodule
1
仿真波形
设计文件程序
module counter_led_one(
input Clk,
input Reset_n,
output reg Led
);
reg [26:0]counter;
parameter MCNT = 26'd125000000;
always @(posedge Clk or negedge Reset_n) begin
if(!Reset_n)
counter <= 0;
else if(counter == MCNT - 1)
counter <= 0;
else
counter <= counter + 1'd1;
end
always @(posedge Clk or negedge Reset_n) begin
if(!Reset_n)
Led <= 1;
else if(counter == MCNT/10 - 1)
Led <= 0;
else if(counter == MCNT/10 + MCNT/5 - 1)
Led <= 1;
else if(counter ==(MCNT/10 + MCNT/5)*2 - 1)
Led <= 0;
else if(counter == MCNT - 1)
Led <= 1;
end
endmodule
2
仿真波形
设计文件程序
Method_1
module counter_led_two(
input Clk,
input Reset_n,
input [7:0] sel,
output reg Led
);
reg [27:0]counter;
parameter MCNT = 28'd100000000;
always @(posedge Clk or negedge Reset_n) begin
if(!Reset_n)
counter <= 0;
else if(counter == MCNT - 1)
counter <= 0;
else
counter <= counter + 1'd1;
end
always @(posedge Clk or negedge Reset_n) begin
if(!Reset_n)
Led <= 0;
else if(counter == MCNT/8 - 1)
Led <= sel[0];
else if(counter == MCNT*2/8 - 1)
Led <= sel[1];
else if(counter == MCNT*3/8 - 1)
Led <= sel[2];
else if(counter == MCNT*4/8 - 1)
Led <= sel[3];
else if(counter == MCNT*5/8 - 1)
Led <= sel[4];
else if(counter == MCNT*6/8 - 1)
Led <= sel[5];
else if(counter == MCNT*7/8 - 1)
Led <= sel[6];
else if(counter == MCNT - 1)
Led <= sel[7];
end
endmodule
Method_2
module counter_led_two(
input Clk,
input Reset_n,
input [7:0] sel,
output reg Led
);
reg [27:0]counter;
parameter MCNT = 28'd100000000;
always @(posedge Clk or negedge Reset_n) begin
if(!Reset_n)
counter <= 0;
else if(counter == MCNT - 1)
counter <= 0;
else
counter <= counter + 1'd1;
end
always @(posedge Clk or negedge Reset_n) begin
if(!Reset_n)
Led <= 0;
else case(counter)
MCNT*1/8 - 1: Led <= sel[0];
MCNT*2/8 - 1: Led <= sel[1];
MCNT*3/8 - 1: Led <= sel[2];
MCNT*4/8 - 1: Led <= sel[3];
MCNT*5/8 - 1: Led <= sel[4];
MCNT*6/8 - 1: Led <= sel[5];
MCNT*7/8 - 1: Led <= sel[6];
MCNT*8/8 - 1: Led <= sel[7];
default:Led <= Led;
endcase
end
endmodule
仿真文件程序
`timescale 1ns / 1ns
module counter_led_zero_tb();
reg Clk;
reg Reset_n;
reg [7:0]sel;
wire Led;
counter_led_two counter_led_zero_test(
.Clk(Clk),
.Reset_n(Reset_n),
.sel(sel),
.Led(Led)
);
defparam counter_led_zero_test.MCNT = 25'd100000;
initial Clk <= 1;
always #10 Clk <= !Clk;
initial begin
Reset_n <= 0;
sel = 0;
#200;
Reset_n <= 1;
#2000;
sel = 8'b1001_1010;
$stop;
end
endmodule
3
仿真波形
设计文件程序
module counter_led_three(
input Clk,
input Reset_n,
input [7:0] sel,
input [31:0] Time,
output reg Led
);
reg [31:0]counter;
always @(posedge Clk or negedge Reset_n) begin
if(!Reset_n)
counter <= 0;
else if(counter == Time - 1)
counter <= 0;
else
counter <= counter + 1'd1;
end
reg [2:0]counter2;
always @(posedge Clk or negedge Reset_n) begin
if(!Reset_n)
counter2 <= 0;
else if(counter == Time - 1)
counter2 <= counter2 + 1'b1;
end
always @(posedge Clk or negedge Reset_n) begin
if(!Reset_n)
Led <= 0;
else case(counter2)
0: Led <= sel[0];
1: Led <= sel[1];
2: Led <= sel[2];
3: Led <= sel[3];
4: Led <= sel[4];
5: Led <= sel[5];
6: Led <= sel[6];
7: Led <= sel[7];
default:Led <= Led;
endcase
end
endmodule
仿真文件程序
`timescale 1ns / 1ns
module counter_led_three_tb();
reg Clk;
reg Reset_n;
reg [7:0]sel;
reg [31:0] Time;
wire Led;
counter_led_three counter_led_three_test(
.Clk(Clk),
.Reset_n(Reset_n),
.sel(sel),
.Time(Time),
.Led(Led)
);
initial Clk <= 1;
always #10 Clk <= !Clk;
initial begin
Reset_n = 0;sel = 0;Time = 0;
#200;
Reset_n = 1;
#2000;
Time = 2500;
sel = 8'b1001_1010;
#20000000;
sel = 8'b1011_1110;
#20000000;
$stop;
end
endmodule
4
仿真波形
设计文件程序
module counter_led_four(
input Clk,
input Reset_n,
input [7:0] sel_A,
input [7:0] sel_B,
input [31:0] Time,
output reg [1:0]Led
);
reg [31:0]counter;
always @(posedge Clk or negedge Reset_n) begin
if(!Reset_n)
counter <= 0;
else if(counter == Time - 1)
counter <= 0;
else
counter <= counter + 1'd1;
end
reg [2:0]counter2;
always @(posedge Clk or negedge Reset_n) begin
if(!Reset_n)
counter2 <= 0;
else if(counter == Time - 1)
counter2 <= counter2 + 1'b1;
end
always @(posedge Clk or negedge Reset_n) begin
if(!Reset_n)
Led <= 0;
else case(counter2)
0: begin Led[0] <= sel_A[0]; Led[1] <= sel_B[0]; end
1: begin Led[0] <= sel_A[1]; Led[1] <= sel_B[1]; end
2: begin Led[0] <= sel_A[2]; Led[1] <= sel_B[2]; end
3: begin Led[0] <= sel_A[3]; Led[1] <= sel_B[3]; end
4: begin Led[0] <= sel_A[4]; Led[1] <= sel_B[4]; end
5: begin Led[0] <= sel_A[5]; Led[1] <= sel_B[5]; end
6: begin Led[0] <= sel_A[6]; Led[1] <= sel_B[6]; end
7: begin Led[0] <= sel_A[7]; Led[1] <= sel_B[7]; end
default:Led <= Led;
endcase
end
endmodule
仿真文件程序
`timescale 1ns / 1ns
module counter_led_four_tb();
reg Clk;
reg Reset_n;
reg [7:0]sel_A;
reg [7:0]sel_B;
reg [31:0]Time;
wire [1:0]Led;
counter_led_four counter_led_four_test(
.Clk(Clk),
.Reset_n(Reset_n),
.sel_A(sel_A),
.sel_B(sel_B),
.Time(Time),
.Led(Led)
);
initial Clk <= 1;
always #10 Clk <= !Clk;
initial begin
Reset_n = 0;sel_A = 0;sel_B = 0;Time = 0;
#200;
Reset_n = 1;
#2000;
Time = 2500;
sel_A = 8'b1001_1010;sel_B = 8'b1011_1110;
#20000000;
$stop;
end
endmodule
5
仿真波形
设计文件程序
module counter_led_five_xmg(
input Clk,
input Reset_n,
input [7:0] sel,
input [31:0] Time,
output reg Led
);
reg [19:0]counter0;
reg [31:0]counter;
reg EN;
always @(posedge Clk or negedge Reset_n) begin
if(!Reset_n)
counter0 <= 0;
else if(counter0 == 500000 - 1)
counter0 <= 0;
else
counter0 <= counter0 + 1'b1;
end
always @(posedge Clk or negedge Reset_n) begin
if(!Reset_n)
EN <= 0;
else if(counter0 == 0)
EN <= 1;
else if((counter2 == 7)&&(counter == Time - 1))
EN <= 0;
end
always @(posedge Clk or negedge Reset_n) begin
if(!Reset_n)
counter <= 0;
else if(EN == 1)begin
if(counter == Time - 1)
counter <= 0;
else
counter <= counter + 1'd1;
end
else
counter <= 0;
end
reg [2:0]counter2;
always @(posedge Clk or negedge Reset_n) begin
if(!Reset_n)
counter2 <= 0;
else if(EN == 1)begin
if(counter == Time - 1)
counter2 <= counter2 + 1'b1;
end
else
counter2 <= 0;
end
always @(posedge Clk or negedge Reset_n) begin
if(!Reset_n)
Led <= 0;
else case(counter2)
0: Led <= sel[0];
1: Led <= sel[1];
2: Led <= sel[2];
3: Led <= sel[3];
4: Led <= sel[4];
5: Led <= sel[5];
6: Led <= sel[6];
7: Led <= sel[7];
default:Led <= Led;
endcase
end
endmodule
仿真文件程序
`timescale 1ns / 1ns
module counter_led_five_xmg_tb();
reg Clk;
reg Reset_n;
reg [7:0]sel;
reg [31:0] Time;
wire Led;
counter_led_five_xmg counter_led_five_xmg_test(
.Clk(Clk),
.Reset_n(Reset_n),
.sel(sel),
.Time(Time),
.Led(Led)
);
initial Clk <= 1;
always #10 Clk <= !Clk;
initial begin
Reset_n = 0;sel = 0;Time = 0;
#200;
Reset_n = 1;
#2000;
Time = 2500;
sel = 8'b1011_1110;
#20000000;
$stop;
end
endmodule