工作串口通信

// 头文件//

module my_uart_top(
CLK,rst_n,rs232_rx,rd_fifo_ena,rd_fifo_data,
rs232_tx,wr_fifo_req,rd_fifo_req,wr_fifo_data,clk_bps_tx,clk_h
);

//Inputs
input CLK; // 50MHz?????
input rst_n; //??????λ???
input rs232_rx; // RS232???????????
input rd_fifo_ena;
input [7:0] rd_fifo_data;

//Outputs
output rs232_tx; // RS232???????????
output wr_fifo_req;
output rd_fifo_req;
output [7:0] wr_fifo_data;
output clk_bps_tx;
output clk_h;

wire clk_bps_rx;
wire clk_bps_tx;
wire bps_start_rx;
wire bps_start_tx;
wire rd_fifo_ena1;
wire [7:0] rd_fifo_data1; //??????????????????????????????
wire [7:0] cnt;

//wire rd_fifo_0 = (cnt>=6)? rd_fifo_req : 1‘b0;
wire clk_100MHz;

/////////////////////////////////////////////////////////////////////////////////////////
//------rx---------//
//
pll pll(
.inclk0(CLK),
.c0(clk),
.c1(clk_25M)

);

speed_select speed_rx( .clk(clk), //???????????飬??????????鸴?ã?????????????
.rst_n(rst_n),
.bps_start(bps_start_rx),
.clk_bps(clk_bps_rx)
);

my_uart_rx uart_rx( .clk(clk), //???????????
.rst_n(rst_n),
.rs232_rx(rs232_rx),
.clk_bps(clk_bps_rx),
.clk_h(clk_h),

.en_1(en_1),

.bps_start(bps_start_rx),
.wr_fifo_data(wr_fifo_data),
.wr_fifo_req(wr_fifo_req) //"1"
);

//wr_fifo fifo (
// .clock(clk),
// .data(wr_fifo_data),
// .rdreq(rd_fifo),
// .wrreq(wr_fifo_req),
// .empty(rd_fifo_ena1),
// .full(),
// .q(rd_fifo_data1),
// .usedw(cnt)
// );

//////////////////////////////////////////////////////////////////////////////////////////
//
speed_select speed_tx( .clk(clk), //???????????飬??????????鸴?ã?????????????
.rst_n(rst_n),
.bps_start(bps_start_tx),
.clk_bps(clk_bps_tx)
);

wire[7:0] wr_fifo_data;
wire en_1;

my_uart_tx uart_tx( .clk(clk), //???????????
.rst_n(rst_n),
.clk_bps(clk_bps_tx),
.rd_fifo_data(rd_fifo_data),
.rd_fifo_ena(rd_fifo_ena),

.wr_fifo_data(wr_fifo_data),
.en_1(en_1),

.rs232_tx(rs232_tx),
.bps_start(bps_start_tx),
.rd_fifo_req(rd_fifo_req)
);

wire rd_fifo_req;
zh_1 zh_1(
. clk(clk),
. rd_fifo_req(rd_fifo_req),
. wrfifo_1(wr_fifo_data)
//. chu_1(chu_1)

);

zh_2 zh_2(
.clk(clk),
. wrfifo_1(wr_fifo_data),
. rd_fifo_req(rd_fifo_req),
.clk_25M(clk_25M)
);

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????????????????????λ???м???????

parameter BPS9600 = 5207, //???????9600bps
BPS19200 = 2603, //???????19200bps
BPS38400 = 2603, //???????38400bps BPS38400 = 1301,
BPS57600 = 867, //???????57600bps
BPS115200 = 433; //???????115200bps

parameter BPS9600_2 = 2603,
BPS19200_2 = 1301,
BPS38400_2 = 1301, //650
BPS57600_2 = 433,
BPS115200_2 = 216;

parameter CLK_100M = 1000000,
BTF9600 = 96,
BTF19200 = 192,
BTF38400 = 384,
BTF57600 = 576,
BTF115200 = 1152;

reg[12:0] bps_para; //????????????
reg[12:0] bps_para_2; //????????????
reg[12:0] cnt; //???????
reg clk_bps_r; //?????????????

reg[31:0] clk_count;

//----------------------------------------------------------
reg[2:0] uart_ctrl; // uart??????????????
//----------------------------------------------------------

