Uart接收狀態(tài)機(jī)設(shè)計

使用一段式狀態(tài)機(jī),設(shè)計了一個uart接收器,采用16倍波特率采樣,無奇偶校驗。

狀態(tài)轉(zhuǎn)移表
仿真輸出圖,串口數(shù)據(jù)0x55
module uart_rx_fsm(clk_16,rx,data_out,ready);
    input clk_16,rx;
    output reg [7:0] data_out;
    output reg ready;
    
    reg[2:0] state;
    reg[3:0] count;
    reg[2:0] bits;
    reg[7:0] buffer;
    
    parameter IDLE = 0; //空閑狀態(tài)
    parameter WAIT_S0 =1;//等待起始位中點(diǎn)
    parameter CHECK_S0 = 2;//復(fù)核起始位
    parameter WAIT_BIT = 3;//等待數(shù)據(jù)位中點(diǎn)
    parameter STORE_BIT = 4;//存儲數(shù)據(jù)位
    parameter WAIT_STOP = 5;//等待停止位中點(diǎn)
    parameter CHECK_STOP = 6;//檢查停止位是否正確
    
    always @(posedge clk_16)begin
        case(state)
            IDLE:begin count<=0;bits<=0;ready<=0;
                if (rx)     state<=IDLE;
                else        state <= WAIT_S0;
            end
            
            WAIT_S0:begin count <= count+1'd1;bits<=0;ready<=0;
                if(count == 6) state <= CHECK_S0;
                else                state <= WAIT_S0;
            end
            
            CHECK_S0:begin count<=0;bits<=0;ready<=0;
                if (rx)     state<=IDLE;
                else        state <= WAIT_BIT;
            end
            
            WAIT_BIT:begin count <= count+1'd1;ready<=0;
                if(count== 14) state <= STORE_BIT;
                else                state <= WAIT_BIT;
            end
            
            STORE_BIT:begin count <= 0;bits<=bits+1'd1;ready<=0; buffer[bits]<= rx;
                if(bits ==7) state <= WAIT_STOP;
                else state<=WAIT_BIT;               
            end
            
            WAIT_STOP:begin count <= count+1'd1;bits<=0;ready<=0;
                if(count == 14) state <= CHECK_STOP;
                else                state <= WAIT_STOP;
            end
            
            CHECK_STOP:begin count <= 0;bits<=0; state<=IDLE;               
                if(rx) begin data_out <= buffer;ready<=1; end
                else    ready<= 0;
            end
            default:state<=IDLE;
        endcase
    end 
endmodule 
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容