increase or decrease a reg

Status
Not open for further replies.

orion64

Newbie level 2
Joined
Jun 13, 2013
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
16
I am trying to make register to increase or decrease in value when push buttons are pressed. It works in simulation but in the FPGA board it doesn't. Does any body know why? any suggestion?

Code Verilog - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module key_speed
    #(parameter speedDefault = 2, speedMax=4, speedMin=0)
    (reset, keyUp, keyDown, speed, reset_reg_eight);
input reset, keyUp, keyDown;
output reset_reg_eight;
output reg [2:0] speed;
 
 
always @(negedge keyUp or negedge keyDown or negedge reset)
begin
if (~reset)
    speed = speedDefault;
    else 
    begin
        if (keyDown==0)
        begin
            if (speed!=speedMin)
                speed--;
            else speed = speed;
        end
        else if (keyUp==0)
        begin
            if (speed!=speedMax)
                speed++;
            else speed = speed;
        end
    end
 
end
 
assign reset_reg_eight = (keyUp&keyDown);
    
endmodule

 

Couple of things... Right now you are using the keyUp and keyDown inputs as if they were clock inputs. Don't do that. Treat them as data inputs instead. Furthermore, if those are mechanical switches you'll have to add some debouncing.

Here is some example code for a button debounce: https://www.fpga4fun.com/Debouncer.html

Also, note how in that example there is no negedge keyUp and such.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…