module uart_rx( input uart_clk, //16倍bps(153.6khz) input rst_n, //系统复位:低电平有效 input rxd, //接收信号线 output rx_data_flag, //接受完8bit数据的标志 output [7:0] rx_data //接受8bit数据 ); reg [7:0] cnt; //节拍计数器 reg endmodule
时间: 2023-05-28 19:06:24 浏览: 84
//定义状态常量,用于状态机
parameter IDLE = 2'b00; //空闲状态
parameter START = 2'b01; //开始接收状态
parameter RECEIVE = 2'b10; //接收数据状态
module uart_rx(
input uart_clk, //16倍bps(153.6khz)
input rst_n, //系统复位:低电平有效
input rxd, //接收信号线
output rx_data_flag, //接受完8bit数据的标志
output [7:0] rx_data //接受8bit数据
);
reg [7:0] cnt; //节拍计数器
reg [1:0] state; //状态寄存器
reg [7:0] shift_reg; //移位寄存器,用于存储接收的数据
reg start_bit_detected; //起始位检测标志
//状态机
always @(posedge uart_clk or negedge rst_n) begin
if (!rst_n) begin
state <= IDLE;
cnt <= 0;
shift_reg <= 0;
start_bit_detected <= 0;
end
else begin
case (state)
IDLE: begin
if (!rxd) begin
state <= START;
cnt <= 0;
start_bit_detected <= 1;
end
end
START: begin
if (cnt == 7) begin
state <= RECEIVE;
cnt <= 0;
end
else begin
cnt <= cnt + 1;
end
end
RECEIVE: begin
if (cnt == 7) begin
state <= IDLE;
cnt <= 0;
rx_data_flag <= 1;
rx_data <= shift_reg;
shift_reg <= 0;
start_bit_detected <= 0;
end
else begin
cnt <= cnt + 1;
shift_reg <= {shift_reg[6:0], rxd};
end
end
default: state <= IDLE; //默认状态
endcase
end
end
endmodule