首页 > 其他 > 详细

使用BCD计数器设计时钟

时间:2020-08-12 20:00:16      阅读:76      评论:0      收藏:0      [点我收藏+]
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Engineer: connor jiao
// Create Date: 21:42 2020/8/5
// Design Name: 
// Module Name: 
// Function   : 
//用计数器设计一个带am/pm的12小时时钟。该计数器通过一个CLK进行计时,用ena使能信号来驱动时钟的递增。
//reset信号将时钟复位为12:00 AM。 信号pm为0代表AM,为1代表PM。
//hh、mm和ss由两个BCD计数器构成hours(01~12), minutes(00~59) , second(00~59)。
//Reset信号比enable信号有更高的优先级,即使没有enable信号也可以进行复位操作。 
// Revision 0.01 - File Created
// Additional Comments:
//////////////////////////////////////////////////////////////////////////////////
module top_clk(
                input clk,
                input reset,
                input en,
                output pm,
                output [7:0]hh,//因为用的BCD,最大到9,需要两组
                output [7:0]mm,
                output [7:0]ss
                );
    
    
//============================================================================
//*******************Define parameter and Internal Signals********************
//============================================================================
    reg [7:0]cnt_out1;
    reg [7:0]cnt_out2;
    reg [7:0]cnt_out3;
    
    reg p;
//============================================================================
//********************************Main Code***********************************
//============================================================================
    always@(posedge clk )
    begin
        if(reset)
            cnt_out1<=d0;
        else begin
            if(en)
                if(cnt_out1==8h59)//注意这里是16进制的5,9哦,这道题的灵魂所在
                    cnt_out1<=d0;
                else begin 
                    if(cnt_out1[3:0]==d9)
                    begin
                        cnt_out1[3:0]<=d0;
                        cnt_out1[7:4]<=cnt_out1+1b1;
                    end 
                    else
                        cnt_out1[3:0]<=cnt_out1[3:0]+1b1;
                end
            else
                cnt_out1<=d0;
        end
    end
    always@(posedge clk )
    begin
        if(reset)
            cnt_out2<=d0;
        else begin
            if(en&&cnt_out1==8h59)
                if(cnt_out2==8h59)//注意这里是16进制的5,9哦
                    cnt_out2<=d0;
                else begin 
                    if(cnt_out2[3:0]==d9)
                    begin
                        cnt_out2[3:0]<=d0;
                        cnt_out2[7:4]<=cnt_out2+1b1;
                    end 
                    else
                        cnt_out2[3:0]<=cnt_out2[3:0]+1b1;
                end
            else
                cnt_out2<=d0;
        end
    end
    always@(posedge clk )
    begin
        if(reset)
            cnt_out3<=d0;
        else begin
            if(en&&cnt_out2==8h59&&cnt_out1==8h59)
                if(cnt_out3==8h12)//注意这里是16进制的1,2哦
                    cnt_out3<=d0;
                else begin 
                    if(cnt_out3[3:0]==d9)
                    begin
                        cnt_out3[3:0]<=d0;
                        cnt_out3[7:4]<=cnt_out3+1b1;
                    end 
                    else
                        cnt_out3[3:0]<=cnt_out3[3:0]+1b1;
                end
            else
                cnt_out3<=d0;
        end
    end
    
    always@(posedge clk)
    begin
        if(reset)
            p<=0;
        else if(en&&cnt_out3==8h11&&cnt_out2==8h59&&cnt_out1==8h59)
            p<=~p;
        else
            p<=p;
    end
    assign pm=p;
    assign ss=cnt_out1;
    assign mm=cnt_out2;
    assign hh=cnt_out3;
endmodule

 

使用BCD计数器设计时钟

原文:https://www.cnblogs.com/ajiaoa/p/13492503.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!