`timescale 1ns / 1ps
//
//奇数分频器,采用"错位异或法"原理
//
module clk_div3(clk_in,reset,clk_out);
input clk_in;
input reset;
output clk_out;
reg[1:0] cnt,cnt1;
reg clk_1to3p,clk_1to3n;
always @(posedge clk_in) begin
//时钟上升沿
if(!reset) begin
//复位
cnt<=0;
clk_1to3p<=0;
end
else begin
if(cnt==2'b10) begin
//模3计数
cnt<=0;
clk_1to3p<=clk_1to3p;
end
else begin
cnt<=cnt+1;
//计数
clk_1to3p<=!clk_1to3p;
end
end
end
always @(negedge clk_in) begin
//时钟下降沿
if(!reset) begin
cnt1<=0;
clk_1to3n<=0;
end
else begin
if(cnt1==2'b10) begin
cnt1<=0;
clk_1to3n<=clk_1to3n;
end
else begin
cnt1<=cnt1+1;
clk_1to3n<=!clk_1to3n;
end
end
end
assign clk_out=clk_1to3p | clk_1to3n;
endmodule
//
// 偶数分频器,从0开始计数,到N/2-1翻转输出时钟
//
module clk_div16(clk_in,reset,clk_out);
input clk_in;
input reset;
output clk_out;
reg[2:0] cnt;
reg clk_out;
always @(posedge clk_in) begin
if(!reset) begin
//置位
cnt<=0;
clk_out<=0;
end
else if(cnt==7) begin
//模N/2计数
cnt<=0;
clk_out<=~clk_out;
end
else begin
clk_out<=clk_out;
end
end
endmodule