UART(通用异步收发传输器)
1.串口通信模块设计的目的是用来发送数据的,因此需要有一个数据输入端口;
2.串口通信,支持不同的波特率,所以需要有一个波特率设置端口;
3.串口通信的本质就是将8位的并行数据通过一根信号线,在不同的时刻传输并行数据的不同位,通过多个时刻,最终将8位并行数据全部传出;
4.串口通信以1位的低电平标志串行传输的开始,待8位数据传输完成之后,再以1位的高电平标志传输的结束;
5.控制信号,控制并转串模块什么时候开始工作?什么时候一个数据发送完成?须有一个发送开始信号,以及一个发送完成信号
代码内容如下:
module uart_byte_tx(
Clk,
Reset_n,
Data,
Send_en,
Baud_set,
uart_tx,
Tx_done
);
input Clk;
input Reset_n;
input [7:0]Data;
input Send_en;
input Baud_set;
output reg uart_tx;
output reg Tx_done;
//Baud_set = 0 就让波特率 = 9600;
//Baud_set = 1 就让波特率 = 19200
//Baud_set = 2 就让波特率 = 38400;
//Baud_set = 3 就让波特率 = 57600;
//Baud_set = 4 就让波特率 = 115200;
//计算每个波特率对应的时间周期
reg [17:0]bps_DR;
always@(*)
case(Baud_set)
0:bps_DR = 1000000000/9600/20;
1:bps_DR = 1000000000/19200/20;
2:bps_DR = 1000000000/38400/20;
3:bps_DR = 1000000000/57600/20;
4:bps_DR = 1000000/115200/20;
default bps_DR = 1000000000/9600/20;
endcase
//设计基础计数器
wire bps_clk;
assign bps_clk =(div_cnt == 1);
reg [17:0]div_cnt;
always@(posedge Clk or negedge Reset_n)
if(!Reset_n)
div_cnt <= 0;
else if(Send_en)
begin
if(div_cnt == bps_DR - 1)
div_cnt <= 0;
else
div_cnt <= div_cnt + 1'b1;
end
else
div_cnt <= 0;
//设计传入信号的计数器 总共发送十位数据 包括一个起始位和一个停止位,还有八个数据位
reg [3:0]bps_cnt;
always@(posedge Clk or negedge Reset_n)
if(!Reset_n)
bps_cnt <= 0;
else if(Send_en)
begin
if(bps_clk)begin
if(bps_cnt == 11)
bps_cnt <= 0;
else
bps_cnt <= bps_cnt + 1'b1;
end
end
else
bps_cnt <= 0;
//传输数据
always@(posedge Clk or negedge Reset_n)
if(!Reset_n)
begin
uart_tx <= 1'b1;
Tx_done <= 1'b0;
end
else
begin
case(bps_cnt)
1:
begin
uart_tx <= 0;
Tx_done <= 0;
end
2:uart_tx <= Data[0];
3:uart_tx <= Data[1];
4:uart_tx <= Data[2];
5:uart_tx <= Data[3];
6:uart_tx <= Data[4];
7:uart_tx <= Data[5];
8:uart_tx <= Data[6];
9:uart_tx <= Data[7];
10:uart_tx <=1;
11:begin
uart_tx <=1;
Tx_done <=1'b1;
end
default uart_tx <=1;
endcase
end
endmodule
仿真代码内容如下:
`timescale 1ns / 1ps
module uart_byte_tx_tb();
reg Clk;
reg Reset_n;
reg [7:0]Data;
reg Send_en;
reg [2:0]Baud_set;
wire uart_tx;
wire Tx_done;
uart_byte_tx uart_byte_tx(
.Clk(Clk),
.Reset_n(Reset_n),
.Data(Data),
.Send_en(Send_en),
.Baud_set(Baud_set),
.uart_tx(uart_tx),
.Tx_done(Tx_done)
);
initial Clk = 1;
always#10 Clk = ~Clk;
initial begin
Reset_n = 0;
Data = 0;
Send_en = 0;
Baud_set = 4;
#201;
Reset_n = 1;
#100;
Data = 8'h57;
Send_en = 1;
#20;
@(posedge Tx_done);
Send_en = 0;
#20000;
Data = 8'h75;
Send_en = 1;
#20;
@(posedge Tx_done);
#20000;
Send_en = 0;
$stop;
end
endmodule
标签:Baud,set,tx,FPGA,Send,发送,en,串口,波特率
From: https://blog.51cto.com/u_16055951/7585621