呼吸灯看它就够 包括源代码,测试代码,经过上板验证成功

呼吸灯小项目

  功能:设计一个周期为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

 上述是呼吸灯的全部代码呦!!!
下一篇:数码管静态显示程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值