UART串口通信实验报告材料.docx

上传人:b****6 文档编号:16719554 上传时间:2023-07-16 格式:DOCX 页数:16 大小:28.73KB
下载 相关 举报
UART串口通信实验报告材料.docx_第1页
第1页 / 共16页
UART串口通信实验报告材料.docx_第2页
第2页 / 共16页
UART串口通信实验报告材料.docx_第3页
第3页 / 共16页
UART串口通信实验报告材料.docx_第4页
第4页 / 共16页
UART串口通信实验报告材料.docx_第5页
第5页 / 共16页
UART串口通信实验报告材料.docx_第6页
第6页 / 共16页
UART串口通信实验报告材料.docx_第7页
第7页 / 共16页
UART串口通信实验报告材料.docx_第8页
第8页 / 共16页
UART串口通信实验报告材料.docx_第9页
第9页 / 共16页
UART串口通信实验报告材料.docx_第10页
第10页 / 共16页
UART串口通信实验报告材料.docx_第11页
第11页 / 共16页
UART串口通信实验报告材料.docx_第12页
第12页 / 共16页
UART串口通信实验报告材料.docx_第13页
第13页 / 共16页
UART串口通信实验报告材料.docx_第14页
第14页 / 共16页
UART串口通信实验报告材料.docx_第15页
第15页 / 共16页
UART串口通信实验报告材料.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

UART串口通信实验报告材料.docx

《UART串口通信实验报告材料.docx》由会员分享,可在线阅读,更多相关《UART串口通信实验报告材料.docx(16页珍藏版)》请在冰点文库上搜索。

UART串口通信实验报告材料.docx

UART串口通信实验报告材料

实验四UART串口通信

学院:

研究生院学号:

1400030034姓名:

张秋明

一、实验目的及要求

设计一个UART串口通信协议,实现“串<-->并”转换功能的电路,也就是“通用异步收发器”。

二、实验原理

UART是一种通用串行数据总线,用于异步通信。

该总线双向通信,可以实现全双工传输和接收。

在嵌入式设计中,UART用来主机与辅助设备通信,如汽车音响与外接AP之间的通信,与PC机通信包括与监控调试器和其它器件,如EEPROM通信。

UART作为异步串口通信协议的一种,工作原理是将传输数据的每个字符一位接一位地传输。

其中各位的意义如下:

起始位:

先发出一个逻辑”0”的信号,表示传输字符的开始。

资料位:

紧接着起始位之后。

资料位的个数可以是4、5、6、7、8等,构成一个字符。

通常采用ASCII码。

从最低位开始传送,靠时钟定位。

奇偶校验位:

资料位加上这一位后,使得“1”的位数应为偶数(偶校验)或奇数(奇校验),以此来校验资料传送的正确性。

停止位:

它是一个字符数据的结束标志。

可以是1位、1.5位、2位的高电平。

由于数据是在传输线上定时的,并且每一个设备有其自己的时钟,很可能在通信中两台设备间出现了小小的不同步。

因此停止位不仅仅是表示传输的结束,并且提供计算机校正时钟同步的机会。

适用于停止位的位数越多,不同时钟同步的容忍程度越大,但是数据传输率同时也越慢。

 

空闲位:

处于逻辑“1”状态,表示当前线路上没有资料传送。

波特率:

是衡量资料传送速率的指标。

表示每秒钟传送的符号数(symbol)。

一个符号代表的信息量(比特数)与符号的阶数有关。

例如资料传送速率为120字符/秒,传输使用256阶符号,每个符号代表8bit,则波特率就是120baud,比特率是120*8=960bit/s。

这两者的概念很容易搞错。

三、实现程序

libraryieee;

useieee.std_logic_1164.all;

useieee.std_logic_arith.all;

useieee.std_logic_unsigned.all;

entityuartis

port(clk:

instd_logic;--系统时钟

rst_n:

instd_logic;--复位信号

rs232_rx:

instd_logic;--RS232接收数据信号;

rs232_tx:

outstd_logic--RS232发送数据信号;);

