我被要求为不同类型的输入设计简单的时钟分频器电路。
我有一个enabler [1:0]
输入和一个输入clock
,以及一个名为clk_enable
的输出。
如果enabler=01
那么我的输入时钟应该在2个时钟信号中启用一次。如果enabler=10
那么我的输入应该除以4等等。
我设法使用 using 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
最佳答案
这将生成门控脉冲时钟,其 posege 速率与 div_ratio 输入匹配。 div_ratio 输出 0 div1 时钟(clk 本身) 1 div2(每 2 个 clk 脉冲产生一个脉冲) 2 分区3 3div4
当不需要在分频时钟的下降沿采样时,这通常是更好的选择 如果您需要 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
关于verilog - 一个简单的时钟分频器模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61838476/