verilog设计一个数字时钟

源程序:

module clock(clk,rst,h1,h2,m1,m2,s1,s2);

  input clk,rst;
  output h1,h2,m1,m2,s1,s2;  // 1表示十位,2表示个位
  reg [3:0]h1,h2,m1,m2,s1,s2;
  
  always@(posedge clk)
    begin  
    if(!rst) s2<=0;  
    else if(s2==9)  
      s2<=0;  
    else  
      s2<=s2+1; 
    end 
  always@(posedge clk)
    begin  
    if(!rst) s1<=0;  
    else if(s2==9)  
      begin  
      if(s1==5)  
         s1<=0;  
      else  
         s1<=s1+1;  
      end  
    end  
  
  
  always@(posedge clk)
    begin  
      if(!rst) m2<=0;  
      else if(s1==5&&s2==9)
        begin 
        if(m2==9)
          m2<=0;
        else
          m2<=m2+1;
        end
    end
  always@(posedge clk)
    begin
      if(!rst) m1<=0;
      else if(s1==5&&s2==9)
        begin
          if(m2==9)
            begin
              if(m1==5)
              m1<=0;
            else
            m1<=m1+1;
            end
        end
    end  
    
  always@(posedge clk)
    begin
      if(!rst) h2<=0;
       else if((s1==5&&s2==9)&&(m1==5&&m2==9))
         begin
           if(h1==2&&h2==3)
             h2<=0;
           else 
             if(h2==9)  
               h2<=0;
             else
               h2<=h2+1;
          end
     end
  always@(posedge clk)
     begin  
       if(!rst)  
             h1<= 0;  
       else if((s1==5&&s2==9)&&(m1==5&&m2==9))  
       begin  
           if((h1==2)&&(h2==3))
             h1<= 0;
           else if(h2==9)  
             h1<=h1+1;  
       end  
     end  
     

endmodule


测试:

module clock_tb();  
  reg clk,rst;  
  wire[3:0] h1,h2,m1,m2,s1,s2;  
  clock u1(clk,rst,h1,h2,m1,m2,s1,s2) ; 


  always #5 clk=~clk;  
  initial  
  begin  
    clk=0;
    rst=0;
    #10 rst=1;  
  end  

endmodule


仿真:




  • 22
    点赞
  • 168
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值