enduart;

architecturebehavofuartis

componentuart_rxport(clk:

instd_logic;--系统时钟

rst_n:

instd_logic;--复位信号

rs232_rx:

instd_logic;--RS232接收数据信号

clk_bps:

instd_logic;--此时clk_bps的高电平为接收数据的采样点

bps_start:

outstd_logic;--接收到数据后,波特率时钟启动置位

rx_data:

outstd_logic_vector(7downto0);--接收数据寄存器,保存直至下一个数据来到

rx_int:

outstd_logic--接收数据中断信号,接收数据期间时钟为高电平,传送给串口发送);

endcomponent;

componentspeed_selectport(clk:

instd_logic;--系统时钟

rst_n:

instd_logic;--复位信号

clk_bps:

outstd_logic;--此时clk_bps的高电平为接收或者发送数据位的中间采样点

bps_start:

instd_logic--接收数据后,波特率时钟启动信号置位);

endcomponent;

componentuart_txport(clk:

instd_logic;--系统时钟

rst_n:

instd_logic;--复位信号

rs232_tx:

outstd_logic;--RS232接收数据信号

clk_bps:

instd_logic;--此时clk_bps的高电平为接收数据的采样点

bps_start:

outstd_logic;--接收到数据后,波特率时钟启动置位

rx_data:

instd_logic_vector(7downto0);--接收数据寄存器,保存直至下一个数据来到

rx_int:

instd_logic--接收数据中断信号,接收数据期间时钟为高电平,传送给串口发送模块,使得串口正在进行接收数据的时候,发送模块不工作,避免了一个完整的数据(1位起始位、8位数据位、1位停止位)还没有接收完全时,发送模块就已经将不正确的数据传输出去);

endcomponent;

signalbps_start_1:

std_logic;

signalbps_start_2:

std_logic;

signalclk_bps_1:

std_logic;

signalclk_bps_2:

std_logic;

signalrx_data:

std_logic_vector(7downto0);

signalrx_int:

std_logic;

begin

RX_TOP:

uart_rxportmap(clk=>clk,

rst_n=>rst_n,

rs232_rx=>rs232_rx,

clk_bps=>clk_bps_1,

bps_start=>bps_start_1,

rx_data=>rx_data,

rx_int=>rx_int

);

SPEED_TOP_RX:

speed_selectportmap(clk=>clk,

rst_n=>rst_n,

clk_bps=>clk_bps_1,

bps_start=>bps_start_1

);

TX_TOP:

uart_txportmap(clk=>clk,--系统时钟

rst_n=>rst_n,--复位信号

rs232_tx=>rs232_tx,--RS232发送数据信号

clk_bps=>clk_bps_2,--此时clk_bps的高电平为发送数据的采样点

bps_start=>bps_start_2,--接收到数据后,波特率时钟启动置位

rx_data=>rx_data,--接收数据寄存器,保存直至下一个数据来到

rx_int=>rx_int--接收数据中断信号,接收数据期间时钟为高电平,传送给串口发送模块,使得串口正在进行接收数据的时候,发送模块不工作,避免了一个完整的数据(1位起始位、8位数据位、1位停止位)还没有接收完全时,发送模块就已经将不正确的数据传输出去);

SPEED_TOP_TX:

speed_selectportmap(clk=>clk,

rst_n=>rst_n,

clk_bps=>clk_bps_2,

bps_start=>bps_start_2

);

endbehav;

 

--------------------------------------------------------------------------------------

---------------------------------3个子模块---------------------------------------------

 

---------------------------------异步接收模块-------------------------------------------

libraryieee;

useieee.std_logic_1164.all;

useieee.std_logic_unsigned.all;

entityuart_rxis

