使用一段式狀態(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