luyaker 发表于 2012-8-20 21:06:07

FPGA时钟问题请教

always@(posedge clk),这个clk可以是始终以外的信号吗?
我用这种clk测频率,发现结果不对。具体程序如下
module cnt(clk,gate,cntout);

input clk;
input gate;
output cntout;

reg cnt;
reg cntout;
reg gatebuf;

always@(posedge clk)
begin
        gatebuf<=gate;
end

always@(posedge clk)
begin
        if((gate==1'b1)&&(gatebuf==1'b0))
                begin
                        cnt<=32'd1;
                end
        else if((gate==1'b0)&&(gatebuf==1'b1))
                begin
                        cntout<=cnt;
                end
       
        else if(gatebuf==1'b1)
        begin
                cnt<=cnt+32'd1;
        end
end


endmodule

其中clk是被测信号,gate是门控信号,cnt_out是输出。
多谢!

bostong 发表于 2012-8-21 11:16:23

用被检测信号来作为时钟不是很好;
按照你的设想与方法:我认为写法可以是:
always@(posedge clk)
      begin
      gatebuf <= gate;
      if(1'b1==gatebuf)
            begin
         cnt_out<= cnt_out + 1;
            if( cnt_out==32'hffffffff)
            begin
             cnt_out<=32'hffffffff
            end
      else
             cnt_out <= 0;
      end
页: [1]
查看完整版本: FPGA时钟问题请教