port(clk:

instd_logic;--系统时钟

rst_n:

instd_logic;--复位信号

rs232_rx:

instd_logic;--RS232接收数据信号

clk_bps:

instd_logic;--此时clk_bps的高电平为接收数据的采样点

bps_start:

outstd_logic;--接收到数据后,波特率时钟启动置位

rx_data:

outstd_logic_vector(7downto0);--接收数据寄存器,保存直至下一个数据来到

rx_int:

outstd_logic--接收数据中断信号,接收数据期间时钟为高电平,传送给串口发送模块,使得串口正在进行接收数据的时候,发送模块不工作,避免了一个完整的数据(1位起始位、8位数据位、1位停止位)还没有接收完全时,发送模块就已经将不正确的数据传输出去);

enduart_rx;

architecturebehavofuart_rxis

signalrs232_rx0:

std_logic;

signalrs232_rx1:

std_logic;

signalrs232_rx2:

std_logic;

signalrs232_rx3:

std_logic;

signalneg_rs232_rx:

std_logic;

signalbps_start_r:

std_logic;

signalnum:

integer;

signalrx_data_r:

std_logic_vector(7downto0);--串口接收数据寄存器,保存直至下一个数据到来

begin

process(clk,rst_n)

begin

if(rst_n='0')then

rs232_rx0<='0';

rs232_rx1<='0';

rs232_rx2<='0';

rs232_rx3<='0';

else

if(rising_edge(clk))then

rs232_rx0<=rs232_rx;

rs232_rx1<=rs232_rx0;

rs232_rx2<=rs232_rx1;

rs232_rx3<=rs232_rx2;

endif;

endif;

neg_rs232_rx<=rs232_rx3andrs232_rx2andnot(rs232_rx1)andnot(rs232_rx0);

endprocess;

process(clk,rst_n)

begin

if(rst_n='0')then

bps_start_r<='0';

rx_int<='0';

else

if(rising_edge(clk))then

if(neg_rs232_rx='1')then--接收到串口数据线rs232_rx的下降沿标志信号

bps_start_r<='1';--启动串口准备数据接收

rx_int<='1';--接收数据中断信号使能

elseif((num=15)and(clk_bps='1'))then--接收完有用数据信息

bps_start_r<='0';--数据接收完毕,释放波特率启动信号

rx_int<='0';--接收数据中断信号关闭

endif;

endif;

endif;

endif;

bps_start<=bps_start_r;

endprocess;

process(clk,rst_n)

begin

if(rst_n='0')then

rx_data_r<="00000000";

rx_data<="00000000";

num<=0;

else

if(rising_edge(clk))then

if(clk_bps='1')then

num<=num+1;

casenumis

when1=>rx_data_r(0)<=rs232_rx;--锁存第0bit

when2=>rx_data_r

(1)<=rs232_rx;--锁存第0bit

when3=>rx_data_r

(2)<=rs232_rx;--锁存第0bit

when4=>rx_data_r(3)<=rs232_rx;--锁存第0bit

when5=>rx_data_r(4)<=rs232_rx;--锁存第0bit

when6=>rx_data_r(5)<=rs232_rx;--锁存第0bit

when7=>rx_data_r(6)<=rs232_rx;--锁存第0bit

when8=>rx_data_r(7)<=rs232_rx;--锁存第0bit

when10=>rx_data<=rx_data_r;

when11=>num<=15;

whenothers=>null;

endcase;

if(num=15)then

num<=0;

endif;

endif;

endif;

endif;

endprocess;

endbehav;

---------------------------------波特率控制模块-----------------------------------------

libraryieee;

useieee.std_logic_1164.all;

useieee.std_logic_arith.all;

useieee.std_logic_unsigned.all;

entityspeed_selectis

port(clk:

instd_logic;--系统时钟

rst_n:

instd_logic;--复位信号

clk_bps:

outstd_logic;--此时clk_bps的高电平为接收或者发送数据位的中间采样点

bps_start:

instd_logic--接收数据后,波特率时钟启动信号置位或者开始发送数据时,波特率时钟启动信号置位);

endspeed_select;

architecturebehavofspeed_selectis

signalcnt:

std_logic_vector(12downto0);

signalclk_bps_r:

std_logic;

constantBPS_PARA:

integer:

=5207;

constantBPS_PARA_2:

integer:

=2603;

begin

