题解 | #时钟分频(偶数)#
时钟分频(偶数)
https://www.nowcoder.com/practice/49a7277c203a4ddd956fa385e687a72e
本题采用计数器可以解决,不要使用4分频依靠二分频和八分频依靠四分频的事,最好是全都挂靠到全局时钟上 `timescale 1ns/1ns module even_div ( input wire rst , input wire clk_in, output wire clk_out2, output wire clk_out4, output wire clk_out8 ); //*************code***********// reg clk_out2_reg; reg clk_out4_reg; reg clk_out8_reg; reg clk4_cnt; reg [1:0] clk8_cnt; always@(posedge clk_in or negedge rst) begin if(!rst) clk4_cnt <= 1'b0; else clk4_cnt <= clk4_cnt + 1'b1; end always@(posedge clk_in or negedge rst) begin if(!rst) clk8_cnt <= 2'b0; else clk8_cnt <= clk8_cnt + 1'b1; end always@(posedge clk_in or negedge rst) begin if(!rst) clk_out2_reg <= 1'b0; else clk_out2_reg <= ~clk_out2_reg; end always@(posedge clk_in or negedge rst) begin if(!rst) clk_out4_reg <= 1'b0; else if(clk4_cnt == 1'b0) //观察信号波形可知,其在最初的时候就进行了输出拉高,所以应该是在0时就变化拉高,下面的也是这样,这么操作不影响分频信号,只是输出的时间位置不同而已,周期是一致的 clk_out4_reg <= ~clk_out4_reg; end always@(posedge clk_in or negedge rst) begin if(!rst) clk_out8_reg <= 1'b0; else if(clk8_cnt == 2'd0) clk_out8_reg <= ~clk_out8_reg; end assign clk_out2 = clk_out2_reg; assign clk_out4 = clk_out4_reg; assign clk_out8 = clk_out8_reg; //*************code***********// endmodule