0
\$\begingroup\$

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
asked Nov 30, 2023 at 20:04
\$\endgroup\$
2
  • 1
    \$\begingroup\$ the button is probably bouncing \$\endgroup\$ Commented 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\$ Commented Nov 30, 2023 at 21:06

2 Answers 2

0
\$\begingroup\$

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).

answered Nov 30, 2023 at 22:20
\$\endgroup\$
0
\$\begingroup\$

You need a clock to count or some kind of delay. See this simmilar question: Verilog always block w/o posedge or negedge

answered Nov 30, 2023 at 20:19
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.