如题,设置任意参数可变的整数分频,分频系数由DSP发送给CPLD,在调试的过程中发现由2分频调到3分频的时候,占空比不为50%,但是从新启动后的3分频的占空比为50%,猜测应该是计数器cnt1与cnt2在改变分频系数之前有值。不是从零开始的。
求大神指点下,怎么改进????
module clk_divx(
rst_n,clkin,N_divn,
clkout
);
input rst_n;
input clkin;
input[7:0] N_divn;
output clkout;
reg clkout_1;
reg clkout_2;
wire clkout1;
reg clkout2;
reg [7:0] N_divn1;
reg [7:0] N_divn1_r1;
reg [7:0] N_divn1_r2;
reg [7:0] N_divn2;
reg [7:0] cnt1;
reg [7:0] cnt2;
reg [7:0] cnt3;
reg clear;
always @ (negedge rst_n or posedge clkin)
if(!rst_n)
begin
N_divn1=0;
N_divn2=0;
end
else
begin
case(N_divn%2)
1:
N_divn1 <= N_divn;
0:
N_divn2 <= N_divn;
endcase
end
always @ (negedge rst_n or posedge clkin)
if(!rst_n)
begin
clear =0;
N_divn1_r1=0;
N_divn1_r2=0;
end
else
begin
N_divn1_r2 <= N_divn1_r1;
N_divn1_r1 <= N_divn1;
if(N_divn1_r2 != N_divn1_r1)
begin
clear=1;
end
else
begin
clear=0;
end
end
/************************************************************************
//输入频率:37.5M
//奇数分频
*************************************************************************/
always @ (negedge rst_n or posedge clkin or posedge clear)
if(!rst_n)
begin
cnt1=0;
clkout_1=0;
// N_divn1_r1 <=0;
end
else if(clear)
begin
cnt1=0;
end
else
begin
// N_divn1_r1 ={N_divn1_r1[7:0],N_divn1};
// if(N_divn1_r1[15:8]!= N_divn1_r1[7:0])
// begin
// cnt1=0;
// end
cnt1=cnt1+1'b1;
if(cnt1==(N_divn1-1)/2)
begin
clkout_1=~clkout_1;
end
else if (cnt1==N_divn1)
begin
clkout_1 = ~clkout_1;
cnt1=0;
end
end
always @ (negedge rst_n or negedge clkin or posedge clear)
if(!rst_n)
begin
cnt2=0;
clkout_2=0;
// N_divn1_r2 <=0;
end
else if(clear)
begin
cnt2=0;
end
else
begin
// N_divn1_r2 ={N_divn1_r2[7:0],N_divn1};
// if(N_divn1_r2[15:8]!= N_divn1_r2[7:0])
// begin
// cnt2=0;
// end
cnt2=cnt2+1'b1;
if(cnt2==(N_divn1-1)/2)
begin
clkout_2=~clkout_2;
end
else if(cnt2==N_divn1)
begin
cnt2=0;
clkout_2=~clkout_2;
end
end
assign clkout1 = clkout_1 || clkout_2;
/************************************************************************
//输入频率:37.5M
//偶数分频
*************************************************************************/
always @ (negedge rst_n or negedge clkin)
if(!rst_n)
begin
cnt3=0;
clkout2=0;
end
else
begin
cnt3=cnt3+1'b1;
if(cnt3==N_divn2/2)
begin
cnt3=0;
clkout2=~clkout2;
end
end
assign clkout =(N_divn<2)?clkin:((N_divn%2)?clkout1:clkout2);
endmodule
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>