在摩尔机上使输出LED闪烁

问题描述

我正在设计的是一种摩尔机,可以为每种状态赋予特定的颜色。 这是我的代码的一部分。

    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
    

endmodule

在上面显示的代码之前,存在有关分配与输入对应的状态的代码。

上面显示的代码是确定摩尔机的输出值。 (因此条件为*)

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

但是S1的条件不仅是特定的颜色,而且还必须“闪烁” 1秒钟。

我已经在Google上搜索了“闪烁LED”验证码,但与我的情况没什么不同。

我不知道所需的编码块。

您能给我一些建议或答案,以补充使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

使用sel在2个led值之间进行选择。

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...