Button to control the direction of the water light - FPGA


foreword

Environment:
1. Quartus18.0
2. vscode
3. Board model: EP4CE6F17C8
Requirements:
Press button 1, the water lamp starts to flow from right to left, press button 2, the water lamp starts to flow from left to right, button 3 Press the LED to turn on and off every 1s, and press the button 4 to keep the LED on.


1. Button

insert image description here
insert image description here

"Self-locking" means that the switch can maintain a certain state (on or off) through the locking mechanism, and "light touch" refers to the amount of force used to operate the switch. The buttons on the cyclone IV development board are light touch buttons.

2. System design

1. Module block diagram

insert image description here

2. RTL view

insert image description here

3. Source code

module key_led #(parameter  MAX_NUM = 24'd9_999_999)(
    input   clk,
    input   rst_n,
    input   [3:0] key,

    output  reg [3:0] led
);

//parameter  MAX_NUM = 24'd9_999_999;//0.2s
reg [1:0] state;
reg [23:0] cnt;
reg [3:0]  key_r;

always @(posedge clk or negedge rst_n) begin
    if(!rst_n)begin
        cnt <= 24'd0;
    end
    else if (cnt == MAX_NUM) begin
        cnt <= 24'd0;
    end
    else begin
        cnt <= cnt + 1'd1;
    end
end

always @(posedge clk or negedge rst_n) begin
    if(!rst_n)begin
        state <= 24'd0;
    end
    else if (cnt == MAX_NUM) begin
        state <= state + 2'd1;
    end
    else begin
        state <= state;
    end
end

always @(posedge clk or negedge rst_n) begin
    if(!rst_n)begin
        led <= 4'b0000;
    end
    else if (key[0] == 0) begin
            case (state)
                2'd0 : led <= 4'b0001;
                2'd1 : led <= 4'b0010;
                2'd2 : led <= 4'b0100;
                2'd3 : led <= 4'b1000; 
                default: ;
            endcase
        end
    else if (key[1] == 0) begin
            case (state)
                2'd0 : led <= 4'b1000;
                2'd1 : led <= 4'b0100;
                2'd2 : led <= 4'b0010;
                2'd3 : led <= 4'b0001; 
                default: ;
            endcase
        end
    else if (key[2] == 0) begin
            case (state)
                2'd0 : led <= 4'b1111;
                2'd1 : led <= 4'b0000;
                2'd2 : led <= 4'b1111;
                2'd3 : led <= 4'b0000; 
                default: ;
            endcase
        end
    else if (key[3] == 0) begin
            case (state)
                2'd0 : led <= 4'b1111;
                2'd1 : led <= 4'b1111;
                2'd2 : led <= 4'b1111;
                2'd3 : led <= 4'b1111; 
                default: ;
            endcase
        end
    else 
        led <= led;
end

endmodule

4. Effect

Button control running water lamp


V. Summary

The implementation here is not difficult. The interaction between the button and the LED is enhanced, and the status control LED is used. The problem is that the button does not debounce.

6. References

Button control led lights

Guess you like

Origin blog.csdn.net/qq_52215423/article/details/131749010