我用单片机测试了一个很简单的RS232程序,通过PC发数据给FPGA,然后FPGA再把接收到的数据发回到PC。用的程序也是网上。
测试发现发HEX码都正确,但是发字符串会有问题。是因为出现误码了吗?感觉不太像。还是哪里没有设置对。
下面是程序
module RS232(clk,rst_n,rs232_rx,rs232_tx,led0,led1);
input clk;
input rst_n;
input rs232_rx;
output rs232_tx;
output led0;
output led1;
wire bps_start1,bps_start2;
wire clk_bps1,clk_bps2;
wire[7:0] rx_data;
wire rx_int;
speed_select speed_select_tx(
.clk(clk),
.rst_n(rst_n),
.bps_start(bps_start2),
.clk_bps(clk_bps2)
);
my_uart_tx tx(
.clk(clk),
.rst_n(rst_n),
.rx_data(rx_data),
.rx_int(rx_int),
.rs232_tx(rs232_tx),
.clk_bps(clk_bps2),
.bps_start(bps_start2),
.led1(led1)
);
my_uart_rx rx(
.clk(clk),
.rst_n(rst_n),
.rx_data(rx_data),
.rx_int(rx_int),
.rs232_rx(rs232_rx),
.clk_bps(clk_bps1),
.bps_start(bps_start1),
.led0(led0)
);
speed_select speed_select_rx(
.clk(clk),
.rst_n(rst_n),
.bps_start(bps_start1),
.clk_bps(clk_bps1)
);
endmodule
`timescale 1ns / 1ps
module my_uart_tx( clk,rst_n,
rx_data,rx_int,rs232_tx,
clk_bps,bps_start,led1
);
input clk;// 50MHz主时钟
input rst_n;//低电平复位信号
input clk_bps;// clk_bps_r高电平为接收数据位的中间采样点,同时也作为发送数据改变点
input[7:0] rx_data;//接收数据寄存器
input rx_int;//接收数据中断信号,接收到数据期间始终为高电平,在该模块中利用它的下降沿来启动串口发送数据
output rs232_tx;// RS232发送数据信号
output bps_start;//接收或者要发送数据,波特率时钟启动信号置位
output led1;
reg rx_int0,rx_int1,rx_int2; //rx_int信号寄存器,捕捉下降沿滤波用
wire neg_rx_int; // rx_int下降沿标志位
always @ (posedge clk or negedge rst_n) begin
if(!rst_n) begin
rx_int0 <= 1'b0;
rx_int1 <= 1'b0;
rx_int2 <= 1'b0;
end
else begin
rx_int0 <= rx_int;
rx_int1 <= rx_int0;
rx_int2 <= rx_int1;
end
end
assign neg_rx_int = ~rx_int1 & rx_int2; //捕捉到下降沿后,neg_rx_int拉高保持一个主时钟周期
reg[7:0] tx_data; //待发送数据的寄存器
reg bps_start_r;
reg tx_en; //发送数据使能信号,高有效
reg[3:0] num;
always @ (posedge clk or negedge rst_n) begin
if(!rst_n) begin
bps_start_r <= 1'bz;
tx_en <= 1'b0;
tx_data <= 8'd0;
end
else if(neg_rx_int) begin //接收数据完毕,准备把接收到的数据发回去
bps_start_r <= 1'b1;
tx_data <= rx_data; //把接收到的数据存入发送数据寄存器
tx_en <= 1'b1; //进入发送数据状态中
end
else if(num==4'd11) begin //数据发送完成,复位
bps_start_r <= 1'b0;
tx_en <= 1'b0;
end
end
assign bps_start = bps_start_r;
reg rs232_tx_r;
reg led1_r;
always @ (posedge clk or negedge rst_n) begin
if(!rst_n) begin
num <= 4'd0;
rs232_tx_r <= 1'b1;
led1_r<=1'b0;
end
else if(tx_en) begin
if(clk_bps) begin
led1_r<=1'b1;
case (num)
4'd0: rs232_tx_r <= 1'b0; //发送起始位
4'd1: rs232_tx_r <= tx_data[0]; //发送bit0
4'd2: rs232_tx_r <= tx_data[1]; //发送bit1
4'd3: rs232_tx_r <= tx_data[2]; //发送bit2
4'd4: rs232_tx_r <= tx_data[3]; //发送bit3
4'd5: rs232_tx_r <= tx_data[4]; //发送bit4
4'd6: rs232_tx_r <= tx_data[5]; //发送bit5
4'd7: rs232_tx_r <= tx_data[6]; //发送bit6
4'd8: rs232_tx_r <= tx_data[7]; //发送bit7
4'd9: rs232_tx_r <= 1'b1; //发送结束位
default: rs232_tx_r <= 1'b1;
endcase
num <= num+1'b1;
end
else if(num==4'd11)
begin
num <= 4'd0; //复位
led1_r<=1'b0;
end
end
end
assign rs232_tx = rs232_tx_r;
assign led1=led1_r;
endmodule
`timescale 1ns / 1ps
module my_uart_rx(clk,rst_n,
rs232_rx,rx_data,rx_int,
clk_bps,bps_start,led0);
input clk; // 50MHz主时钟
input rst_n; //低电平复位信号
input rs232_rx; // RS232接收数据信号
input clk_bps; // clk_bps的高电平为接收或者发送数据位的中间采样点
output bps_start; //接收到数据后,波特率时钟启动信号置位
output[7:0] rx_data; //接收数据寄存器,保存直至下一个数据来到
output rx_int; //接收数据中断信号,接收到数据期间始终为高电平
output led0;
reg rs232_rx0,rs232_rx1,rs232_rx2,rs232_rx3; //接收数据寄存器,滤波用
wire neg_rs232_rx; //表示数据线接收到下降沿
always @ (posedge clk or negedge rst_n) begin
if(!rst_n) begin
rs232_rx0 <= 1'b0;
rs232_rx1 <= 1'b0;
rs232_rx2 <= 1'b0;
rs232_rx3 <= 1'b0;
end
else begin
rs232_rx0 <= rs232_rx;
rs232_rx1 <= rs232_rx0;
rs232_rx2 <= rs232_rx1;
rs232_rx3 <= rs232_rx2;
end
end
assign neg_rs232_rx = rs232_rx3 & rs232_rx2 & ~rs232_rx1 & ~rs232_rx0; //接收到下降沿后neg_rs232_rx置高一个时钟周期
reg bps_start_r;
reg[3:0] num; //移位次数
reg rx_int; //接收数据中断信号,接收到数据期间始终为高电平
always @ (posedge clk or negedge rst_n)
if(!rst_n) begin
bps_start_r <= 1'bz;
rx_int <= 1'b0;
end
else if(neg_rs232_rx) begin //接收到串口接收线rs232_rx的下降沿标志信号
bps_start_r <= 1'b1; //启动串口准备数据接收
rx_int <= 1'b1; //接收数据中断信号使能
end
else if(num==4'd12) begin //接收完有用数据信息
bps_start_r <= 1'b0; //数据接收完毕,释放波特率启动信号
rx_int <= 1'b0; //接收数据中断信号关闭
end
assign bps_start = bps_start_r;
reg[7:0] rx_data_r; //串口接收数据寄存器,保存直至下一个数据来到
reg led0_r;
reg[7:0] rx_temp_data; //当前接收数据寄存器
always @ (posedge clk or negedge rst_n)
if(!rst_n) begin
rx_temp_data <= 8'd0;
num <= 4'd0;
rx_data_r <= 8'd0;
led0_r<=1'b0;
end
else if(rx_int) begin //接收数据处理
led0_r<=1'b1;
if(clk_bps) begin //读取并保存数据,接收数据为一个起始位,8bit数据,1或2个结束位
case (num)
4'd1: rx_temp_data[0] <= rs232_rx; //锁存第0bit
4'd2: rx_temp_data[1] <= rs232_rx; //锁存第1bit
4'd3: rx_temp_data[2] <= rs232_rx; //锁存第2bit
4'd4: rx_temp_data[3] <= rs232_rx; //锁存第3bit
4'd5: rx_temp_data[4] <= rs232_rx; //锁存第4bit
4'd6: rx_temp_data[5] <= rs232_rx; //锁存第5bit
4'd7: rx_temp_data[6] <= rs232_rx; //锁存第6bit
4'd8: rx_temp_data[7] <= rs232_rx; //锁存第7bit
default: ;
endcase
num <= num+1'b1;
end
else if(num == 4'd12) begin //我们的标准接收模式下只有1+8+1(2)=11bit的有效数据
num <= 4'd0; //接收到STOP位后结束,num清零
rx_data_r <= rx_temp_data; //把数据锁存到数据寄存器rx_data中
led0_r<=1'b0;
end
end
assign rx_data = rx_data_r;
assign led0=led0_r;
endmodule
module speed_select(clk,rst_n,
bps_start,clk_bps);
input clk; // // 50MHz主时钟
input rst_n; //低电平复位信号
input bps_start; ////接收到数据后,波特率时钟启动信号置位
output clk_bps; // clk_bps的高电平为接收或者发送数据位的中间采样点
`define BPS_PARA 5207 //波特率9600分频点数
`define BPS_PARA_2 2603
reg[12:0] cnt;
reg clk_bps_r;
reg[2:0] uart_ctrl; // uart????????
always @ (posedge clk or negedge rst_n)
begin
if(!rst_n) cnt <= 13'd0;
else if((cnt == `BPS_PARA) || !bps_start) cnt <= 13'd0; //clear
else cnt <= cnt+1'b1; end
always @ (posedge clk or negedge rst_n)
begin
if(!rst_n) clk_bps_r <= 1'b0;
else if(cnt == `BPS_PARA_2) clk_bps_r <= 1'b1;
else clk_bps_r <= 1'b0;
end
assign clk_bps = clk_bps_r;
endmodule