代码 如下 在高电平计数到86ms以上时就会出现很大的误差
仿真也没问题 是什么原因呢?
module dutycycle(
clk ,
rst_n ,
clk_400M ,
signal ,
en_d ,
flag_d ,
dout
);
//输入信号定义
input clk ;
input rst_n ;
input clk_400M ;
input signal ;
input en_d ;
//输出信号定义
output[31:0] dout ;
output flag_d ;
//输出信号reg定义
reg [31:0] dout ;
reg flag_d ;
reg [31:0]
time_h ;
reg [31:0] time_a ;
//中间信号定义
reg signal_ff0;
reg signal_ff1;
reg signal_ff2;
reg [31:0] time_h_buf;
reg [31:0] time_a_buf;
reg [2:0] state ;
wire jump_p1 ;
wire jump_p2 ;
wire jump_n ;
assign jump_p1 = signal_ff0 && ~signal_ff1; //第一个上升沿
assign jump_p2 = signal_ff1 && ~signal_ff2; //第二个上升沿
assign jump_n = ~signal_ff0 && signal_ff1; //第一个下降沿
always@(posedge clk_400M or negedge rst_n)begin
if(rst_n==1'b0)begin
signal_ff0 <= 0;
signal_ff1 <= 0;
signal_ff2 <= 0;
end
else begin
signal_ff0 <= signal;
signal_ff1 <= signal_ff0;
signal_ff2 <= signal_ff1;
end
end
//占空比测量
always @(posedge clk_400M or negedge rst_n)begin
if(rst_n==1'b0)begin
state <= 0;
end
else if(jump_p1&&state==0&&en_d)begin
state <= 1;
end
else if(jump_n&&state==1)begin
state <= 2;
end
else if(jump_p2&&state==2)begin
state <= 3;
end
else if(state==3)begin
state <= 4;
end
else if (state==4)begin
state <= 0;
end
end
always @(posedge clk_400M or negedge rst_n)begin
if(rst_n==1'b0)begin
dout <= 32'd0;
flag_d <= 0;
time_h <= 32'd0;
time_a <= 32'd0;
time_h_buf <= 32'd0;
time_a_buf <= 32'd0;
end
else begin
case(state)
3'd1:begin
time_h_buf <= time_h_buf + 1'b1;
time_a_buf <= time_a_buf + 1'b1;
end
3'd2:begin
time_h_buf <= time_h_buf;
time_a_buf <= time_a_buf + 1'b1;
end
3'd3:begin
time_h <= time_h_buf;
time_a <= time_a_buf;
end
3'd4:begin
dout <= 1+1000* time_h/time_a;
flag_d <= 1'd1;
end
default:begin
dout <= 32'd0;
flag_d <= 0;
//time_h <= 32'd0;
//time_a <= 32'd0;
time_h_buf <= 32'd0;
time_a_buf <= 32'd0;
end
endcase
end
end
endmodule
一周热门 更多>