I'm currently working on a simple up counter triggered by the negative edge of a push button. If the button is held while pressed down, the counter gets stuck in the always block and increments by more than 1. Is there any way to only execute the always block only once per negative edge?
always @(negedge button) begin
count <= count + 1;
end
-
1\$\begingroup\$ the button is probably bouncing \$\endgroup\$jsotola– jsotola2023年11月30日 20:20:37 +00:00Commented Nov 30, 2023 at 20:20
-
\$\begingroup\$ The always block will only execute exactly one per falling edge. It cannot get stuck as you put it. If it's counting more than once per button press it means there is more than one edge - which is caused by the switch bouncing. This question should help you. \$\endgroup\$Tom Carpenter– Tom Carpenter2023年11月30日 21:06:29 +00:00Commented Nov 30, 2023 at 21:06
2 Answers 2
A better design is to use a double-rank flip flop to synchronize the push-button to the system clock.
The always block is then clocked from the system clock and includes a simple state-machine to detect the push-button falling edge that is then used to synchronously increment your counter (which is also operated from the system clock).
You need a clock to count or some kind of delay. See this simmilar question: Verilog always block w/o posedge or negedge