【作业要求】
1、什么是波特率?
单位时间内传输的码元符号个数
2、利用状态机编写串口接收端代码?
module uart_fsm_rx(
input sysclk,
input rst_n ,
input rx ,
output reg [7:0] rx_date,
output reg rx_done
);
localparam IDLE = 4'b0001; //空闲位
localparam START = 4'b0010; //起始位
localparam DATE = 4'b0100; //数据位
localparam STOP = 4'b1000; //停止位
parameter COLOCKFREQUEN = 50_000_000;
parameter BAUD = 115200;
parameter cnt_baud_max = COLOCKFREQUEN/BAUD;
reg [12:0] cnt_baud;
reg [2:0] cnt_bit;
reg [3:0] curt_state, next_state;
reg rx_reg1, rx_reg2;
always@(posedge sysclk or negedge rst_n) //rx打两拍
if(!rst_n)begin
rx_reg1 <= 0;
rx_reg2 <= 0;
end
else
begin
rx_reg1 <= rx;
rx_reg2 <= rx_reg1;
end
always@(posedge sysclk or negedge rst_n) //一段:总的状态
if(!rst_n)
curt_state <= IDLE;
else
curt_state <= next_state;
always@(*) //二段(组合逻辑):状态+动作
case(curt_state)
IDLE:
begin
if(rx_reg2 == 0)
next_state = START;
end
START:
begin
if(cnt_baud == cnt_baud_max- 1)
next_state = DATE;
end
DATE:
begin
if((cnt_baud == cnt_baud_max- 1)&&(cnt_bit == 7))
next_state = STOP;
end
STOP:
begin
if(cnt_baud == cnt_baud_max- 1)
next_state = IDLE;
end
default: next_state = IDLE;
endcase
always@(posedge sysclk or negedge rst_n) //三段:状态+动作+现象
if(!rst_n)
begin
cnt_baud <= 0;
cnt_bit <= 0;
rx_date <= 0;
rx_done <= 0;
end
else
begin
cnt_baud <= cnt_baud + 1;
rx_done <= 0;
case(curt_state)
IDLE:
begin
cnt_baud <= 0;
cnt_bit <= 0;
end
START:
begin
if(cnt_baud == cnt_baud_max)
cnt_baud <= 0;
end
DATE:
begin
if(cnt_baud == cnt_baud_max)begin
cnt_bit <= cnt_bit + 1;
cnt_baud <= 0;
rx_date[cnt_bit] <= rx_reg2;
end
end
STOP:
begin
if(cnt_baud == cnt_baud_max - 1)
cnt_baud <= 0;
rx_done <= 1;
end
endcase
end
endmodule【我的做法】
【结果/时序图/实验现象(可视频)】

评论 0