使摩尔机中的输出 LED 闪烁

To make output LED blink in moore machine

我正在设计的是一台摩尔机器,可以为每个状态提供特定的颜色。 这是我的代码的一部分。

    always @(*) begin            
        case (state)
            S0 : led = 6'b111111;
            S1 : led = 6'b010100;  // have to blink //
            S2 : led = 6'b100010;
            S3 : led = 6'b110110; 
            default : led_output = 6'b000000;
        endcase
    end
    

结束模块

在上述代码之前,有输入对应的赋值状态的代码

上面的代码是判断摩尔机的输出值。 (因此条件为*)

我剩下的就是为每个状态分配led输出值。

然而,S1 的条件不仅是特定颜色,而且必须 'blink' 持续 1 秒。

我已经在谷歌上搜索了 'blink LED' verilog 代码,但它们与我的情况略有不同。

我不知道需要什么编码块。

你能给我一些建议或答案吗?要添加什么才能使 S1 闪烁?

要让它闪烁,需要区分2种状态。因此,创建一个 1 位信号,例如 sel,它在 S1 状态下切换,并且其切换速度和占空比满足您在 'blink' for period of 1s 中的要求。这基本上是借助计数器来实现的。

reg [3:0] cnt; // assume 4-bit long
reg sel;

// counter to determine the HIGH / LOW time of sel.
always@(posedge clk or negedge resetn)begin
    if(!resetn)begin
        cnt <= 4'h0;
    end
    // since you can exit S1 state at any time using push_button,
    // make sure cnt is always ZERO when entering S1 state next time.
    // also to save power when using clock gating, because this statement
    // is only active for 1 cycle.
    else if((state == S1) & ((push_button == 2'b01) | (push_button == 2'b10)))begin  // S1 exit condition
        cnt <= 4'h0;
    end
    else if(state == S1)begin
        // sel HIGH lasts <HIGH_MAX_VAL>+1 cycles
        // sel LOW lasts <LOW_MAX_VAL>+1 cycles
        if(cnt == (sel ? <HIGH_MAX_VAL> : <LOW_MAX_VAL>))begin
            cnt <= 4'h0;
        end
        else begin
            cnt <= cnt + 4'h1;
        end
    end
end

always@(posedge clk or negedge resetn)begin
    if(!resetn)begin
        sel <= 1'h0;
    end
    else if((state == S1) & ((push_button == 2'b01) | (push_button == 2'b10)))begin
        sel <= 1'h0;
    end
    else if(state == S1)begin
        if(cnt == (sel ? <HIGH_MAX_VAL> : <LOW_MAX_VAL>))begin
            sel <= ~sel;
        end
    end
end

在 2 个 LED 值之间使用 sel 到 select。

always@(*)begin            
    case(state)
        ....
        S1 : led = sel ? 6'b010100 : <ANOTHER_VALUE>;
        ....
    endcase
end