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.

Increment 4 bit counter by 1

Status
Not open for further replies.

piratecoug

Newbie level 3
Joined
Sep 13, 2016
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
25
Hi,

I'm making a stopwatch on a Nexys4 and I'm having trouble with one part. I can start, stop, and reset the stopwatch, but I cannot figure out how to get it to increment by 1 bit. So if the stopwatch is stopped at 1:021, when I hit the increment button the output would be 1:022. Currently, when I hit the increment button, it will increment a random amount, and I assume this is because it is being pushed over multiple clock edges. How do I just do 1 bit increment no matter how long I hold the button for?

Here is a snippet of my code for my counter:

Cen is coming from the state machine, where Cen = 2'b01 means start counting. Inc is also coming from the state machine.

Code:
always @ (posedge (clk), posedge(rst))//, posedge(inc))
	begin 
	
	if (rst == 1'b1)begin
			Dig0 <= 4'b0000;
			Dig1 <= 4'b0000;
			Dig2 <= 4'b0000;
			Dig3 <= 4'b0000;
		end	
	
	//increment if inc
	else if(inc == 1'b1)// && Dig3 < 4'b1001)begin
    begin
        if (enable == 1'b1)
        begin
            Dig0 <= Dig0 + 4'b0001;   
            enable = 1'b0;
        end      
    end
	
	
	//only continue if Cen is 01 & not inc
	else if(Cen == 2'b01)begin	

		//add 1 to first digit up till 9
		Dig0 <= Dig0 + 1'b1;
		
		//reset if == 10	
			if(Dig0 > 4'b1001)begin
				Dig0 <= 4'b0000;
				
				//add 1 to second digit (when first resets) up till 9
				Dig1 <= Dig1 + 1'b1;
				end
				//reset if == 10
				if(Dig1 == 4'b1010)begin
					Dig1 <= 4'b0000;
					
					//add 1 to third digit (when second reset) up till 9
					Dig2 <= Dig2 + 1'b1;
					end
					//reset if == 10
					if(Dig2 == 4'b1010)begin
						Dig2 <= 4'b0000;
						
						//add 1 to fourth digit (when third reset) up till 9
						Dig3 <= Dig3 + 1'b1;
						end
						//reset if == 10
						if(Dig3 > 4'b1001)begin
							Dig3 <= 4'b0000;
						
						end			
		end
 

Example of case in point is simple.

>Check button for press
>Increment your variable value
>Wait until button is pressed.

Exa. in C

Code:
if(button == 1) 
{
   value++;
   while(button != 0);
}
 

But it could go to different states in between button presses. So I have a button to start it, stop it, and increment it. If I hit increment and then start, it needs to increment and then start the stopwatch again.
 

The suggested code will only work if no contact bounce occurs, but worst case you'll observe bounce for both events, key press and key release.

Secondly, it's a HDL (Verilog) problem, programming doesn't work like a microcontroller.

As for the original code, I don't understand how an "increment button" is related to a stop watch design. Increment of a stop watch is done by a timer, not a button.
 

As for the original code, I don't understand how an "increment button" is related to a stop watch design. Increment of a stop watch is done by a timer, not a button.

It's part of what I need to do. It's just incrementing the counter by 1 bit when a button is pressed.
 

the normal method for dealing with pushbutton IO in FPGAs would be to write a debouncing circuit.

This works by sampling the button with a clock. Typically two registers in a row to form a "synchronizer". This dramatically reduces the chance of "metastability" -- that the button will be changing at the exact instant of the clock edge. The actual results of this can be odd, the output of the register will take time to become stable. Placing two or more registers in a row will make it more unlikely that this condition will occur on the 2nd stage.

The second part is the debouncer. This can be done in many different ways. The one that will help you would be the counter method. The counter drops to 0 if the input is 0. If the input is 1, the counter increments unless the current value is N. When the counter reaches N-1, there is an output pulse.

When you have a mechanical switch, there is mechanical bouncing of the contacts. This can occur for a short time for humans, but a long time for a circuit. The result is that button input, used directly, could cause multiple "pressed" events.

The value for N is based on the switch and the clock rate. for a stop watch, a few milliseconds will be fine.

There are other methods to do this, but the counter method works well for longer debouncing windows and allows for this "trigger once" output in an easy manner.
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top