I want to count the number of pulses in a certain period of time, for eg. 1 Sec. If it s 1 pulse , I want to do certain actions and if it is 2 pulses then do another action. If is more than 2 do nothing.
Can you help with System Verilog code for this ?
reg [31:0] pulse_counter;
reg [31:0] pulse_time;
always @(posedge clk) begin
if (rst_n == 0) begin
pulse_counter <= 0;
pulse_time <= 0;
end else begin
if (pulse_time < 1000000000) begin // 1 sec
pulse_time <= pulse_time + 1;
if (pulse == 1) pulse_counter <= pulse_counter + 1;
end else begin
pulse_time <= 0;
if (pulse_counter == 1) begin
// do certain actions if it's 1 pulse
end else if (pulse_counter == 2) begin
// do another action if it's 2 pulses
end else if (pulse_counter > 2) begin
// do nothing if it's more than 2 pulses
end
pulse_counter <= 0;
end
end
end