用Verilog实现12_hour_clock(HDLbits的Count clock题)

Title:Count clock
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.
题目的意思就是创建一个十二小时的时钟,2位BCD码(8位宽)hh、mm、ss分别表示时分秒,pm则表示上午、下午(当pm=0便是上午,pm=1表示下午),然后同步复位reset=1’b1使能,将时钟重置为12:00:00,同时使能位ena=1’b1表示时钟的运行,而且reset优先级大于ena。
时序图如下:
在这里插入图片描述
Module Declaration
module top_module(
input clk,
input reset,
input ena,
output pm,
output [7:0] hh,
output [7:0] mm,
output [7:0] ss
);

废话不多说,直接上代码

module top_module(
    input clk,
    input reset,
    input ena,
    output reg pm,
    output [7:0] hh,
    output [7:0] mm,
    output [7:0] ss
    ); 
    
    wire ss_en,mm_en,hh_en;
    assign ss_en = ena;
    assign mm_en = ss_en&(ss == 8'h59);
    assign hh_en = mm_en&(mm == 8'h59);
    
    mm_ss u0_mm_ss(clk,reset,ss_en,ss);    
    mm_ss u1_mm_ss(clk,reset,mm_en,mm);
    hh u_hh(clk,reset,hh_en,hh);
    
    always@(posedge clk)begin
        if(reset)
        	pm <= 1'b0;
        else begin
            if((hh == 8'h11)&(mm == 8'h59)&(ss == 8'h59)) //当12:00:00翻转pm
            	pm <= ~pm;
            else
                pm <= pm;
        end
    end  
endmodule

//时模块
module hh(
	input            clk  ,
    input            reset,
    input            en   ,
    output  [7:0]    hh
);
    wire [3:0] counter1,counter0;
    assign hh = {counter1,counter0};
    h0_bcdcounter u_h0_bcdcounter(clk,reset,en,counter1,counter0);
    h1_bcdcounter u_h1_bcdcounter(clk,reset,(en&((hh==8'h12)|(counter0==4'h9))),counter1);//当九点或十二点使能来时,翻转时十位;
    
endmodule  

//分秒模块
module mm_ss(
	input            clk  ,
    input            reset,
    input            en   ,
    output  [7:0]    mm_ss
);
    wire [3:0] counter1,counter0;
    assign mm_ss = {counter1,counter0};
    bcdcounter u0_bcdcounter(clk,reset,en,4'h0,4'h9,counter0);
    bcdcounter u1_bcdcounter(clk,reset,(en&(counter0==4'h9)),4'h0,4'h5,counter1);
endmodule 

//bcd码计数
module bcdcounter(
	input            clk     ,
    input            reset   ,
    input            en      ,
    input      [3:0] resetnum, //这里设置本来想时也可以例化这个,结果发现不可以
    input      [3:0] loadtnum,
    output reg [3:0] counter
);
    always@(posedge clk)begin
        if(reset)
            counter <=resetnum;
        else begin
            if(en)begin
                if(counter == loadtnum)
                	counter <= 4'h0;
                else
                	counter <= counter + 1'b1;
            end
            else
            	counter <= counter;
        end
    end
endmodule    

//时个位
module h0_bcdcounter( 
	input            clk     ,
    input            reset   ,
    input            en      ,
    input      [3:0] h1      ,    
    output reg [3:0] counter
);
    always@(posedge clk)begin
        if(reset)
            counter <=4'h2;
        else begin
            if(en)begin
                if(counter == 4'h9)
                	counter <= 4'h0;
                else if((h1==4'h1)&(counter == 4'h2)) //当12点使能来时,将时个位置1
                    counter <= 4'h1;
                else
                	counter <= counter + 1'b1;
            end
            else
            	counter <= counter;
        end
    end
endmodule 

//时十位
module h1_bcdcounter(  
	input            clk     ,
    input            reset   ,
    input            en      ,
    output reg [3:0] counter
);
    always@(posedge clk)begin
        if(reset)
            counter <=4'h1;
        else begin
            if(en)begin
                if(counter == 4'h1)
                	counter <= 4'h0;
                else
                	counter <= counter + 1'b1;
            end
            else
            	counter <= counter;
        end
    end
endmodule 

仿真图如下:
simulation

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Iceeeewoo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值