verilog时钟计数器

题目:
Create a set of counters suitable for use as a 12-hour clock (with am/pm indicator). Your counters are clocked by a fast-running clk, with a pulse on ena whenever your clock should increment (i.e., once per second).

reset resets the clock to 12:00 AM. pm is 0 for AM and 1 for PM. hh, mm, and ss are two BCD (Binary-Coded Decimal) digits each for hours (01-12), minutes (00-59), and seconds (00-59). Reset has higher priority than enable, and can occur even when not enabled.

The following timing diagram shows the rollover behaviour from 11:59:59 AM to 12:00:00 PM and the synchronous reset and enable behaviour.
在这里插入图片描述Note that 11:59:59 PM advances to 12:00:00 AM, and 12:59:59 PM advances to 01:00:00 PM. There is no 00:00:00.
代码:

 module top_module(
    input clk,
    input reset,
    input ena,
    output pm,
    output [7:0] hh,
    output [7:0] mm,
    output [7:0] ss); 
    
    reg en1,en2,en3,en4;
    //分和秒的例化
    count1 c1(clk,reset,ena,ss[3:0],en1);
    count2 c2(clk,reset,en1,ss[7:4],en2);
    count1 c3(clk,reset,en2,mm[3:0],en3);
    count2 c4(clk,reset,en3,mm[7:4],en4);
    //时计数
    always@(posedge clk)begin
        if(reset)
            hh<=8'h12;
        else begin
            if(en4)
                if(hh==8'h12)
                    hh<=8'h01;
                    else begin
                if(hh[3:0]==4'd9)
                    hh<=8'h10;
                else
                    hh[3:0]<=hh[3:0]+1;
                    end
        end       
      end
      //pm值
    always@(posedge clk)begin
        if(reset)
            pm<=0;
        else 
            if(en4&hh==8'h11)
               pm<=~pm;
            else;
    end
    endmodule

//例化模块0-9
module count1(
   input clk,
   input reset,
   input in,
    output [4:0]out,
    output en);
    always@(posedge clk)begin
        if(reset)
            out<=0;
        else begin
            if(in)begin
                if(out==4'd9)
                    out<=0;
               else
                   out<=out+1;
            end
            else
               out<=out; 
endmodule
        end
    end
    assign en=out[3]&out[0]&in;
endmodule
//例化模块0-5
module count2(
   input clk,
   input reset,
   input in,
    output [4:0]out,
    output en);
    always@(posedge clk)begin
        if(reset)
            out<=0;
        else begin
            if(in)begin
                if(out==4'd5)
                    out<=0;
               else
                   out<=out+1;
            end
            else
               out<=out; 
        end
    end
    assign en=out[2]&out[0]&in;
endmodule
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值