原理
将输入时钟进行分频,如输入50M时钟,分频为50/5M时钟或50/4时钟。
利用计数器,计满预定时钟则翻转输出,达到分频效果。
实现
1)偶数分频
这里给出两种方法
计数分频系数
module divider #(parameter DIV = 4) (
input clk ,
input rst_n ,
output dout
);
reg [7:0] cnt;//分频计数器
wire add_cnt;
wire end_cnt;
//计数分频系数
always@(posedge clk or negedge rst_n)begin
if(!rst_n)begin
cnt <= 0;
end
else if(add_cnt)begin
if(end_cnt)begin
cnt <= 0;
end
else begin
cnt <= cnt + 1;
end
end
end
assign add_cnt = 1'b1;
assign end_cnt = add_cnt && (cnt == DIV - 1);
//当cnt计数小于分频系数的一半时,输出1,否则输出0
assign dout = cnt < (DIV >> 1) ? 1'b0 : 1'b1;
endmodule
第一种资源消耗情况