Verilog计时器无法正常运行

时间:2019-05-01 15:09:33

标签: verilog

我目前正在使用de2板上的7段显示器创建一个用作秒表和计时器的程序。它具有一个复位开关,暂停开关和一个允许其递减计数的开关。我目前正在尝试通过手动输入数字并将其取消暂停来增加其用作计时器的功能,这样它将向下计数。问题是我无法弄清楚如何以可行的方式将输入开关添加到if语句中。这是添加十进制输入之前的代码:

module UpDnCount(out4, out3, out2, out1, out0, updn, run, rst, clk);

parameter counter_maxcount = 500000;
parameter second_maxcount = 100000;

input updn, run, rst, clk;

output[6:0] out4, out3, out2, out1, out0;

reg[6:0] out4reg, out3reg, out2reg, out1reg, out0reg;

reg[$clog2(counter_maxcount)-1:0] counter;
reg[$clog2(second_maxcount)-1:0] second_count;

wire[3:0] ones;
wire[7:0] tens;
wire[10:0] hundreds;
wire[14:0] thousands; 
wire[17:0] tenthousands;

assign out4 = ~out4reg;
assign out3 = ~out3reg;
assign out2 = ~out2reg;
assign out1 = ~out1reg;
assign out0 = ~out0reg;

always @ (posedge clk)
    if((~rst) || (counter == counter_maxcount))
        counter = 0;
    else
        counter = counter + 1;

always @ (posedge clk)
    if(~rst || (second_count == second_maxcount)&updn)
        second_count = 0;
    else if((second_count == 0)&~updn)
        second_count = second_maxcount - 1;
    else if((counter == counter_maxcount) & updn & run)
        second_count = second_count + 1;
    else if((counter == counter_maxcount) & ~updn & run)
        second_count = second_count - 1;
    else
        second_count = second_count;

    assign ones = second_count % 10;

    assign tens = second_count % 100 - ones;

    assign hundreds = second_count % 1000 - tens - ones;

    assign thousands = second_count % 10000 - hundreds - tens - ones;

    assign tenthousands = second_count - thousands - hundreds - tens - ones;

    always @ (ones)
        case(ones)
            0: out0reg = 63;
            1: out0reg = 6;
            2: out0reg = 91;
            3: out0reg = 79;
            4: out0reg = 102;
            5: out0reg = 109;
            6: out0reg = 125;
            7: out0reg = 7;
            8: out0reg = 127;
            9: out0reg = 103;
            default: out0reg = "X";
        endcase

        always @ (tens)
        case(tens)
            00: out1reg = 63;
            10: out1reg = 6;
            20: out1reg = 91;
            30: out1reg = 79;
            40: out1reg = 102;
            50: out1reg = 109;
            60: out1reg = 125;
            70: out1reg = 7;
            80: out1reg = 127;
            90: out1reg = 103;
            default: out1reg = "X";
        endcase

        always @ (hundreds)
        case(hundreds)
            000: out2reg = 63;
            100: out2reg = 6;
            200: out2reg = 91;
            300: out2reg = 79;
            400: out2reg = 102;
            500: out2reg = 109;
            600: out2reg = 125;
            700: out2reg = 7;
            800: out2reg = 127;
            900: out2reg = 103;
            default: out2reg = "X";
        endcase

        always @ (thousands)
        case(thousands)
            0000: out3reg = 63;
            1000: out3reg = 6;
            2000: out3reg = 91;
            3000: out3reg = 79;
            4000: out3reg = 102;
            5000: out3reg = 109;
            6000: out3reg = 125;
            7000: out3reg = 7;
            8000: out3reg = 127;
            9000: out3reg = 103;
            default: out3reg = "X";
        endcase

        always @ (tenthousands)
        case(tenthousands)
            00000: out4reg = 63;
            10000: out4reg = 6;
            20000: out4reg = 91;
            30000: out4reg = 79;
            40000: out4reg = 102;
            50000: out4reg = 109;
            60000: out4reg = 125;
            70000: out4reg = 7;
            80000: out4reg = 127;
            90000: out4reg = 103;
            default: out4reg = "X";
        endcase



    endmodule

我正在尝试通过将其添加到if语句中来对其进行修改:

always @ (posedge clk)
    if(~rst || (second_count == second_maxcount)&updn)
        second_count = 0;
    else if((second_count == 0)&~updn)
        second_count = second_maxcount - 1;
    else if(((counter == counter_maxcount) & updn & run) || ~in1)
        second_count = second_count + 1;
    else if((counter == counter_maxcount) & ~updn & run)
        second_count = second_count - 1;
    else if(~in10)
        second_count = second_count + 10;
    else if(~in100)
        second_count = second_count + 100;
    else if(~in1000)
        second_count = second_count + 1000;
    else if(~in10000)
        second_count = second_count + 10000;

这应该只将小数点后加一,但是却弄乱了我的整个代码。我认为这是因为我正在使用的运营商,但我不确定。

0 个答案:

没有答案