HDLbits: Fsm serial

根据题意设计了四个状态,写出代码如下:

cs 复制代码
module top_module(
    input clk,
    input in,
    input reset,    // Synchronous reset
    output done
); 
    parameter IDLE = 3'b000, START = 3'b001, DATA = 3'b010, STOP = 3'b100, bit_counter_end = 4'd7;
    
    reg [2:0] state,next_state;
    
    reg [3:0] bit_counter;
    
    always@(*)begin
        case(state)
            START: next_state = DATA;
            DATA: next_state = (bit_counter >= bit_counter_end)? (in?STOP:IDLE):DATA;
            STOP: next_state = in?IDLE:START;
            IDLE: next_state = in?IDLE:START;
            default: next_state = IDLE;
        endcase
    end
    
    always@(posedge clk)begin
        if(reset)
            state <= IDLE;
        else
            state <= next_state;
    end
    
    always@(posedge clk)begin
        if(reset)
           bit_counter <= 4'd0;            
        else if(state == DATA)
           bit_counter <= bit_counter + 4'd1;  
        else
           bit_counter <= 4'd0;
    end
        
    assign done = (state == STOP);    

endmodule

时序图如下,有误:

参考网上的答案,加入了一个ERROR状态表示例题时序图"?"的时候,下面代码没问题了

注意bit_counter计数的那块如果用state==DATA判断,那么上面长度判断就得用7,如果用next_state==DATA判断,上面长度判断就得用8

cs 复制代码
module top_module(
    input clk,
    input in,
    input reset,    // Synchronous reset
    output done
); 
    parameter IDLE = 4'b0000, START = 4'b0001, DATA = 4'b0010, STOP = 4'b0100, ERROR = 4'b1000, 
    bit_counter_end = 4'd7;
    
    reg [3:0] state,next_state;
    
    reg [3:0] bit_counter;
    
    always@(*)begin
        case(state)
            START: next_state = DATA;
            DATA: next_state = (bit_counter >= bit_counter_end)? (in?STOP:ERROR):DATA;
            STOP: next_state = in?IDLE:START;
            ERROR: next_state = in?IDLE:ERROR;
            IDLE: next_state = in?IDLE:START;
            default: next_state = IDLE;
        endcase
    end
    
    always@(posedge clk)begin
        if(reset)
            state <= IDLE;
        else
            state <= next_state;
    end
    
    always@(posedge clk)begin
        if(reset)
           bit_counter <= 0;            
        else if(state == DATA)          
           bit_counter <= bit_counter + 1;
        else
           bit_counter <= 0;
    end
        
    assign done = (state == STOP);    

endmodule
相关推荐
Dale_e7 个月前
18 19 SPI接口的74HC595驱动数码管实验
经验分享·笔记·学习·fpga开发·verilog学习
Dale_e7 个月前
15 ABC基于状态机的按键消抖原理与状态转移图
经验分享·笔记·学习·fpga开发·verilog学习
Dale_e7 个月前
16 亚稳态原理和解决方案
经验分享·笔记·学习·fpga开发·verilog学习
weixin_410042389 个月前
Verilog学习 | 用initial语句写出固定的波形
verilog学习
weixin_410042381 年前
HDLbits: Fsm ps2
verilog学习
weixin_410042381 年前
HDLbits: ps2data
verilog学习
weixin_410042381 年前
HDLbits:Lemmings4
verilog学习
weixin_410042381 年前
HDLbits:Exams/ece241 2013 q4
verilog学习
Moon_31819617251 年前
征战MINI学习路线
嵌入式硬件·verilog学习·fpg学习路线·小梅哥acx720·征战mini开发板