题解 | #占空比50%的奇数分频#
占空比50%的奇数分频
http://www.nowcoder.com/practice/ccfba5e5785f4b3f9d7ac19ab13d6b31
````timescale 1ns/1ns
//关键是(N-1)/2和(N-1)翻转,上升沿和下降沿分别计数
module odo_div_or
(
input wire rst ,
input wire clk_in,
output wire clk_out7
);
//*************code***********//
reg[2:0]cnt_p;
reg[2:0]cnt_n;
reg clk_p;
reg clk_n;
always @(posedge clk_in or negedge rst)begin
if(~rst)
cnt_p <= 0;
else if(cnt_p == 6)
cnt_p <= 0;
else
cnt_p <= cnt_p + 1'b1;
end
always @(posedge clk_in or negedge rst)begin
if(~rst)
clk_p <= 0;
else if(cnt_p == 3 || cnt_p ==6)
clk_p <= ~clk_p;
end
always @(negedge clk_in or negedge rst)begin
if(~rst)
cnt_n <= 0;
else if(cnt_n == 6)
cnt_n <= 0;
else
cnt_n <= cnt_n + 1'b1;
end
always @(negedge clk_in or negedge rst)begin
if(~rst)
clk_n <= 0;
else if(cnt_n == 3||cnt_n == 6)
clk_n <= ~clk_n;
end
assign clk_out7 = clk_p|clk_n;
//*************code***********//
endmodule