module uart_tx(
input clk,
input rst,
input start,
input [7:0] data,
output reg tx_done,
output reg tx_out
);
// 定义状态机的状态
typedef enum logic [2:0] {
IDLE, START, DATA, STOP
} state_t;
reg [10:0] count; // 用于计数发送的位数
reg [2:0] state; // 用于记录状态机的当前状态
reg [7:0] tx_data; // 用于暂存要发送的数据
always @(posedge clk) begin
if (rst) begin
state <= IDLE;
count <= 0;
tx_done <= 0;
tx_out <= 1; // 初始化时设置为停止位
end else begin
case (state)
IDLE: begin
if (start) begin
tx_data <= data;
state <= START;
count <= 0;
tx_done <= 0;
tx_out <= 0; // 开始位
end
end
START: begin
if (count < 8) begin
tx_out <= tx_data[count];
count <= count + 1;
end else begin