首页 > 解决方案 > 一个简单的时钟分频器模块

问题描述

我被要求为不同类型的输入设计简单的时钟分频器电路。

我有一个enabler [1:0]input 和一个 input clock,以及一个名为clk_enable.

如果enabler=01那么我的输入时钟应该在 2 个时钟信号中启用一次。如果enabler=10那么我的输入应该除以 4 等。

我设法使用 case 关键字将我的输入时钟划分为不同的情况,但是enabler=00我的输入时钟应该等于我clk_enable无法做到的输出。

这是我尝试过的。我正在寻求帮助enabler=00

module project(input [1:0] enabler,
                            input clock,
                            output reg clk_enable);

reg  [3:0] count,c;
initial begin
count=4'b0000;
c=4'b0000;
end

always @( posedge clock)
    case(enabler)
    2'b00:clk_enable<=clock;
    2'b01:clk_enable<=~clk_enable;
    2'b10:begin
            if (count >= 4'b0100-1)      
                count<=4'b0000;
            else begin 
            count<=count + 4'b0001;
            clk_enable<=(count<(4'b0100 / 2));
            end
            end
    2'b11: begin
            if (count >= 4'b1000-1)      
                count<=4'b0000;
            else begin 
            count<=count + 4'b0001;
            clk_enable<=(count<(4'b1000 / 2));
            end
            end
    endcase                                             
endmodule

标签: verilog

解决方案


这将生成与 div_ratio 输入相匹配的 posedge 速率的门控脉冲时钟。div_ratio output 0 div1 clock (clk as it is) 1 div2 (pulse every 2 pulse of clk) 2 div3 3 div4

当不需要在分频时钟的 negedge 进行采样时,这通常更可取如果您需要 50% 占空比,我可以给您另一个片段

module clk_div_gated (
    input [1:0] div_ratio,
    input       clk,
    input       rst_n,    // async reset - a must for clock divider
    output      clk_div
);

reg [1:0] cnt;
reg clk_en;

always @(posedge clk or negedge rst_n)
    if (~rst_n)
        cnt    <= 2'h0;
    else
        cnt    <= (cnt == div_ratio)? 2'h0 : cnt + 1'b1;

// clk_en toggled at negedge to prevent glitches on output clock
// This is ok for FPGA, synthesizeable ASIC design must use latch + AND method
always @(negedge clk)
    clk_en <= (cnt == div_ratio);

assign clk_div <= clk & clk_en;

endmodule

推荐阅读