偶数分频,M = 时钟输入频率/时钟输出频率,N = M/2 如输入时钟为50M,输出时钟为5M,则M=10,N=5。
因此只需将counter以clk为时钟驱动计数,当counter = (N-1)时,clk_out翻转进行了。
module fen( input clk, input rst_n, output reg clk_out ); parameter N = 2; //N=2,此时M=4,即系统四分频 reg [31:0] counter;
always @(posedge clk or posedge rst_n) begin if (rst_n) counter <= 0; else if (counter == N-1) counter <= 0; else counter <= counter + 1; end
always @(posedge clk or posedge rst_n) begin if (rst_n) clk_out <= 0; else if (counter == N-1) clk_out <= !clk_out; end
endmodule
