思路是用一高速时钟信号驱动对此信号进行采样,以获得上升沿和下降沿。根据采样定理,采样时钟频率应大于被计数信号最高频率的2倍以上,否则会发生频域混叠造成计数误差。
具体这样写:
reg [2:0]signal_reg;
wire signal_edge;
reg [15:0]signal_count;
always @(posedge clk_sample) signal_reg = {signal_reg[1:0],signal_in};
assign signal_edge = signal_reg[1]&(~signal_reg[2]);
always @(posedge signal_edge) signal_count = signal_count + 1'b1;