module even_divider( input [3:0] divider, input clk, input rst, output reg out_clk ); reg [2:0] count=3'd0; always @ (posedge clk or negedge rst) begin if (!rst) begin count <= 3'd0; out_clk <= 1'd0; end end // N/2-1 always @(posedge clk) begin if (count==(divider/2)-1) begin count <= 3'd0; out_clk <= !out_clk; end else begin count <= count+1; end end endmodule
奇数分频
out_clk=p_clk | n_clk p_clk: switch in posedge clk when count==N-1 or count==(N-1)/2 n_clk: same, but check in negedge
assign out_clk=p_clk|n_clk; always @(posedge clk or negedge rst) begin if (!rst) begin count <= 4'd0; end else if (count == divider-1) begin count <= 4'd0; end else begin count <= count+1'd1; end end always @(posedge clk or negedge rst) begin if (!rst) begin p_clk <= 1'd0; end elseif (count==divider-1'd1 || count==(divider-1'd1)/2) begin p_clk <= !p_clk; end else begin p_clk <= p_clk; end end
always @(negedge clk or negedge rst) begin if (!rst) begin n_clk <= 1'd0; end else if (count==divider-1'd1 || count==(divider-1'd1)/2) begin n_clk <= !n_clk; end else begin n_clk <= n_clk; end end endmodule