


版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、第三章UART設計3.1 UART的幀格式在UART中,數(shù)據(jù)位是以字符為傳送單位,數(shù)據(jù)的前、后要有起始位、停止 位,另外可以在停止位的前面加上一個比特(bit)的校驗位。其幀格式如圖所示圖3_1數(shù)據(jù)幀格式文章通 過分析UART的功能,利用有限狀態(tài)機來描述UART核心控制邏輯 的方法,將其核心功能集成,從而使整個設計更加穩(wěn)定、可靠。基本的UART通信只需要兩條信號線就可以完成數(shù)據(jù)的相互通信。 UART的功能模塊如圖3_2 所示。圖3_2UART的功能模塊圖3.2 UART 模塊在大規(guī)模電路的設計中,廣泛采用層次化,結構化的設計方法。它將一個完 整的硬件設計任務從系統(tǒng)級開始,劃分為若干個可操作的模
2、塊,編制出相應的模 型并進行仿真驗證,最后在系統(tǒng)級上進行組合。這樣在提高設計效率的同時又提 高了設計質(zhì)量,是目前復雜數(shù)字系統(tǒng)實現(xiàn)的主要手段,也是本文設計思想的基礎 其系統(tǒng)模塊可劃分為4個部分,如波特發(fā)生器,控制器,接收器,發(fā)送器,如圖3-3所示:圖3-3uart結構圖主要引腳功能介紹Read串行輸入Datan并行輸入Cs通知cpu接收數(shù)據(jù)位Reset重啟輸入Clk:48M時鐘輸入sen d:串行輸出data_out并行輸出ks:通知cpu發(fā)送準備位state:uart狀態(tài)輸入主體程序timescale 1ns/1nsmodule gs_opt(in putwireread,in putwire
3、clk,in putwirereset,in putwirestate,in putwire7:0 dat_in,output wire send,output wire cs,output wire ks,output wire 7:0 dat_out);wiresend_en able;wireread_e nable;wire clk_e nable3;wire clk_e nable4;wire clear3 ;wire clear4 ;wire clk_e nable;wire 7:0 coun ters;wire clear ;wiret1;/*read,se nd,cs,ks,r
4、eset,state,clk,dat_i n,dat_out);/module uart(read,se nd,cs,ks,reset,state,clk,dat_i n,dat_out); in put read,clk,reset,state;/read為串行輸入,clk為時鐘輸入50MHZ , reset為重啟鍵 input7:0 dat_in;并行數(shù)據(jù)輸入output sen d,cs,ks;/send為串行輸出,cs為通知cpu接收數(shù)據(jù)位,ks為發(fā)送準備位 output7:0 dat_out;/ 并行數(shù)據(jù)輸出wire clear,clk_e nable,read_e nable,cl
5、ear3,se nd_en able,clear4,t1; wire7:0 coun ters,dat_i n;*/rxd u1 (.dat_out (dat_out) ,.cs (cs),.read (read),.reset (reset) ,.clk_e nable3 (clk_e nable3),.clk (clk) ,.read_enable (read_enable),.clear3 (clear3),.co un ters(co un ters);/接收數(shù)據(jù)moduletxd u2 (.dat_in (dat_i n),.ks (ks),.se nd (se nd),.reset
6、 (reset),.clk_e nable4 (clk_e nable4),.clk (clk),.se nd_en able (se nd_en able),.clear4 (clear4),.co un ters( coun ters);發(fā)送數(shù)據(jù)moduleclk_bau u3 (.clk(clk),t1 (t1),.clk_e nable (clk_e nable) );時鐘計數(shù)器模塊ctrl u4(.read_e nable (read_e nable), .se nd_en able (se nd_en able), .clk (clk),.state (state),.t1 (t1
7、),.read (read ),.co un ters (cou nters),.reset (reset ),.clear (clear);check_cle u5 (.state (state),.clear3 (clear3),.clear4 (clear4),.clear (clear),.clk_e nable3 (clk_e nable3),.clk_e nable4 (clk_e nable4), .clk_e nable (clk_e nable) );en dmodule/ /3.3UART發(fā)送模塊的數(shù)據(jù)發(fā)送服務發(fā)送器實現(xiàn)的功能是將輸入的8位并行數(shù)據(jù)變?yōu)榇袛?shù)據(jù),同時在數(shù)據(jù)頭
8、部加 起始位,在數(shù)據(jù)位尾部加奇偶校驗位和停止位。數(shù)據(jù)發(fā)送服務如表3.1計數(shù)器018910操作發(fā)送低電平發(fā)送數(shù)據(jù)和奇偶校驗發(fā)送奇偶校驗結果發(fā)送高電平表3.1數(shù)據(jù)發(fā)送其基本特點是: 在信號線上共有兩種狀態(tài),可分別用邏輯 1和邏輯。來區(qū)分。在發(fā)送器空 閑時,數(shù)據(jù)線應該保持在邏輯高電平狀態(tài)。 發(fā)送 起 始 位:該位是一個邏輯0,總是加在每一幀的頭部,提示接受器數(shù)據(jù) 傳輸即將開始,在接收數(shù)據(jù)位過程中又被分離出去,占據(jù)一個數(shù)據(jù)位的時間。 發(fā)送數(shù)據(jù)位:在起始位之后就是數(shù)據(jù)位,一般為8位一個字節(jié)的數(shù)據(jù),低位 在前,高位在后。如字母 C在ASCII表中是十進制67,二進制01000011,那么 傳輸?shù)膶⑹?10
9、000100=并在數(shù)據(jù)發(fā)送過程當中,進行數(shù)據(jù)位奇偶校驗。 發(fā)送校 驗 位:該位一般用來判斷接收的數(shù)據(jù)位有無錯誤, 常用的校驗方法是 奇偶校驗法。將3過程當中奇偶校驗的結果輸入到數(shù)據(jù)線,并占一個數(shù)據(jù)位時鐘。 停止位:停止位總在每一幀的末尾,為邏輯1,用于標志一個字符傳送的結束, 占據(jù)一個數(shù)據(jù)位的時間。 幀:從起始位到停止位之間的一組數(shù)據(jù)稱為一幀。的數(shù)據(jù)發(fā)送操作如圖3-4圖3-4數(shù)據(jù)發(fā)送操作解釋:采用9600波特率發(fā)送從cpu傳入數(shù)據(jù):是指將data_in端口的數(shù)據(jù)存入寄存器中Cpu發(fā)送為檢測:是指將ks寄存器置位,即數(shù)據(jù)發(fā)送完畢的數(shù)據(jù)發(fā)送模塊程序module rxd(dat_out,cs,rea
10、d,reset,clk_e nable3,clk,read_e nable,clear3,co un ters);接收數(shù)據(jù)modulein put read_e nable;in put read,reset,clk;/read 為串行輸入,read_co ntrol 為時鐘控制,reset 為 重啟鍵in put7:0 coun ters;output cs,clear3,clk_enable3;/cs為通知 cpu 讀取數(shù)據(jù)位output7:0 dat_out;/wire clear3;reg cs,cs1,clk_e nable3;reg7:0 data_out;移位寄存器reg par
11、ity_check_result,parity_result,clear3,clear1;/ /always(posedge clk)beg inif(read_enable)/當read_enable為高電平時為發(fā)送操作狀態(tài)beg inclk_e nable3=1;clear3=clear1;endelsebeg inclear3=1;endend/always( negedge coun tersO)/ 接收操作if(read_e nable & !reset)beg inif (cou nters=8b00011000) 1beg indata_out7v=read; parity_ch
12、eck_result=parity_check_result + read; endelse if (cou nters=8b00101000) 2beg indata_out6v=read; parity_check_result=parity_check_result + read; endelse if (cou nters=8b00111000) 3beg indata_out5v=read; parity_check_result=parity_check_result + read; endelse if (cou nters=8b01001000) 4beg indata_out
13、4=read; parity_check_result=parity_check_result + read; endelse if (cou nters=8b01011000) 5beg indata_out3=read;parity_check_result=parity_check_result + read;endelse if (cou nters=8b01101000) 6beg indata_out2v=read;parity_check_result=parity_check_result + read;endelse if (cou nters=8b01111000) /7b
14、eg indata_out1=read;parity_check_result=parity_check_result + read;endelse if (cou nters=8b10001000) 8beg indata_out0=read;parity_check_result=parity_check_result + read;endelse if (cou nters=8b10011000) 9進行奇偶校驗檢測beg inparity_result=read;parity_result=#2 (parity_check_result = parity_result)1:0;ende
15、lse if (cou nters=8b10101000) 0進行幀檢測beg incs1=(read) ? 1:0;endelse if (counters=8b10101010) 01 給 cpu 發(fā)送接收信號beg incs=(cs1 & parity_result) ? 1:0;/當奇偶校驗結果與幀檢測結果都為1時,cs置位一clear1=1;/clk_e nable=0;clk_e nable3=0;endelse if(cou nters=8b00001000)/檢測是否是毛刺beg inclear1=(!read)?0:1;endelse clear1=0;endelse cle
16、ar1=1;en dmodule的數(shù)據(jù)發(fā)送模塊程序仿真圖當reset為零時圖3-5reset為零時仿真圖如圖為UART的數(shù)據(jù)發(fā)送模塊的功能仿真圖,為方便觀察,其中的時鐘是直 接給出來的,根據(jù)圖中的數(shù)據(jù)判讀,其功能為正確,UART的數(shù)據(jù)發(fā)送模塊編譯成功。1. 當計時器為140 ns時,為數(shù)據(jù)接收2. 當計時器為357ns時,為數(shù)據(jù)發(fā)送3. 當計時器為705ns時,為奇偶校驗結果發(fā)送4. 當計時器為825ns時,發(fā)送高電平當reset為1時如圖3-6圖3-6reset為1時仿真圖3.4UART接收模塊數(shù)據(jù)接收服務串行數(shù)據(jù)幀和接收時鐘是異步的,由邏輯1跳變?yōu)檫壿?可視為一個數(shù)據(jù) 幀的開始,所以接收器
17、首先要判斷起始位。如表3.2計數(shù)器0910操作數(shù)據(jù)起始位檢 測數(shù)據(jù)接收和奇偶校驗奇偶校驗數(shù)據(jù)判斷表3.2uart的數(shù)據(jù)接收服務其基本特點是:UART接收狀態(tài)一共有4個:state0檢測起始位),stat e1(對數(shù)據(jù)位進行采樣, 并串/并轉換),state2奇偶校驗 分析),state3接收數(shù)據(jù)正確與否檢測)。 起始位判讀:當UART接收器復位以后,接收器將處 于這一狀態(tài)。在該狀態(tài), 控制器一直等待read電平的跳變,即從邏輯1變?yōu)檫壿?,也就是等待起始位 的到來。一旦檢測到起始位,就對采樣時鐘elk rev上跳沿計 數(shù),當計數(shù)為 8時,也就是確保在起始位的中間點 ,然后轉到state1狀態(tài)。
18、 數(shù)據(jù)接收:該狀態(tài)下,每間隔16位倍頻采樣一位 串行數(shù)據(jù),接收8位異步 數(shù)據(jù)并進行串/并轉換。即對clk 一 rev上跳沿計數(shù),當為16時,就對數(shù)據(jù)采樣, 這樣 保證了數(shù)據(jù)位是在中點處被采樣的,同時串/并轉換,當檢測到已收到8個 數(shù)據(jù)后以后,便進入了 state2狀態(tài)。 奇偶校驗:該狀態(tài)實現(xiàn)的功能是奇偶校驗。本文采用的是偶校驗。校驗結束以后,轉到state3狀態(tài)。 數(shù)據(jù)幀判讀:該狀態(tài)是用來幀校驗的,即在校驗位以后,檢測停止位是否為邏輯高電平數(shù)據(jù)接收操作圖3-7UART數(shù)據(jù)接收操作圖3-7UART數(shù)據(jù)接收操作解釋:數(shù)據(jù)接收速度9600波特率,以16倍頻接收cpu接收位檢測:當奇偶結果比較和數(shù)據(jù)幀
19、檢測都正確時,cpu檢測接收位CS置位的數(shù)據(jù)接收模塊程序/發(fā)送數(shù)據(jù)模塊module txd(dat_i n,sen d,reset,clk_e nable4,clk,send_enable,clear4,counters,ks);發(fā)送數(shù)據(jù) modulein put7:0 dat_ in,coun ters;in put reset,clk,se nd_en able;output sen d,clk_e nable4,clear4;output ks;/jiawire clear;wire7:0 dat_s;reg sen d,parity_result,ks;reg clk_e nable,
20、clear1,clear4,clk_e nable4;reg7:0 date_s;/always(posedge elk)beginif(send_enable & !reset)當send_enable為高電平時為發(fā)送操作狀態(tài) beginclk_e nable4=1;clear4=clear1;endelsebeginclear4=1;endend/always(posedge clk)if(send_en able & !reset)beginif(cou nters=8bOOOOOOO1)Obeginsen d=0;date_s=dat_ in;/?parity_result=1;end
21、else if(cou nters=8bOOO1OOOO)1beginsen d=date_s0; parity_result=parity_result + date_s0;endelse if(cou nters=8bOO1OOOOO)2beginsen d=date_s1; parity_result=parity_result + date_s0;endelse if(cou nters=8bOO11OOOO)3beginsen d=date_s2; parity_result=parity_result + date_s0;endelse if(cou nters=8b0100000
22、0)/4beginsen d=date_s3;parity_result=parity_result + date_s0;endelse if(cou nters=8bO1O1OOOO)5beginsen d=date_s4; parity_result=parity_result + date_s0;endelse if(cou nters=8bO11OOOOO)6beginsen d=date_s5; parity_result=parity_result + date_s0;endelse if(cou nters=8bO111OOOO)7beginsen d=date_s6; pari
23、ty_result=parity_result + date_s0;endelse if(cou nters=8b10000000)/8beginsen d=date_s7; parity_result=parity_result + date_s0;endelse if(counters=8b10010000)/9 發(fā)送奇偶校驗結果 beginsen d=parity_result;endelse if(counters=8b10100000)/0 發(fā)送高電平beginsend=1;endelse if(cou nters=8b10101111)/0beginclear1=1;ks=(se
24、nd & parity_result) ? 1:0;endelse if(cou nters=8b00000000)beginsend=1;endelse clear1=0;elseendsend:k5-dG.二5口應 *|配WQ1Rbs Ti.rit Sir 12. nt jJ | tr99 03 fitiKtarvklfill 5E S h rtErii也o 皿iw.p m滋mpasiD.p u.債拓口衛(wèi)皿男o.pmir1i1i;1N.r1 l . . r 1 i1111Ji.111i. j. .ii . ii1_1 1_1 111 1_1 l_ 1 :_11 1_1 111 Lnnjvi
25、JTrLrLrLn_rLrLrLrLTLn .nLrLrirLn_r run jl rLrLrLn_rLn_rLTLrb-:jLVJi-.n.unrjir-nrULnr.nnrnnrr.mnuirimriiiimiiinnifwmitiiiWMiiiwiwiiwiMUiMiwifWwri 廠廠廠 iriirir.iirir 眄 iiiinr呱 iriimiiuni “hrJMiScskjGnnJ:ia*圖3-9當counters未指定時間時功能仿真3.5UART控制器控制器服務UART控制器實質(zhì)上是一組計數(shù)器,由 state決定計數(shù)器數(shù)據(jù)發(fā)送對象,在這 里指定當state為1時,發(fā)送到UART
26、接收模塊,反之,發(fā)送到UART發(fā)送模塊 當計數(shù)到指定數(shù)據(jù)時,觸發(fā)指定模塊的制定操作。控制器模塊程序module coun ters(read_e nable,se nd_en able,clk,state,t1,read,co un ters,reset,clear)程序計數(shù)寄存器in put clk,state,t1,read,reset,clear;/stat為 uart 狀態(tài)輸入,/clear為程序計數(shù)寄存器清零控制位output7:0 coun ters;output read_e nable,se nd_en able;reg read_enable,send_enable,cont
27、rol;/read_enab為接收控制位?/send_enable為發(fā)送控制位,control為程序計數(shù)寄存器為零狀態(tài)寄存位 reg7:0 co un ters;/8位程序計數(shù)寄存器always(posedge clk)/當程序計數(shù)寄存器為零時,程序計數(shù)寄存器為零狀態(tài)寄存位置位begi ncon trol=(!co un ters)? 1 : 0;end/always( negedge read)/uar發(fā)送或接收狀態(tài)判斷if(con trol)beg inif(state)begi nif(!read)beg inread_e nable=1;send_en able=0;endelsebeg insend_en able=0;endendelsebegi nsend_en able=1;read_e nable8b10101111)cou nters=8b00000000;elsecoun ters=co un ters + 1;endels
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 工程投標部門管理制度
- 關于病歷病案管理制度
- 六安農(nóng)村公路管理制度
- 廠內(nèi)工齡胸章管理制度
- 勞動中介日常管理制度
- 中醫(yī)內(nèi)科偏方試題及答案
- 嵌入式開發(fā)前景分析試題及答案
- 單位生產(chǎn)用電管理制度
- 安全檔案資料管理制度
- 數(shù)據(jù)庫管理人員發(fā)展路徑試題及答案
- 偉大的《紅樓夢》智慧樹知到期末考試答案章節(jié)答案2024年北京大學
- 中華水文化智慧樹知到期末考試答案2024年
- 整套電子課件:液壓傳動與氣動技術(第二版)
- 《人類起源的演化過程》閱讀測試題及答案
- MOOC 葡萄酒文化與鑒賞-西北工業(yè)大學 中國大學慕課答案
- MOOC 航空發(fā)動機故障診斷-西北工業(yè)大學 中國大學慕課答案
- 學前教育技能實訓報告
- 2024年中儲糧集團招聘筆試參考題庫附帶答案詳解
- 3D打印在醫(yī)療設備中的應用
- 《祝福》-課件(共60張)
- 四川省南充市2022-2023學年八年級下學期期末道德與法治試題
評論
0/150
提交評論