Verilog: push button starts sequence

Discussion in 'Embedded Systems and Microcontrollers' started by n02293588, May 1, 2013.

  1. n02293588

    Thread Starter New Member

    Nov 17, 2012
    2
    1
    Hey guys, I was wondering if I could get some help on something I'm working on. I'm very new to Verilog, so I'm not really the best / most efficient at it. I'm trying to get a counter to start counting down when a push button is pressed. I currently have it so that when I press and hold the button, it counts, but when I release it, the counter the stops counting down. Here's the code that I have so far. (I'm using a Nexys2 Spartan3E board)

    Code ( (Unknown Language)):
    1. `timescale 1ns / 1ps
    2.  
    3. module countonpb(
    4.     input clock, pb,
    5.     output reg [3:0] msbs,
    6.     output reg [3:0] lsbs,
    7.     output reg [6:0] sevseg,
    8.     output reg [3:0] an,
    9.      output reg [1:0] clk
    10.     );
    11.     reg [3:0] i;
    12.     reg [3:0] j;
    13.     integer count_a = 0;
    14.     integer count_50M = 0;
    15.     reg pb_press;
    16.  
    17.     initial begin
    18.         an[0] <= 0;
    19.         an[1] <= 1;
    20.         an[2] <= 1;
    21.         an[3] <= 1;
    22.         i <= 9;
    23.         j <= 9;
    24.     end        
    25.    
    26.     always @ (posedge clk[0])
    27.     begin
    28.         an[0] <= ~an[0];
    29.         an[1] <= ~an[1];
    30.         an[2] <= 1;
    31.         an[3] <= 1;
    32.         msbs <= i;
    33.         lsbs <= j;
    34.     end
    35.    
    36.     always @ (posedge clk[1])
    37.     begin
    38.         pb_press <= pb;
    39.         if (pb_press == 1)
    40.         begin
    41.             if (i == 0)
    42.             begin
    43.                 i = 0;
    44.                 j = j - 1;
    45.             end
    46.             else
    47.             begin
    48.                 if (j == 0)
    49.                 begin
    50.                     i = i - 1;
    51.                     j = 9;
    52.                 end
    53.                 else
    54.                     j = j - 1;
    55.             end
    56.         end
    57.     end
    58.  
    59.  
    60.     always @ (posedge clk[0])
    61.     begin
    62.     if (an[1])
    63.     begin
    64.         case(msbs)
    65.             0: sevseg = 7'b0000001;
    66.             1: sevseg = 7'b1001111;
    67.             2: sevseg = 7'b0010010;
    68.             3: sevseg = 7'b0000110;
    69.             4: sevseg = 7'b1001100;
    70.             5: sevseg = 7'b0100100;
    71.             6: sevseg = 7'b0100000;
    72.             7: sevseg = 7'b0001101;
    73.             8: sevseg = 7'b0000000;
    74.             9: sevseg = 7'b0000100;
    75.             default: sevseg = 7'b0000001;
    76.         endcase
    77.     end
    78.     if (an[0])
    79.     begin
    80.         case(lsbs)
    81.             0: sevseg = 7'b0000001;
    82.             1: sevseg = 7'b1001111;
    83.             2: sevseg = 7'b0010010;
    84.             3: sevseg = 7'b0000110;
    85.             4: sevseg = 7'b1001100;
    86.             5: sevseg = 7'b0100100;
    87.             6: sevseg = 7'b0100000;
    88.             7: sevseg = 7'b0001101;
    89.             8: sevseg = 7'b0000000;
    90.             9: sevseg = 7'b0000100;
    91.             default: sevseg = 7'b0000001;
    92.         endcase
    93.     end
    94.     end
    95.  
    96.     always @ (posedge clock) //slower clock
    97.         if (count_50M < 25000000) count_50M = count_50M + 1;
    98.         else
    99.         begin
    100.                 clk[1] = ~clk[1];
    101.                 count_50M = 0;
    102.         end
    103.        
    104.     always @ (posedge clock) //slow clock
    105.         if (count_a < 25000) count_a = count_a + 1;
    106.         else
    107.         begin
    108.             clk[0] = ~clk[0];
    109.             count_a = 0;
    110.         end
    111.  
    112. endmodule
    I was thinking of possibly making a different module for the counting, and just activate that when the button is pressed once.

    When I do get the program counting down on button press, I also will have a statement to set a variable to 1 when a certain input is entered on the switches, so I was wondering if I can lock out the switch inputs while the counter is not counting down.

    Thanks in advance.
     
Loading...