在verilog中组合led,开关和按钮

时间:2016-06-16 00:45:22

标签: verilog

我在verilog中创建一个basys2板,用于七段显示以二进制显示。我正在使用开关0-3并想要使用4个按钮,这样当按下按钮[0]时,显示开关的二进制数(向上为1,向下为0)。但是,我仍然坚持如何将按钮与二进制和led集成。我是verilog的新手,遇到了一些麻烦。有什么建议?

module final(btn,clk,sw,cathodes,anodes,led,rst);

    input clk,rst;
    output [6:0] cathodes;
    output [7:0] led;
    output [3:0] anodes;
    input [3:0] sw;
    input [3:0] btn;

    reg [6:0] cathodes;
    reg [15:0] dig;
    reg [3:0] anodes;
    reg slow_clock;
    integer count;
    reg [7:0] led;

    always @ (posedge clk)
        create_slow_clock(clk, slow_clock);

坚持如何分配led:

always @(posedge clk)
        begin
            if (btn[0:3]);
                begin
                    led[0]
                    led[1]
                    led[2]
                    led[3]

也不确定这是否正确:

always @ (posedge slow_clock)
            begin
                led=~led;
                if (rst == 0) anodes = 4'b1111;
                else
                    begin
                        case (btn)
                            0: anodes = 4'b0111;
                            1: anodes = 4'b1011;
                            2: anodes = 4'b1101;
                            3: anodes = 4'b1110;
                            default: anodes = 4'b1111;



                    endcase
                    cathodes = calc_cathode_value(dig);
                    end
                end

function[6:0] calc_cathode_value;
    input [15:0] dig;
    begin
        case (dig)
            0: calc_cathode_value = 8'b00000011;
            1: calc_cathode_value = 8'b10011111;
            2: calc_cathode_value = 8'b00100101;
            3: calc_cathode_value = 8'b00001101;
            4: calc_cathode_value = 8'b10011001;
            5: calc_cathode_value = 8'b01001001;
            6: calc_cathode_value = 8'b01000001;
            7: calc_cathode_value = 8'b00011111;
            8: calc_cathode_value = 8'b00000001; 
            9: calc_cathode_value = 8'b00001001;
            'hA: calc_cathode_value = 8'b00010001;
            'hb: calc_cathode_value = 8'b11000001;
            'hC: calc_cathode_value = 8'b01100011;
            'hd: calc_cathode_value = 8'b10000101;
            'hE: calc_cathode_value = 8'b01100001;
            'hF: calc_cathode_value = 8'b01110001;
            default: calc_cathode_value = 8'b0000001;
        endcase
    end
endfunction

task create_slow_clock;
    input clock;
    inout slow_clock;
    integer count;

        begin
            if (count > 250000)
            begin
                count = 0;
                slow_clock = ~slow_clock;
            end
            count = count + 1;
        end
    endtask

endmodule

1 个答案:

答案 0 :(得分:0)

你发信号btn定义为4位意味着case语句需要表示从0到F的所有16种可能性。想想当同时按下多个按钮时需要发生什么。如果您只关心按下其中一个按钮的情况,则您的大小写值必须为0,1,2,4,因为每个位位置代表另一个2 ^ n值。