FPGA要用到奇数分频,一时没想起来好办法。网上看到的一段比较好的代码,保存着用
module any_odd_div (clkdiv,clk);
output clkdiv; //输出分频信号
input clk; //时钟信号
reg[2:0]cnt1,cnt2; //计数器1,计数器2
reg clk_temp1,clk_temp2;
parameter n = 7; //7分频
always @(posedge clk)
begin
if(cnt1 == n-1)
begin
cnt1 <=3'b000;
end
else
begin
cnt1 <= cnt1 +1'b1;
end
if(cnt1 ==3'b000)
begin
clk_temp1 =1'b1;
end
if(cnt1 ==(n-1)/2)
begin
clk_temp1 =0;
end
end
always @(negedge clk)
begin
if(cnt2 == n-1)
begin
cnt2 <=3'b000;
end
else
begin
cnt2 <=cnt2 +1'b1;
end
if(cnt2 ==3'b000)
begin
clk_temp2 =1;
end
if(cnt2 ==(n-1)/2)
begin
clk_temp2 =0;
end
end
assign clkdiv = clk_temp1 | clk_temp2;
endmodule