verilog - 一个简单的时钟分频器模块
问题描述
我被要求为不同类型的输入设计简单的时钟分频器电路。
我有一个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
解决方案
这将生成与 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
推荐阅读
- python - 我正在尝试从 power shell(vs 代码)安装 py 音频,它给了我这个错误
- javascript - 在鼠标移动或滚动时添加跟踪代码
- checkbox - Vuetable:复选框切换方法的数据项中没有可用的行数据
- docker - Ignite Node 在 Docker 容器中启动时失败
- javascript - 将表单数据转换为请求有效负载以在 Angular 中发布请求
- laravel - 通过删除方法将请求中的数组传递给 Laravel 不起作用
- python - 如何使用 excel 或 python 将数据从行和列转换为表格形式?
- arrays - Power Automate:如何将具有多项选择列的 SharePoint 列表项复制到另一个列表?
- python - 您如何计算页面上所有单词的所有唯一实例?(Python/硒)
- mongodb - mongodb驱动聚合框架