|4 个 LED 不同频率

4 个 LED 不同频率

作者: 石志超更新于: 2026/2/27
module top (
    input        sys_clk,       // 50MHz 系统时钟
    input        sys_rst_n,     // 复位(低有效)
    output [3:0] led            // 4 个 LED(低有效)
);

// 4 组不同频率的分频参数
parameter CNT_MAX0 = 25_000_000 - 1;  // LED0: 1Hz
parameter CNT_MAX1 = 12_500_000 - 1;  // LED1: 2Hz
parameter CNT_MAX2 =  5_000_000 - 1;  // LED2: 5Hz
parameter CNT_MAX3 =  2_500_000 - 1;  // LED3: 10Hz

reg [24:0] cnt0, cnt1, cnt2, cnt3;    // 4 个独立计数器
reg [3:0]  led_r;                     // 每位控制一个 LED

// ── LED0 — 1Hz ──
always @(posedge sys_clk or negedge sys_rst_n) begin
    if (!sys_rst_n) begin cnt0 <= 0; led_r[0] <= 1'b0; end
    else if (cnt0 >= CNT_MAX0) begin cnt0 <= 0; led_r[0] <= ~led_r[0]; end
    else cnt0 <= cnt0 + 1;
end

// ── LED1 — 2Hz ──
always @(posedge sys_clk or negedge sys_rst_n) begin
    if (!sys_rst_n) begin cnt1 <= 0; led_r[1] <= 1'b0; end
    else if (cnt1 >= CNT_MAX1) begin cnt1 <= 0; led_r[1] <= ~led_r[1]; end
    else cnt1 <= cnt1 + 1;
end

// ── LED2 — 5Hz ──
always @(posedge sys_clk or negedge sys_rst_n) begin
    if (!sys_rst_n) begin cnt2 <= 0; led_r[2] <= 1'b0; end
    else if (cnt2 >= CNT_MAX2) begin cnt2 <= 0; led_r[2] <= ~led_r[2]; end
    else cnt2 <= cnt2 + 1;
end

// ── LED3 — 10Hz ──
always @(posedge sys_clk or negedge sys_rst_n) begin
    if (!sys_rst_n) begin cnt3 <= 0; led_r[3] <= 1'b0; end
    else if (cnt3 >= CNT_MAX3) begin cnt3 <= 0; led_r[3] <= ~led_r[3]; end
    else cnt3 <= cnt3 + 1;
end

assign led = ~led_r;  // 低有效:取反后输出

endmodule