process(clk,rst_n)

begin

if(rst_n='0')then

cnt<="0000000000000";

else

if(rising_edge(clk))then

if((cnt=BPS_PARA)or(bps_start='0'))then

cnt<="0000000000000";--波特率计数器清零

else

cnt<=cnt+'1';--波特率时钟计数启动

endif;

endif;

endif;

endprocess;

process(clk,rst_n)

begin

if(rst_n='0')then

clk_bps_r<='0';

else

if(rising_edge(clk))then

if(cnt=BPS_PARA_2)then

clk_bps_r<='1';--clk_bps_r高电平为接收数据位的中间采样点,同时也作为发送数据的数据改变点

else

clk_bps_r<='0';--波特率计数器清零

endif;

endif;

endif;

clk_bps<=clk_bps_r;

endprocess;

endbehav;

 

---------------------------------异步发送模块-------------------------------------------

libraryieee;

useieee.std_logic_1164.all;

useieee.std_logic_unsigned.all;

entityuart_txis

port(clk:

instd_logic;--系统时钟

rst_n:

instd_logic;--复位信号

rs232_tx:

outstd_logic;--RS232接收数据信号

clk_bps:

instd_logic;--此时clk_bps的高电平为接收数据的采样点

bps_start:

outstd_logic;--接收到数据后,波特率时钟启动置位

rx_data:

instd_logic_vector(7downto0);--接收数据寄存器,保存直至下一个数据来到

rx_int:

instd_logic--接收数据中断信号,接收数据期间时钟为高电平,传送给串口发送模块,使得串口正在进行接收数据的时候,发送模块不工作,避免了一个完整的数据(1位起始位、8位数据位、1位停止位)还没有接收完全时,发送模块就已经将不正确的数据传输出去);

enduart_tx;

architecturebehavofuart_txis

signalrx_int0:

std_logic;

signalrx_int1:

std_logic;

signalrx_int2:

std_logic;

signalneg_rx_int:

std_logic;

signalbps_start_r:

std_logic;

signalnum:

integer;

signaltx_data:

std_logic_vector(7downto0);--串口接收数据寄存器,保存直至下一个数据到来

begin

process(clk,rst_n)

begin

if(rst_n='0')then

rx_int0<='0';

rx_int1<='0';

rx_int2<='0';

else

if(rising_edge(clk))then

rx_int0<=rx_int;

rx_int1<=rx_int0;

rx_int2<=rx_int1;

endif;

endif;

neg_rx_int<=not(rx_int1)and(rx_int2);

endprocess;

process(clk,rst_n)

begin

if(rst_n='0')then

bps_start_r<='0';

tx_data<="00000000";

else

if(rising_edge(clk))then

if(neg_rx_int='1')then--接收到串口数据线rs232_rx的下降沿标志信号

bps_start_r<='1';--启动串口准备数据接收

tx_data<=rx_data;--接收数据中断信号使能

elseif((num=15)and(clk_bps='1'))then--接收完有用数据信息

bps_start_r<='0';--数据接收完毕,释放波特率启动信号

endif;

endif;

endif;

endif;

bps_start<=bps_start_r;

endprocess;

process(clk,rst_n)

begin

if(rst_n='0')then

rs232_tx<='1';

num<=0;

else

if(rising_edge(clk))then

if(clk_bps='1')then

num<=num+1;

casenumis

when1=>rs232_tx<='0';

when2=>rs232_tx<=tx_data(0);--发送第1bit

when3=>rs232_tx<=tx_data

(1);--发送第2bit

when4=>rs232_tx<=tx_data

(2);--发送第3bit

when5=>rs232_tx<=tx_data(3);--发送第4bit

when6=>rs232_tx<=tx_data(4);--发送第5bit

when7=>rs232_tx<=tx_data(5);--发送第6bit

when8=>rs232_tx<=tx_data(6);--发送第7bit

when9=>rs232_tx<=tx_data(7);--发送第8bit

when10=>rs232_tx<='1';

when11=>num<=15;

whenothers=>null;

endc

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2