always @ (posedge clk or negedge rst_n)
begin
if(!rst_n)
uart_ctrl <= 3‘d2; //?????????38400bps
else
case (uart_ctrl) //??????????
3‘d0: begin
bps_para <= BPS9600;
bps_para_2 <= BPS9600_2;
end
3‘d1: begin
bps_para <= BPS19200;
bps_para_2 <= BPS19200_2;
end
3‘d2: begin
bps_para <= BPS38400;
bps_para_2 <= BPS38400_2;
end
3‘d3: begin
bps_para <= BPS57600;
bps_para_2 <= BPS57600_2;
end
3‘d4: begin
bps_para <= BPS115200;
bps_para_2 <= BPS115200_2;
end
default: ;
endcase
end

always @ (posedge clk or negedge rst_n)
begin
if(!rst_n)
cnt <= 13‘d0;
else if(bps_start)
if(cnt==bps_para)
cnt <= 13‘d0;
else
cnt <= cnt + 1‘b1; //????????????????
else
cnt <= 13‘d0;
end

always @ (posedge clk or negedge rst_n)
begin
if(!rst_n)
clk_bps_r <= 1‘b0;
else if(cnt==bps_para_2)// && bps_start)
clk_bps_r <= 1‘b1; // clk_bps_r???????????????????λ???м???????
else
clk_bps_r <= 1‘b0;
end

assign clk_bps = clk_bps_r;

endmodule

/*

input clk; //50MHz?????
input rst_n; //??????λ???
input bps_start; //????????????????????????????λ
output clk_bps; // clk_bps????????????????????λ???м???????

parameter BPS9600 = 5207, //???????9600bps
BPS19200 = 2603, //???????19200bps
BPS38400 = 2603, //???????38400bps BPS38400 = 1301,
BPS57600 = 867, //???????57600bps
BPS115200 = 433; //???????115200bps

parameter BPS9600_2 = 2603,
BPS19200_2 = 1301,
BPS38400_2 = 1301, //650
BPS57600_2 = 433,
BPS115200_2 = 216;

parameter CLK_100M = 1000000,
BTF9600 = 96,
BTF19200 = 192,
BTF38400 = 384,
BTF57600 = 576,
BTF115200 = 1152;

reg[12:0] bps_para; //????????????
reg[12:0] bps_para_2; //????????????
reg[12:0] cnt; //???????
reg clk_bps_r; //?????????????

reg[31:0] clk_count;

//----------------------------------------------------------
reg[2:0] uart_ctrl; // uart??????????????
//----------------------------------------------------------

//always @ (posedge clk or negedge rst_n)
//begin
// if(!rst_n)
// uart_ctrl <= 3‘d2; //?????????38400bps
// else
// case (uart_ctrl) //??????????
// 3‘d0: begin
// bps_para <= BPS9600;
// bps_para_2 <= BPS9600_2;
// end
// 3‘d1: begin
// bps_para <= BPS19200;
// bps_para_2 <= BPS19200_2;
// end
// 3‘d2: begin
// bps_para <= BPS38400;
// bps_para_2 <= BPS38400_2;
// end
// 3‘d3: begin
// bps_para <= BPS57600;
// bps_para_2 <= BPS57600_2;
// end
// 3‘d4: begin
// bps_para <= BPS115200;
// bps_para_2 <= BPS115200_2;
// end
// default: ;
// endcase
//end

//always @ (posedge clk or negedge rst_n)
//begin
// if(!rst_n)
// cnt <= 13‘d0;
// else if(bps_start)
// if(cnt==bps_para)
// cnt <= 13‘d0;
// else
// cnt <= cnt + 1‘b1; //????????????????
// else
// cnt <= 13‘d0;
//end

always @ (posedge clk or negedge rst_n)
begin
if(!rst_n)
uart_ctrl <= 3‘d4; //?????????38400bps
else
case (uart_ctrl) //??????????
3‘d0: begin
bps_para <= BTF9600;
end
3‘d1: begin
bps_para <= BTF19200;
end
3‘d2: begin
bps_para <= BTF38400;
end
3‘d3: begin
bps_para <= BTF57600;
end
3‘d4: begin
bps_para <= BTF115200;
end
default: ;
endcase
end

always @ (posedge clk or negedge rst_n)
begin
if(!rst_n)
clk_count <= 32‘d0;
else if(bps_start)
if( clk_count >= CLK_100M )
begin
clk_count <= clk_count - CLK_100M + bps_para;
// clk_bps_r <= 1‘b0;
end
else if( clk_count >= CLK_100M / 2 )
begin
// clk_bps_r <= 1‘b1; // clk_bps_r???????????????????λ???м???????
clk_count <= clk_count + bps_para;
end
else
begin
clk_count <= clk_count + bps_para; //????????????????
// clk_bps_r <= 1‘b0;
end
else
clk_count <= 32‘d0;
end

always @ (posedge clk or negedge rst_n)
begin
if(!rst_n)
clk_bps_r <= 1‘b0;
else if(clk_count >= CLK_100M / 2 )// && bps_start)
clk_bps_r <= 1‘b1; // clk_bps_r???????????????????λ???м???????
else
clk_bps_r <= 1‘b0;
end

//always @ (posedge clk or negedge rst_n)
//begin
// if(!rst_n)
// cnt <= 13‘d0;
// else if((cnt==bps_para) || (~bps_start))
// cnt <= 13‘d0;
// 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)// && bps_start)
// clk_bps_r <= 1‘b1; // clk_bps_r???????????????????λ???м???????
// else
// clk_bps_r <= 1‘b0;
//end

assign clk_bps = clk_bps_r;

endmodule

*/

