Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Verilog Debouncer for a Counter

Status
Not open for further replies.

eddie_b

Newbie level 2
Newbie level 2
Joined
Mar 16, 2013
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,295
Hi everyone, I'm trying to build a debouncer for an 8-bit counter that registers how many times the button is pushed that differentiates between a full push and release and the button being held down. In other words, the output should be the "clean" number of times the button was fully pushed and released. I also want to include a reset. At the moment, I'm a bit stuck and I don't know what to do. I'm also unable to test it at the moment because I have to be at home all day today. Would anyone please be able to give me some advice for the code I have? Would this code work?

Code:
module debouncer(clock, reset, bounce, y);

	input clock;
	input reset;
	input bounce;
	output reg [7:0] y;
	
	reg bounce;
	reg value;
	reg [32:0] counter;
	parameter delay = 50000;

 always @ (posedge btn or posedge rst)
	begin
	
	   if (bounce)
		begin
			if (bounce != value)
			   begin
			   	value <= bounce;
				counter <= 32'b0;
			   end

	     		else if (counter = = delay)
			   begin
				y <= value;
			   end
			else
			   begin
				counter <= counter + 1'b1;
			   end
	      end


		if (reset)
			begin
			y <= 1'b0;
		end
	end
	
endmodule

Thank you, so greatly appreciate it.
 
Last edited:

Definitely not the right way. The counter can't work at all in a level triggered (asynchronous) always block.

Think about a synchronous design based on the clock input. After synchronizing the button input in a register chain, count it#s high and low states, etc.
 

What about something like this that uses the positive edge of the clock? Would this work?

Code:
module debouncer(clock, bounce, y)

input clock, bounce;
output reg[7:0] = y;

reg[29:0] delay = 0;

assign y = (delay >= 500000)

always @ (posedge clock or posedge reset) begin
	if (bounce)
		if (delay <= 500000)
			delay <= delay + 1;
		else
		    	delay <= delay;
		else begin
			delay <= 0;
		end
	if (reset)
		y <= 1'b0;
	end
end

endmodule

It assigns the output y to the delay and waits for the delay to count up before outputting.
 

That's much better. But it's important to synchronize the bounce input to the clock, preferably in double registers.
e.g.

Code:
bounce_s1 <= bounce;
bounce_s2 <= bounce_s1;
if (bounce_s2)
Otherwise the delay counter might jump to unexpected values if the bounce input changes in the setup- and hold window of the counter FFs.

It's also mandatory to make the asynchronous reset mutual exclusive to clock action:
Code:
always @(posedge clock or posedge reset)
if (reset)
  begin
  end
else
  begin
  end
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top