三分频代码完全可以不用双触发,双触发不要用。引用别人的代码,这个代码经过仿真测试过。
module div_3(clk,rst,count1,count2,clk_even);
input clk;
input rst;
output [3:0] count1;
output [3:0] count2;
output clk_even;
reg [3:0] count1,count2;
reg clkA,clkB;
reg clktet;
wire clk_even,clk_re;
parameter N = 3;
assign clk_re = ~clk;
assign clk_even = clkA | clkB;
//count计数到2后归零
always @(posedge clk or negedge rst)
begin
if(!rst)
begin
count1 <= 0;
end
else if(count1 < (N-1))
begin
count1 <= count1 + 1'b1;
end
else
begin
count1 <= 0;
end
end
wire [3:0] NA;
assign NA = (N-1) >> 1;//NA=2
always @(posedge clk or negedge rst)
begin
if(!rst)
begin
clkA <= 1'b0;
end
else if(count1 < (N-1))
begin
if(count1 == NA)
begin
clkA <= ~clkA;
end
end
else
begin
clkA <= ~clkA;
end
end
always @(posedge clk_re or negedge rst) begin
if(!rst)
begin
count2 <= 0;
end
else if(count2 < (N-1))
begin
count2 <= count1 + 1'b1;
end
else begin
count2 <= 0;
end
end
wire [3:0] NB;
assign NB = (N-1) >> 1;
always @(posedge clk_re or negedge rst) begin
if(!rst) begin
clkB <= 0;
end
else if(count2 < (N-1)) begin
if(count2 == NB) begin
clkB <= ~clkB;
end
end
else begin
clkB <= ~clkB;
end
end
reg [3:0] cnt10;
always @(posedge clk or negedge rst) begin
if(!rst) begin
cnt10 <= 0;
end
else if(cnt10==4) begin
cnt10 <= 0;
end
else begin
cnt10 <= cnt10 + 1'b1;
end
end
always @(posedge clk or negedge rst) begin
if(!rst) begin
clktet <= 0;
end
else if(cnt10 == 4) begin
clktet <= ~clktet;
end
end
endmodule |