Verilog code for led blinking

Following is a simple Verilog code to control blinking of LED on FPGA board. The code uses a clock divider to control the blinking speed of the LED, and a counter to determine the state of the LED (on/off).

module led_blink(
  input clk, //时钟输入
  output reg led // LED输出
);

// 定义计数器变量
reg [23:0] counter;

// 定义闪烁速度,此处为1秒
localparam COUNTER_MAX = 50000000;

// 时钟分频器
reg [7:0] divider;
always @(posedge clk) begin
    if (divider == 8'd100) begin
        counter <= counter + 1;
        divider <= 0;
    end else begin
        divider <= divider + 1;
    end
end

// LED状态计算
always @(posedge clk) begin
    if (counter == COUNTER_MAX) begin
        led <= ~led; // 取反输出
        counter <= 0; // 计数器清零
    end
end

endmodule

This code divides the clock down to 1 second, and the LED will toggle every second. You can adjust the blinking speed according to your needs, just modify the value of COUNTER_MAX. Note that you also need to connect the LEDs to the correct pins before you can burn the code onto the FPGA board.

Guess you like

Origin blog.csdn.net/qq_36314279/article/details/129439954