呼吸灯小项目
功能:设计一个周期为2s呼吸灯,从暗到亮。
2s=2000ms 2ms=200us,2us=2000ns
下面是时序图
当cnt_2ms大于cnt_2us的时候,led赋值为1,其他时候赋值为0;
module huxideng_1(clk,rst_n,led);
input clk,rst_n;
output reg led;
reg [9:0] cnt_2us; //count 2us
reg [9:0] cnt_2ms; //count 2ms
reg [6:0] cnt; //定义计数值
//cnt 2ms=100*2us
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
cnt <=0;
else if(cnt==99)
cnt <=0;
else
cnt <=cnt+1;
end
//cnt_2us
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
cnt_2us<=0;
else if((cnt==99)&&(cnt_2us==999))
cnt_2us<=0;
else if(cnt==99)
cnt_2us<=cnt_2us+1;
else
cnt_2us<=cnt_2us;
end
//cnt_2ms
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
cnt_2ms<=0;
else if((cnt_2us==999)&&(cnt==99)&&(cnt_2ms==999))
cnt_2ms<=0;
else if((cnt_2us==999)&&(cnt==99))
cnt_2ms<=cnt_2ms+1;
else
cnt_2ms<=cnt_2ms;
end
//led
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
led<=1'b0;
else if(cnt_2ms>cnt_2us)
led<=1'b1;
else
led<=1'b0;
end
endmodule
testbench
`timescale 1ns/1ns
module huxideng_1_tb;
reg clk;
reg rst_n;
wire led;
huxideng_1 inst(
.clk(clk),
.rst_n(rst_n),
.led(led)
);
initial
begin
clk=0;
rst_n=0;
#100
rst_n=1;
end
always #10 clk=~clk;
endmodule
上述是呼吸灯的全部代码呦!!!
下一篇:数码管静态显示程序