VHDL串口通信.doc_第1頁
VHDL串口通信.doc_第2頁
VHDL串口通信.doc_第3頁
VHDL串口通信.doc_第4頁
VHDL串口通信.doc_第5頁
已閱讀5頁,還剩14頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

4 .UART 模塊設(shè)計(jì)UART 異步通信串口協(xié)議的VHDL實(shí)現(xiàn)包括3個基本模塊:時(shí)鐘分頻、接收模塊和發(fā)送模塊,下面逐一介紹其實(shí)現(xiàn)方法。4.2.1 時(shí)鐘分頻模塊由于UART是異步傳輸,沒有傳輸同步時(shí)鐘。為了能保證數(shù)據(jù)傳輸?shù)恼_性,UART采用16倍數(shù)據(jù)波特率的時(shí)鐘進(jìn)行采樣。每個數(shù)據(jù)有16個時(shí)鐘采樣,取中間的采樣值,以保證采樣不會滑碼或誤碼。一般UART一幀的數(shù)據(jù)位數(shù)為8,這樣即使每個數(shù)據(jù)有一個時(shí)鐘的誤差,接收端也能正確地采樣到數(shù)據(jù)。這里采用常用的數(shù)據(jù)波特率為9600bps,則所需時(shí)鐘的頻率為16*9600。系統(tǒng)時(shí)鐘為50MHz,則分頻系數(shù)為50000000/(16*9600) =325.52,取整為325。分頻器實(shí)現(xiàn)相對簡單,這里對其設(shè)計(jì)流程圖不做詳細(xì)介紹。只是將設(shè)計(jì)過程和結(jié)果簡述如下:首先用VHDL語言進(jìn)行設(shè)計(jì)輸入,并生成模塊文件如圖4.3所示,其中clk為50M系統(tǒng)時(shí)鐘輸入,clkout為325分頻后時(shí)鐘輸出。 圖4.3 分頻模塊 然后建立波形文件,對以上模塊進(jìn)行時(shí)序仿真,仿真結(jié)果如圖4.4所示,方正結(jié)果說明,分頻輸出實(shí)現(xiàn)了對輸入的325分頻,分頻模塊設(shè)計(jì)正確。 圖4.4 分頻模塊仿真結(jié)果4.2.2 UART發(fā)送模塊 發(fā)送過程:空閑狀態(tài),線路處于高電平;當(dāng)受到發(fā)送數(shù)據(jù)指令后,拉低線路一個數(shù)據(jù)位的時(shí)間T,接著數(shù)據(jù)按地位到高位依次發(fā)送,數(shù)據(jù)發(fā)送完畢后,接著發(fā)送停止位(停止位為高電平),一幀數(shù)據(jù)發(fā)送結(jié)束。(1) 模塊流程圖 根據(jù)以上發(fā)送過程,發(fā)送模塊算法示意圖設(shè)計(jì)如圖4.5所示。開始wrsig=1Nidle=0YN發(fā)送數(shù)據(jù)Y停止位N結(jié)束Y 圖4.5 UART發(fā)送數(shù)據(jù)算法示意圖(2)生成模塊文件 新建一原理圖文件,將VHDL源文件生成對應(yīng)的模塊文件如圖4.6所示,其中clk為時(shí)鐘輸入,datain為需要發(fā)送的數(shù)據(jù)輸入,wrsig為發(fā)送命令輸入,idle為忙閑信號輸出,tx為串行數(shù)據(jù)輸出端。 圖4.6 UART發(fā)送模塊(3) 波形仿真 要對發(fā)送模塊進(jìn)行時(shí)序仿真必須設(shè)計(jì)一測試模塊,即在每一個clk來時(shí)產(chǎn)生一個八位的數(shù)據(jù)。測試模塊代碼如下:library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_arith.all;use ieee.std_logic_unsigned.all;entity testuart is port(clk:in std_logic; dataout: out std_logic_vector(7 downto 0); wrsig:out std_logic );end testuart;architecture one of testuart is signal dataoutreg: std_logic_vector(7 downto 0); signal cnt :std_logic_vector(7 downto 0);begin dataout=dataoutreg; process(clk) begin if(clkevent and clk=1) then if(cnt=11111110)then dataoutreg=dataoutreg+00000001; wrsig=1; cnt=00000000; else wrsig=0; cnt=cnt+00000001; end if; end if; end process;end one;保存文件為testuart.hdl,單擊Files Create/Update Create Symbol Files for Current File命令,為testuart.hdl生成原理圖模塊。新建一個原理圖文件,各個模塊的連接如圖4.7所示。在原理圖的適當(dāng)位置放置testuart模塊和uarttx模塊,并添加輸入輸出端口。圖4.7 UART發(fā)送模塊仿真原理圖 保存原理圖為uarttxts.bdf 。編譯工程文件,編譯無誤后新建波形仿真文件,加入輸入輸出信號,設(shè)置系統(tǒng)時(shí)鐘信號clk的周期為20ns,保存波形文件為uarttxts.vwf,進(jìn)行UART數(shù)據(jù)發(fā)送的波形仿真,波形仿真報(bào)告如圖4.8所示 圖4.8 UART發(fā)送模塊仿真結(jié)果 對上圖分析看出,當(dāng)發(fā)送命令wrsig的上升沿有效時(shí),啟動發(fā)送數(shù)據(jù)。串行數(shù)據(jù)的波形與發(fā)送數(shù)據(jù)dataout相一致,UART的發(fā)送模塊得到正確驗(yàn)證。4.2.3 UART接收模塊 UART接收模塊的功能:時(shí)時(shí)檢測線路,當(dāng)線路產(chǎn)生下降沿時(shí),即認(rèn)為線路有數(shù)據(jù)傳輸,啟動接收數(shù)據(jù)進(jìn)程進(jìn)行接收,按從低位到高位接收數(shù)據(jù)。(1) 模塊流程圖 根據(jù)以上描述的接收模塊的功能,可將接收模塊算法示意圖設(shè)計(jì)如圖4.9所示。開始rx=0Nidle=0YN接收數(shù)據(jù)Y停止位N結(jié)束Y 圖4.9 接收模塊算法示意圖(2) 生成模塊文件 新建一原理圖文件,將VHDL源文件生成對應(yīng)的模塊文件如圖4.10所示,其中clk為時(shí)鐘輸入,rx為需要串行數(shù)據(jù)輸入,dataout為并行輸出,rdsig為忙閑信號輸出。 圖4.10 UART接收模塊(3)波形仿真 新建一個原理圖文件,加入各功能模塊,并添加輸入輸出端口,各個模塊的連接如圖4.11所示。圖4.11 UART接收模塊仿真原理圖 保存原理圖為uartrxts.bdf。編譯工程文件,編譯無誤后新建波形仿真文件,加入輸入輸出信號,設(shè)置系統(tǒng)時(shí)鐘clk為50MHz,保存為uartrxts.vwf,進(jìn)行UART數(shù)據(jù)接收的波形仿真,波形仿真報(bào)告如圖4.12所示。 圖4.12 UART接收模塊仿真結(jié)果對上圖分析看出,UART接收模塊接收到的數(shù)據(jù)與UART發(fā)送模塊發(fā)送的數(shù)據(jù)相一至,每接收到一個數(shù)據(jù)都有一個讀取數(shù)據(jù)指示rdisg,UART接收模塊得到正確驗(yàn)證。4.2.3 硬件測試 按照下圖連接個模塊,在PC機(jī)上安裝一個串口調(diào)試工具,通過串口調(diào)試工具向FPGA發(fā)送一個數(shù)據(jù),看在PC機(jī)上能否接受到發(fā)送的數(shù)據(jù)。 測試結(jié)果如下附錄:程序代碼UART 分頻模塊代碼:library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_arith.all;use ieee.std_logic_unsigned.all;entity clkdiv is port( clk:in std_logic; clkout: out std_logic );end clkdiv;architecture one of clkdiv is signal cnt:std_logic_vector(15 downto 0);begin process(clk) begin if(clkevent and clk=1)then if(cnt=0000000010100010)then clkout=1; cnt=cnt+0000000000000001; elsif(cnt=0000000101000100)then clkout=0; cnt=0000000000000000; else cnt=cnt+0000000000000001; end if; end if; end process;end one;UART發(fā)送模塊代碼:library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_arith.all;use ieee.std_logic_unsigned.all;entity uarttx is port( clk : in std_logic; datain: in std_logic_vector(7 downto 0); wrsig: in std_logic; idle: out std_logic; tx: out std_logic );end uarttx;architecture one of uarttx is signal send:std_logic; signal wrsigbuf:std_logic; signal idlereg:std_logic; signal wrsigrise:std_logic; signal cnt:std_logic_vector(7 downto 0);begin idle=idlereg;- process(clk) begin if(clkevent and clk=1)then wrsigbuf=wrsig; wrsigrise=(not wrsigbuf) and wrsig; end if; end process;- process(clk) begin if(clkevent and clk=1)then if(wrsigrise=1 and (not idlereg=1)then send=1; elsif(cnt=10100000) then send tx=0; idlereg=1; cnt tx=datain(0); idlereg=1; cnt tx=datain(1); idlereg=1; cnt tx=datain(2); idlereg=1; cnt tx=datain(3); idlereg=1; cnt tx=datain(4); idlereg=1; cnt tx=datain(5); idlereg=1; cnt tx=datain(6); idlereg=1; cnt tx=datain(7); idlereg=1; cnt tx=1; idlereg=1; cnt tx=1; idlereg=0; cnt cnt=cnt+00000001; end case; else tx=1; cnt=00000000; idlereg=0; end if; end if; end process; end one;UART 接收模塊代碼:library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_arith.all;use ieee.std_logic_unsigned.all;entity uartrx is port(clk:in std_logic; rx: in std_logic; dataout:out std_logic_vector(7 downto 0); rdsig: out std_logic );end uartrx;architecture one of uartrx is signal dataoutreg:std_logic_vector(7 downto 0); signal rdsigreg: std_logic; signal cnt:std_logic_vector(7 downto 0); signal rxbuf:std_logic; signal rxfall:std_logic; signal receive:std_logic; signal idle:std_logic;begin- process(clk) begin if(clkevent and clk=1)then rxbuf=rx; if (rxbuf=1 and rx=0)then rxfall=1; else rxfall=0; end if; end if; end process;- process(clk) begin if(clkevent and clk=1)then if(rxfall=1 and idle=0 ) then receive=1; elsif (cnt=10011000) then receive idle=1; cnt=cnt+00000001; rdsig idle=1; dataout(0)=rx; cnt=cnt+00000001; rdsig idle=1; dataout(1)=rx; cnt=cnt+00000001; rdsig idle=1; dataout(2)=rx; cnt=cnt+00000001; rdsig idle=1; dataout(3)=rx; cnt=cnt+00000001; rdsig idle=1; dataout(4)=rx; cnt=cnt+00000001; rdsig idle=1; dataout(5)=rx; cnt=cnt+00000001; rdsig idle=1; dataout(6)=rx; cnt=cnt+00000001; rdsig idle=1; dataout(7)=rx; cnt=cnt+00000001; rdsig -idle=1; -cnt=cnt+00000001; -rdsig cnt=cnt+00000001; end case; else cnt=00000000; idle=0; rdsig=0; end if; end if; end process;-end one; UART 測試模塊代碼:library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_arith.all;use ieee.std_logic_unsigned.all;entity testuart is port(clk:in std_logic; dataout: out std_logic_vector(7 downto 0); wrsig:out std_logic );end testuart;architecture one of testuart is signal dataoutreg: std_logic_vector

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論