2

我想知道是否有人可以阐明如何在 verilog 中编写一个 LED 模式 fsm,它在 8 个 LED 上产生 4 种不同的模式,并且 LED 会改变每个滴答脉冲,有 4 个按钮可以触发 4 种不同的模式,每种模式将触发 8 个 LED 以模式移动,即从左到右,从右到左。

我写了一个顺序逻辑,但我不知道如何将 LED 的模式插入到每个状态中。这是我的代码:

`timescale 1ns / 1ps
module pattern_fsm(
input [3:0] mode,
input tick,
input clk,
input reset,
output reg [7:0] Led
);

 reg [3:0] state, nextstate;

 parameter s0 = 4'b0001;
 parameter s1 = 4'b0010;
 parameter s2 = 4'b0100;
 parameter s3 = 4'b1000;

 always @(posedge clk, posedge reset)
    if(reset) 
        state <= s0;
    else
        state <= nextstate;

always @(*)
    begin
        case(state)
            s0: if(mode == 4'b0001) nextstate = s0;
                    else nextstate = s3;
            s1: if(mode == 4'b0010) nextstate = s1;
                    else nextstate = s0;
            s2: if(mode == 4'b0100) nextstate = s2;
                    else nextstate = s1;
            s3: if(mode == 4'b1000) nextstate = s3;
                    else nextstate = s2;
            default: nextstate = s0;
        endcase
    end

always @(state)
    begin
        case(state)
            s0: Led = 8'b00000001;
            s1: Led = 8'b00000010;
            s2: Led = 8'b00000011;
            s3: Led = 8'b00000100;
        endcase
    end

endmodule
4

1 回答 1

1

也许你可以使用移位操作?

always @(posedge clk or posedge reset )
    if(reset) begin
        Led <= 8'h00;
    end
    else begin
        case(state)
            s0: Led <= 8'h01;              // a single Led lit 
            s1: Led <= {Led[0], Led[7:1]}; // rotate right
            s2: Led <= {Led[6:0], Led[7]}; // rotate left
            s3: Led <= ~Led;               // flip?
            default: Led <= Led;           // do nothing
        endcase
    end

我希望你发现这很有启发性。我还没有测试过这段代码,所以要小心......

于 2012-10-12T16:34:57.753 回答