verilog实现时钟的时和分,其中时为24进制,分为60进制

使用modelsim编写verilog程序如下:

module digitalclock (clk,reset,hour_g,hour_d,minute_g,minute_d,cout);
  
input         clk,reset;
output[3:0]   hour_g,hour_d,minute_g,minute_d;
output        cout;
reg[3:0]      hour_g,hour_d,minute_g,minute_d;
wire          cout;

always @(posedge clk)
begin
  if(~reset)
        minute_d <= 0;
  else if(minute_d==9)
        minute_d <= 0;
  else 
        minute_d <= minute_d + 1;
end

always @(posedge clk)
begin
  if(~reset)
        minute_g <= 0;
  else if(minute_d==9)
  begin
      if(minute_g==5)
        minute_g <= 0;
      else 
        minute_g <= minute_g + 1;
  end
end

assign cout = ((minute_d==9)&&(minute_g==5))?1:0;

always @(posedge clk)
begin
  if(~reset)
        hour_d <= 0;
  else if(cout)
  begin
      if(hour_d==9)
        hour_d <= 0;
      else if((hour_d==3)&&(hour_g==2))
        hour_d <= 0;
      else
        hour_d <= hour_d + 1;
  end
end

always @(posedge clk)
begin
  if(~reset)
        hour_g <= 0;
  else if(cout)
  begin
      if((hour_d==3)&&(hour_g==2))
        hour_g <= 0;
      else if(hour_d==9)
        hour_d <= hour_d + 1;
  end
end

endmodule

使用modelsim仿真还需要测试程序,程序如下:

module test;
  
reg clk, reset;
wire[3:0]   hour_g,hour_d,minute_g,minute_d;
wire        cout;
parameter cycle = 100;

digitalclock u1(.clk(clk),
                .reset(reset),
                .hour_g(hour_g),
                .hour_d(hour_d),
                .minute_g(minute_g),
                .minute_d(minute_d),
                .cout(cout));
                
always 
  #(cycle/2)  clk = ~clk;
  
initial
begin
  clk = 0;
  reset = 1;
#305  reset = 0;
#450  reset = 1;
end

endmodule 
如此,就可以实现时钟的时和分,并且可以仿真出波形。

  • 5
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值