Verilog實(shí)現(xiàn)UART之一:接收模塊_第1頁
Verilog實(shí)現(xiàn)UART之一:接收模塊_第2頁
Verilog實(shí)現(xiàn)UART之一:接收模塊_第3頁
Verilog實(shí)現(xiàn)UART之一:接收模塊_第4頁
全文預(yù)覽已結(jié)束

下載本文檔

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

文檔簡介

Verilog實(shí)現(xiàn)UART之一:接收模塊異步串行數(shù)據(jù)的一般格式是:起始位+數(shù)據(jù)位+停止位,其中起始位1位,8位數(shù)據(jù)位,奇校驗(yàn)、偶校驗(yàn)或無校驗(yàn)位;停止位可以是1、2位,LSBfirst:2.接收原理:由于UART是異步傳輸,沒有傳輸同步時(shí)鐘。為了能保證數(shù)據(jù)傳輸?shù)恼_性,采樣模塊利用16倍數(shù)據(jù)波特率的時(shí)鐘進(jìn)行采樣,假設(shè)波特率為115200,則采樣時(shí)鐘為clk16x=115200×16。每個(gè)數(shù)據(jù)占據(jù)16個(gè)采樣時(shí)鐘周期,1bit起始位+8bit數(shù)據(jù)為+1bit停止位=10bit,因此一幀共占據(jù)16×10=160個(gè)采樣時(shí)鐘,考慮到每個(gè)數(shù)據(jù)為可能有1-2個(gè)采樣時(shí)鐘周期的便宜,因此將各個(gè)數(shù)據(jù)位的中間時(shí)刻作為采樣點(diǎn),以保證采樣不會(huì)滑碼或誤碼。一般UART一幀的數(shù)據(jù)位數(shù)為8,這樣即使每個(gè)數(shù)據(jù)有一個(gè)時(shí)鐘的誤差,接收端也能正確地采樣到數(shù)據(jù)。因此,采樣時(shí)刻為24(跳過起始位的16個(gè)時(shí)鐘)、40、56、72、88、104、120、136、152(停止位),如下圖所示:其中,RX為接收引腳,CNT為對采樣時(shí)鐘進(jìn)行計(jì)數(shù)的計(jì)數(shù)器;3.代碼實(shí)現(xiàn):

/******************************************************************************

*

*Module:rx_module

*FileName:rx_module.v

*Author:JC_Wang

*Version:1.0

*Date:2012/12/5

*Description:UART接收模塊

*

*

********************************************************************************/modulerx_module(

inputGClk,/*systemtopestclock*/

inputclk16x,/*sampleclock,16×115200*/

inputrst_n,/*glabolresetsignal*/

inputrx,/*serialdatain*/

outputregDataReady,/*acompletebytehasbeenreceived*/

outputreg[7:0]DataReceived/*Bytesreceived*/

);/*捕獲rx的下降沿,即起始信號(hào)*/

regtrigger_r0;

wireneg_tri;

always@(posedgeGClkornegedgerst_n)/*下降沿使用全局時(shí)鐘來捕獲的,其實(shí)用clk16x來捕獲也可以*/

begin

if(!rst_n)

begin

trigger_r0<=1'b0;

end

else

begin

trigger_r0<=rx;

end

endassignneg_tri=trigger_r0&~rx;//----------------------------------------------

/*countercontrol*/

regcnt_en;

always@(posedgeGClkornegedgerst_n)

begin

if(!rst_n)

cnt_en<=1'b0;

elseif(neg_tri==1'b1)/*如果捕獲到下降沿,則開始計(jì)數(shù)*/

cnt_en<=1'b1;

elseif(cnt==8'd152)

cnt_en<=1'b0;end

//---------------------------------------------

/*countermodule,對采樣時(shí)鐘進(jìn)行計(jì)數(shù)*/

reg[7:0]cnt;

always@(posedgeclk16xornegedgerst_n)

begin

if(!rst_n)

cnt<=8'd0;

elseif(cnt_en)

cnt<=cnt+1'b1;

else

cnt<=8'd0;end

//---------------------------------------------

/*receivemodule*/

regStopBit_r;

always@(posedgeclk16xornegedgerst_n)

begin

if(!rst_n)

begin

DataReceived<=8'b0;

end

elseif(cnt_en)

case(cnt)

8'd24:DataReceived[0]<=rx;/*在各個(gè)采樣時(shí)刻,讀取接收到的數(shù)據(jù)*/

8'd40:DataReceived[1]<=rx;

8'd56:DataReceived[2]<=rx;

8'd72:DataReceived[3]<=rx;

8'd88:DataReceived[4]<=rx;

8'd104:DataReceived[5]<=rx;

8'd120:DataReceived[6]<=rx;

8'd136:DataReceived[7]<=rx;endcaseendalways@(posedgeclk16xornegedgerst_n)

begin

if(!rst_n)

DataReady<=1'b0;

elseif(cnt==8'd152)

DataReady<=1'b1;//接收到停止位后,給出數(shù)據(jù)準(zhǔn)備好標(biāo)志位

else

DataReady<=1'b0;

endendmodule注:1)采樣時(shí)鐘clk16x必須是波特率的16倍,波特率任意設(shè)置如57600、9600等皆可,只要滿足16倍關(guān)系;2)此模塊經(jīng)過測試上萬字節(jié)的接收,從未出錯(cuò)!3)引入了最高時(shí)鐘對起始信號(hào)下降沿進(jìn)行捕獲,會(huì)造成什么不良影響,比如所謂的“時(shí)鐘滿天飛”問題,博主還不清楚,若您有好的講解,請您發(fā)鏈接給我,在此感謝!4.如果需要校驗(yàn)位的朋友,可以參考xilinx公司的參考設(shè)計(jì):

`timescale1ns/1nsmodulercvr(dout,data_ready,framing_error,parity_error,rxd,clk16x,rst,rdn);inputrxd;/*數(shù)據(jù)接收端*/

inputclk16x;/*采樣時(shí)鐘*/

inputrst;/*復(fù)位信號(hào),高電平有效*/

inputrdn;/*數(shù)據(jù)接收使能,低電平有效*/

output[7:0]dout;/*數(shù)據(jù)輸出*/

outputdata_ready;/*數(shù)據(jù)接收完畢標(biāo)志位*/

outputframing_error;/*幀錯(cuò)誤標(biāo)志位*/

outputparity_error;/*校驗(yàn)位錯(cuò)誤標(biāo)志位*/regrxd1;

regrxd2;

regclk1x_enable;

reg[3:0]clkdiv;

reg[7:0]rsr;

reg[7:0]rbr;

reg[3:

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(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

提交評論