// 发数据 //

module my_uart_rx(clk,rst_n,rs232_rx,clk_bps,bps_start,wr_fifo_data,wr_fifo_req,en_1,clk_h);

input clk;
input rst_n;
input rs232_rx;
input clk_bps;

output en_1;
output reg clk_h;

output bps_start;
output [7:0] wr_fifo_data;
output wr_fifo_req;

reg[31:0]cnt1;
parameter SEC_TIME=100;
[email protected](posedge clk)
if(cnt1 == SEC_TIME)
begin
cnt1 <= 32‘b0;
clk_h = !clk_h;
end
else cnt1 <= cnt1 + 1‘b1;

reg clk_bps_reg0;
reg clk_bps_reg1;

always @ (posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
clk_bps_reg0 <= 1‘b0;
clk_bps_reg1 <= 1‘b0;
end
else
begin
clk_bps_reg0 <= clk_bps;
clk_bps_reg1 <= clk_bps_reg0;
end
end

reg rs232_rx_reg0,rs232_rx_reg1;
wire neg_rs232_rx;

always @ (posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
rs232_rx_reg0 <= 1‘b0;
rs232_rx_reg1 <= 1‘b0;
end
else
begin

//en_1<=bps_start;

rs232_rx_reg0 <= rs232_rx;
rs232_rx_reg1 <= rs232_rx_reg0;
end
end

assign neg_rs232_rx = rs232_rx_reg1 && ~rs232_rx_reg0;

reg bps_start;
reg wr_fifo_req;
reg rx_int;
reg [3:0] num;
reg rs232_start;

always @ (posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
//en_1<=1‘b0;

rx_int <= 1‘b0;
bps_start <= 1‘b0;
wr_fifo_req <= 1‘b0;
// en_1<=1‘b0;

rs232_start <= 1‘b1;
rs232_start <= 1‘b0;
end
else if( ( neg_rs232_rx == 1‘b1 ) && ( rs232_start == 1‘b0 ) ) //每发一次就进行一次
begin

rx_int <= 1‘b1;
bps_start <= 1‘b1;
wr_fifo_req <= 1‘b0;
end
else if(num==4‘d10)
begin
rs232_start <= 1‘b0;
rx_int <= 1‘b0;
bps_start <= 1‘b0;
wr_fifo_req <= 1‘b1;

en_1<=1‘b1;

end
else
begin
rx_int <= rx_int;
bps_start <= bps_start;
wr_fifo_req <= 1‘b0;

en_1<=1‘b0;

end
end

reg [7:0] rx_data_r;
reg [9:0] rx_temp_data;
reg [10:0] cnt;
reg en_1;

always @ (posedge clk or negedge rst_n)
begin
if(!rst_n)
begin

// en_1<=1‘b0;
rx_temp_data <= 8‘d0;
num <= 4‘d0;
rx_data_r <= 8‘d0;
end
else if(rx_int)
begin //???????????
if( ( clk_bps == 1‘b1 ) && ( clk_bps_reg0 == 1‘b0 ) )//检测他的上升沿
begin //?????????????,???????????????λ??8bit????????????λ
num <= num + 1‘b1;

case(num)
4‘d0: rx_temp_data[0] <= rs232_rx; //???λ
4‘d1: rx_temp_data[1] <= rs232_rx; //0 bit
4‘d2: rx_temp_data[2] <= rs232_rx; //1 bit
4‘d3: rx_temp_data[3] <= rs232_rx; //2 bit
4‘d4: rx_temp_data[4] <= rs232_rx; //3 bit
4‘d5: rx_temp_data[5] <= rs232_rx; //4 bit
4‘d6: rx_temp_data[6] <= rs232_rx; //5 bit
4‘d7: rx_temp_data[7] <= rs232_rx; //6 bit
4‘d8: rx_temp_data[8] <= rs232_rx; //7 bit
4‘d9: rx_temp_data[9] <= rs232_rx; //????λ
default:;
endcase
end
else if(num==4‘d10)
begin

// en_1<=1‘b1;

num <= 4‘d0;
//?????STOPλ??????,num????
rx_data_r <= rx_temp_data[8:1]; //?????????浽????????rx_data??
end
// else en_1<=1‘b0;

end
end

assign wr_fifo_data = rx_data_r;

endmodule

//  收数据//

module my_uart_tx(clk,rst_n,clk_bps,rd_fifo_data,rd_fifo_ena,rs232_tx,bps_start,rd_fifo_req,wr_fifo_data,en_1);

input clk;
input rst_n;
input clk_bps;
input [7:0] rd_fifo_data;
input rd_fifo_ena;
input [7:0] wr_fifo_data;
input en_1;

output rs232_tx;
output bps_start;
output rd_fifo_req;

reg clk_bps_reg0;
reg clk_bps_reg1;

always @ (posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
clk_bps_reg0 <= 1‘b0;
clk_bps_reg1 <= 1‘b0;
end
else
begin
clk_bps_reg0 <= clk_bps;
clk_bps_reg1 <= clk_bps_reg0;
end
end

reg rd_fifo_ena_reg0;
reg rd_fifo_ena_reg1;
wire neg_rd_fifo_ena;

always @ (posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
rd_fifo_ena_reg0 <= 1‘b0;
rd_fifo_ena_reg1 <= 1‘b0;
end
else
begin
rd_fifo_ena_reg0 <= rd_fifo_ena;
rd_fifo_ena_reg1 <= rd_fifo_ena_reg0;
end
end

assign neg_rd_fifo_ena = rd_fifo_ena_reg1 & ~rd_fifo_ena_reg0;

reg rd_fifo_req;
reg bps_start;
reg tx_en;
reg[3:0] num;
reg[7:0] tx_data;

reg [3:0] state;

always @ (posedge clk or negedge rst_n)
begin

if(!rst_n)
begin
bps_start <= 1‘b0;
tx_en <= 1‘b0;
tx_data <= 8‘d0;

rd_fifo_req <= 1‘b0;
state <= 4‘b0001;
end
else
case(state)

// 改动
4‘b0001 : //if(rd_fifo_ena_reg1==1‘b0)
if(en_1)
begin
state <= 4‘b0010;
rd_fifo_req <= 1‘b1;
bps_start <= 1‘b1;
tx_en <= 1‘b1;
end
else
begin
state <= 4‘b0001;
rd_fifo_req <= 1‘b0;
bps_start <= 1‘b0;
tx_en <= 1‘b0;
end

4‘b0010 : begin
rd_fifo_req <= 1‘b0;
tx_data <= wr_fifo_data;
state <= 4‘b1000;
end

4‘b1000 : if(num==4‘d10)
begin
bps_start <= 1‘b0;
tx_en <= 1‘b0;
state <= 4‘b0001;
end
else
begin
bps_start <= 1‘b1;
tx_en <= 1‘b1;
state <= 4‘b1000;
end
default : ;
endcase
end

reg rs232_tx_r;

always @ (posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
num <= 4‘d0;
rs232_tx_r <= 1‘b1;
end

else if(tx_en)
begin
if( (clk_bps_reg0 == 1‘b0) && ( clk_bps == 1‘b1 ) ) //clk_bps时钟沿由低位变成高位瞬间
begin
num <= num+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; //???????λ
//4‘d10:begin if(en_1==1) num<=0;else num<=10;end
default: rs232_tx_r <= 1‘b1;
endcase
end
else if(num==4‘d10) num <= 4‘d0;
//4‘d10: if(en_1==1) num<=0;else num<=10;end

end
end

assign rs232_tx = rs232_tx_r;

endmodule

时间: 2024-10-09 00:47:18

工作串口通信的相关文章

51单片机之串口通信(三)

51单片机之串口通信(三) 已有 47 次阅读2015-12-29 00:21 |个人分类:51单片机| 单片机, 通信 用串口实现发送和接收同时可操作: 电脑显示结果如图: 源程序: /*项目名称:串口发送接收实验项目内容:串口发送接收同时进行,计算机发送数据给单片机控制P0口,从而控制LED灯的亮灭,单片机发送数据到计算机,计算机显示出来时间:2015年12月23日项目负责人:YUAN*/#include <reg52.h>typedef unsigned char uChar8;type

嵌入式Linux裸机开发(七)——UART串口通信

嵌入式Linux裸机开发(七)--UART串口通信 一.UART串口通信简介 通用异步收发器简称UART,即UNIVERSAL ASYNCHRONOUS RECEIVER AND TRANSMITTER, 它用来传输串行数据.发送数据时, CPU 将并行数据写入UART,UAR按照一定的格式在一根电线上串 行发出:接收数据时, UART检测另一根电线的信号,将串行收集在缓冲区中, CPU 即可读取 UART 获得这些数据. 在 S5PV210中, UART提供了 4 对独立的异步串口I/O端口,

51单片机之串口通信(一)

一.基础知识 1.串行通信和并行通信:目前用的比较多的是串行通信.串行通信优点是连接简单,传输距离远:缺点是传输速度慢. 2.串行通信:分为同步通信和异步通信:异步通信是指发送和接收设备利用各自的时钟控制数据的发送和接收. 3.串行通信的传输方向:单工,半双工,全双工. 4.波特率:每秒钟传输2进制代码的位数,如1个字节为10位,每秒传输240个字节,则波特率为10*240=2400bps,单位是bps: 5.RS-232C:25个管脚,我们用到的是PGND(保护接地).TXD(发送数据),RX

SPCOMM控件在Delphi串口通信中的应用

SPCOMM控件在Delphi串口通信中的应用 2010-07-08 22:20:31|  分类: 个人日记 |举报 |字号 订阅 2009-03-01 05:35 摘要:利用Delphi开发工业控制系统软件成为越来越多的开发人员的选择,而串口通信是这个过程中必须解决的问题之一.本文在对几种常用串口通信方法分析比较的基础上,着重讨论了Delphi开发环境下利用Spcomm控件实现PC机与单片机之间串口通信的方法,研究了Spcomm串口通信的关键技术问题,并通过一个实例给出了Spcomm控件在De

51单片机GPIO口模拟串口通信

51单片机GPIO口模拟串口通信 标签: bytetimer终端存储 2011-08-03 11:06 6387人阅读 评论(2) 收藏 举报 本文章已收录于: 分类: 深入C语言(20) 作者同类文章X 1 #include "reg52.h" 2 #include "intrins.h" 3 #include "math.h" 4 #include "stdio.h" 5 sbit BT_SND =P1^5; 6 sbit

奶爸业余单片机学习之:UART串口通信学习笔记(二)

/************************************** 串口通信实验* 晶振11.0592MHz* 波特率9600bps* 中断方式实现:单片机接收电脑数据,加1后发送回电脑***************************************/ #include<reg52.h> unsigned char dat; /**********串口通信配置*******************/void UART_CONFIG(unsigned long baud

labview与单片机串口通信

VISA是虚拟仪器软件体系结构的缩写(即Virtual Instruments Software Architecture),实质上是一个I/O口软件库及其规范的总称. VISA是应用于仪器编程的标准I/0应用程序接口,是工业界通用的仪器驱动器标准API(应用程序接口),采用面向对象编程,具有很好的兼容性.扩展性和独立性.用户可用一个API控制包括VXI.GPIB及串口仪器在内的不同种类的仪器.它还支持多平台工作.多接口控制,是一个多类型的函数库. 在LabVIEW中编写的VISA接口程序,当外

Win10 IoT C#开发 3 - UART 串口通信

Windows 10 IoT Core 是微软针对物联网上市场的一个重要产品,既可以开发设备UI与用户交互式操作,又可以控制GPIO等接口,使得原来嵌入式繁琐的开发变得简单.通过Remote Debug功能可以进行断点追踪调试.C#语言本身也有很好的用户基础,相信Win10 IoT 不远的将来会火起来.上个月帮朋友解决了关于Win10 IoT 的一些技术问题,当前也有很多公司在尝试采用Win10 IoT进行开发,可能也会遇到这些问题,相关文档现在还非常少,这里写出来供大家参考.因为要做一个Jav

Qt实现串口通信总结

注意: Qt5发布之前,Qt实现串口通信一般是采用第三方类库qextserialport.Qt5发布后自带了QtSerialPort 能够支持串口通信. 1.Qextserialport类介绍 在Qt5之前的版本中并没有特定的串口控制类,现在大部分人使用的是第三方写的qextserialport类,本文章主要是讲解怎样利用此类实现串口通信. 2.文件下载地址: http://sourceforge.net/projects/qextserialport/files/ 3.文件内容:     3.