题解 | #占空比50%的奇数分频#
占空比50%的奇数分频
https://www.nowcoder.com/practice/ccfba5e5785f4b3f9d7ac19ab13d6b31
利用clk_out7高电平计数negcnt 4为周期,clk_out7低电平计数poscnt 4为周期,为了后续循环均计数4次,分别在clk_out7高低电平时对negcnt和poscnt进行提前清零,代码如下:
`timescale 1ns/1ns
module odo_div_or
(
input wire rst ,
input wire clk_in,
output wire clk_out7
);
//*************code***********//
reg clk_out77;
reg [2:0] negcnt, poscnt;
//always #5 clk_in = ~clk_in;
always @(*) begin
if(~rst) begin
negcnt = 0;
poscnt = 0;
clk_out77 = 0;
end
end
always @(posedge clk_in or negedge rst) begin
if(~rst) begin
negcnt <= 0;
poscnt <= 0;
end
else if(clk_out7) begin
negcnt <= 0;
poscnt <= (poscnt > 3'd3)? 0 : poscnt + 1;
end
end
always @(negedge clk_in or negedge rst) begin
if(~rst) begin
negcnt <= 0;
poscnt <= 0;
end
if(~clk_out7) begin
poscnt <= 0;
negcnt <= (negcnt > 3'd3)? 0 : negcnt + 1;
end
end
always @(*) begin clk_out77 = (((poscnt == 3'd4) & clk_out7) | ((negcnt == 3'd4) & ~clk_out7))? ~clk_out7 : clk_out7;
end
assign clk_out7 = clk_out77;
//*************code***********//
endmodule
另外,牛客上提交由于无法使用initial对寄存器初始化,导致所有寄存器值均出现不确定,而被判定为错误,望大家指点指点!
目前只能用always @(*)来对寄存器附上初始值