《數(shù)字電子技術(shù)與接口技術(shù)試驗(yàn)教程》課件第5章_第1頁
《數(shù)字電子技術(shù)與接口技術(shù)試驗(yàn)教程》課件第5章_第2頁
《數(shù)字電子技術(shù)與接口技術(shù)試驗(yàn)教程》課件第5章_第3頁
《數(shù)字電子技術(shù)與接口技術(shù)試驗(yàn)教程》課件第5章_第4頁
《數(shù)字電子技術(shù)與接口技術(shù)試驗(yàn)教程》課件第5章_第5頁
已閱讀5頁,還剩88頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

第5章基于HDL的時(shí)序邏輯電路實(shí)驗(yàn)5.1邊沿D觸發(fā)器實(shí)驗(yàn)5.2計(jì)數(shù)器實(shí)驗(yàn)5.3寄存器和移位寄存器實(shí)驗(yàn)5.4串行序列檢測器設(shè)計(jì)

數(shù)字電路分為組合邏輯電路和時(shí)序邏輯電路。組合邏輯電路任何時(shí)刻輸出信號(hào)的邏輯狀態(tài)僅取決于該時(shí)刻輸入信號(hào)的邏輯狀態(tài),電路中不包含記憶性電路或器件。時(shí)序邏輯電路的輸出狀態(tài)不僅與該時(shí)刻的輸入有關(guān),而且還與電路歷史狀態(tài)有關(guān)。時(shí)序邏輯電路的基本單元是觸發(fā)器,具有記憶輸入信息的功能。

5.1邊沿D觸發(fā)器實(shí)驗(yàn)

通過使用ISE軟件和FPGA實(shí)現(xiàn)實(shí)驗(yàn)圖5-1所示的帶有置位和清零端的邊沿D觸發(fā)器的邏輯圖。在ISE上仿真并在實(shí)驗(yàn)開發(fā)板上實(shí)現(xiàn),通過開發(fā)板上的SW7改變D的狀態(tài),觀察觸發(fā)器輸出變化,說明為什么輸出q會(huì)隨著D變化,并用D觸發(fā)器設(shè)計(jì)一個(gè)2分頻計(jì)數(shù)器,通過兩個(gè)LED顯示分頻前后的信號(hào)。部分實(shí)驗(yàn)參考內(nèi)容見后。圖5-1維持阻塞型D觸發(fā)器

(1)實(shí)現(xiàn)圖5-1所示的邊沿D觸發(fā)器的VerilogHDL參考源代碼如下:

moduleflipflopcs(

inputwireclk,

inputwireD,

inputwireset,

inputwireclr,

outputq,

outputnotq

);

wiref1,f2,f3,f4,f5,f6;

assign#5f1=~(f4&f2&~set);//#5表示與門加5個(gè)單位時(shí)間的傳輸延時(shí)

assign#5f2=~(f1&f5&~clr);

assign#5f3=~(f6&f4&~set);

assign#5f4=~(f3&clk&~clr);

assign#5f5=~(f4&clk&f6&~set);

assign#5f6=~(f5&D&~clr);

assignq=f1;

assignnotq=f2;

endmodule

//以下是帶清零端的DFF

moduleDff(

inputwireclk,

inputwireclr,

inputwireen,

inputwireD,

outputregq

);

always@(posedgeclkorposedgeclr)

begin

if(clr)q<=0;

elseif(en)q<=D;

end

endmodule

使用ISESimulator,VerilogHDL參考源代碼的仿真結(jié)果如圖5-2所示。

由圖5-2可見,電路開始工作時(shí),如果set和clr信號(hào)為低電平無效,且clk無有效上沿時(shí),觸發(fā)器的輸出q和notq是不確定的x(圖中的非0非1線),當(dāng)?shù)谝粋€(gè)clk上沿到來時(shí),D觸發(fā)器的輸出狀態(tài)變化(D為1,則輸出q為1)。同時(shí),由圖可見門電路的延時(shí)。圖5-2邊沿D觸發(fā)器的仿真結(jié)果

(2)邊沿D觸發(fā)器的VHDL源代碼如下:

--BehavioralDFlip-FlopwithClockEnableandAsynchronousReset

entityDflipflopis

Port(D,clk,rst,ce:inSTD_LOGIC;

Q:outSTD_LOGIC);

endDflipflop;

architectureBehavioralofDflipflopis

begin

process(clk,rst,D,ce)

begin

ifrst=‘1’thenQ<=‘0’;

elseif(clk‘eventandclk=’1‘)

thenifce=’1‘thenQ<=D;

endif;

endif;

endprocess;

endBehavioral;

--BehavioralDFlip-Flop,SynchronousReset

entityDFFis

Port(D,clk,rst:inSTD_LOGIC;

Q:outSTD_LOGIC);

endDFF;

architectureBehavioralofDFFis

begin

process(clk,rst,D)

begin

if(CLK'eventandCLK='1')then

ifrst='1'thenQ<='0';

elseQ<=D;

endif;

endif;

endprocess;

endBehavioral;

--Behavioral8bitDRegisterwithAsynchronousReset

entityReg8is

port(D:inSTD_LOGIC_VECTOR(7downto0);

clk,rst:inSTD_LOGIC;

Q:outSTD_LOGIC_VECTOR(7downto0));

endReg8;

architectureBehavioralofReg8is

begin

process(clk,rst)

begin

ifrst='1'thenQ<="00000000";

elsif(CLK'eventandCLK='1')thenQ<=D;

endif;

endprocess;

endBehavioral;

(3)帶有置位和清零端的邊沿D觸發(fā)器的約束文件規(guī)定如下:

#Basys2約束文件:

NET"clk"LOC="B8";//時(shí)鐘

NET"D"LOC="N3";//SW7

NET"set"LOC="L3";//SW1

NET"clr"LOC="P11";//SW0

NET"q"LOC="G1"; //LD7

NET"notq"LOC="P4";//LD6

#Basys2約束文件:

NET"clk"LOC="B8";//時(shí)鐘

NET"D"LOC="N3";//SW7

NET"set"LOC="L3";//SW1

NET"clr"LOC="P11";//SW0

NET"q"LOC="G1"; //LD7

NET"notq"LOC="P4";//LD6

5.2計(jì)?數(shù)?器?實(shí)?驗(yàn)

5.2.1計(jì)數(shù)器簡介

計(jì)數(shù)器是一種最常用的時(shí)序邏輯電路,通常有加、減和可逆三種計(jì)數(shù)方式,計(jì)數(shù)器的模多數(shù)為2n進(jìn)制或十進(jìn)制。一般都有使能控制端,當(dāng)控制信號(hào)有效時(shí),一個(gè)N進(jìn)制計(jì)數(shù)器在有效時(shí)鐘Clk邊沿作用下,按照計(jì)數(shù)方式改變次態(tài)。當(dāng)計(jì)數(shù)到最后一個(gè)狀態(tài)Sn-1時(shí),一般會(huì)產(chǎn)生進(jìn)位或/和借位信號(hào),如圖5-3中的TC(TerminalCount),表示最后一個(gè)計(jì)數(shù)狀態(tài),在下一個(gè)有效時(shí)鐘邊沿,狀態(tài)回到初始狀態(tài)S0。2n進(jìn)制的計(jì)數(shù)器每個(gè)輸出都是時(shí)鐘信號(hào)Clk的分頻,且占空比為50%,如圖5-3和圖5-4所示。圖5-3二進(jìn)制計(jì)數(shù)器及波形圖圖5-42n進(jìn)制計(jì)數(shù)器5.2.2計(jì)數(shù)器實(shí)驗(yàn)和預(yù)習(xí)內(nèi)容

(1)學(xué)習(xí)VHDL的讀者分析以下代碼功能。

libraryIEEE;

useIEEE.STD_LOGIC_1164.all;

useIEEE.STD_LOGIC_ARITH.all;

useIEEE.STD_LOGIC_UNSIGNED.all;

entitycounteris

Port(clk:inSTD_LOGIC;

rst:inSTD_LOGIC;

B:inoutSTD_LOGIC_VECTOR(3downto0));

endcounter;

architectureBehavioralofcounteris

begin

process(clk,rst)

begin

if(rst='1')then

B<="0000";

elseif(clk'eventandclk='1')then

B<=B+1;

endif;

endproess;

endBehavioral;

--將50MHz時(shí)鐘分頻為1Hz的代碼如下,常數(shù)是分頻數(shù),可以根據(jù)分頻需要改變。

libraryIEEE;

useIEEE.STD_LOGIC_1164.all;

useIEEE.STD_LOGIC_ARITH.all;

useIEEE.STD_LOGIC_UNSIGNED.all;

entityclkdivis

Port(clk:inSTD_LOGIC;

rst:inSTD_LOGIC;

clkout:outSTD_LOGIC);

endclkdiv;

architectureBehavioralofclkdivis

constantcntendval:STD_LOGIC_VECTOR(25downto0):="10111110101111000010000000";

signalcntval:STD_LOGIC_VECTOR(25downto0);

begin

process(clk,rst)

begin

if(rst='1')then

cntval<="00000000000000000000000000";

elsif(clk'eventandclk='1')then

if(cntval=cntendval)then

cntval<="00000000000000000000000000";

elsecntval<=cntval+1;

endif;

endif;

endprocess;

clkout<=cntval(25);

endBehavioral;仿真并在開發(fā)板上驗(yàn)證以下設(shè)計(jì)(用1s的時(shí)鐘接clk可以看到結(jié)果,BASYS2板上clk接C8)。

LIBRARYIEEE;

USEIEEE.STD_LOGIC_1164.ALL;

USEIEEE.STD_LOGIC_UNSIGNED.ALL;

ENTITYCNT4BIS

PORT(CLK:INSTD_LOGIC;

RST:INSTD_LOGIC;

ENA:INSTD_LOGIC;

OUTY:OUTSTD_LOGIC_VECTOR(3DOWNTO0);

COUT:OUTSTD_LOGIC);

ENDCNT4B;

ARCHITECTUREbehavOFCNT4BIS

SIGNALCQI:STD_LOGIC_VECTOR(3DOWNTO0);

BEGIN

P_REG:PROCESS(CLK,RST,ENA,CQI)

BEGIN

IFRST='1'THENCQI<="0000";

ELSIFCLK'EVENTANDCLK='1'THEN

IFENA='1'THENCQI<=CQI+1;

ENDIF;

ENDIF;

OUTY<=CQI;

ENDPROCESSP_REG;--進(jìn)位輸出

COUT<=CQI(0)ANDCQI(1)ANDCQI(2)ANDCQI(3);

ENDbehav;

(2)使用ISE軟件和FPGA實(shí)現(xiàn)模6計(jì)數(shù)器,即計(jì)數(shù)狀態(tài)從000依次加1到101狀態(tài),下一次有效觸發(fā)沿到來時(shí)再回到000狀態(tài)。仿真并在開發(fā)板上驗(yàn)證其計(jì)數(shù)功能。

modulemod6cnt(

inputwireclr,

inputwireclk,

outputreg[2:0]q

);

reg[24:0]q1;

//25位計(jì)數(shù)器,對(duì)50MHz時(shí)鐘進(jìn)行225分頻

always@(posedgeclkorposedgeclr)

begin

if(clr==1)

q1<=0;

else

q1<=q1+1;

end

assignmclk=q1[24];//1.5Hz

//模6計(jì)數(shù)器

always@(posedgemclkorposedgeclr)

begin

if(clr==1)

q<=0;

elseif(q==5)

q<=0;

else

q<=q+1;

end

endmodule

建立上述代碼的VerilogTestFixture仿真文件,在文件模板中添加以下激勵(lì)代碼:

//Addstimulushere

#100;clr<=1;clk<=0;

#100;clr<=1;clk<=1;

#100;clr<=0;clk<=0;

#100;clr<=0;clk<=1;

#100;clr<=0;clk<=0;

#100;clr<=0;clk<=1;

#100;clr<=0;clk<=0;

#100;clr<=0;clk<=1;

#100;clr<=0;clk<=0;

#100;clr<=0;clk<=1;

#100;clr<=0;clk<=0;

#100;clr<=0;clk<=1;

#100;clr<=0;clk<=0;

#100;clr<=0;clk<=1;

#100;clr<=1;clk<=0;

#100;clr<=1;clk<=1;仿真結(jié)果如圖5-5所示,由圖可見clr清0無效,而且在出現(xiàn)clk有效上沿后計(jì)數(shù)器的計(jì)數(shù)值q[2:0]始終為000,分析原因。圖5-5模6計(jì)數(shù)器仿真結(jié)果模6計(jì)數(shù)器的VHDL程序:

entitymod6cntis

Port(clr:inSTD_LOGIC;

clk:inSTD_LOGIC;

q:inoutSTD_LOGIC_VECTOR(2downto0));

endmod6cnt;

architectureBehavioralofmod6cntis

signalq1:std_logic_vector(24downto0);

signalmclk:std_logic;

begin

process(clr,clk)

begin

if(clr='1')then

q1<="0000000000000000000000000";

elsif(clk'eventandclk='1')then

q1<=q1+1;

endif;

endprocess;

mclk<=q1(24);

process(mclk,clr,q)

begin

if(clr='1')then

q<="000";

elsif(q="110")then

q<="000";

elsif(mclk'eventandmclk='1')then

q<=q+1;

endif;

endprocess;

endBehavioral;在開發(fā)板上驗(yàn)證模6計(jì)數(shù)器的約束文件規(guī)定如下,觀察結(jié)果是否正確。

#Basys2約束文件:

NET"clk"LOC="B8";//時(shí)鐘

NET"clr"LOC="P11";//SW0

NET"q[2]"LOC="G1";//LD7

NET"q[1]"LOC="P4";//LD6

NET"q[0]"LOC="N4";//LD5

#Basys2約束文件:

NET"clk"LOC="B8";//時(shí)鐘

NET"clr"LOC="P11";//SW0

NET"q[2]"LOC="G1";//LD7

NET"q[1]"LOC="P4";//LD6

NET"q[0]"LOC="N4";//LD5在實(shí)現(xiàn)上述工程文件之后,Place&Route中出現(xiàn)以下WARNING信息,查找資料說明是什么問題。

WARNING:Route:455-CLKNet:q1<24>mayhaveexcessiveskewbecause

但WARNING并不影響設(shè)計(jì)文件的功能,將上述工程產(chǎn)生的代碼下載到開發(fā)板,在開發(fā)板的LD7~LD5可以看到計(jì)數(shù)循環(huán)是由000B依次加1到101B的6進(jìn)制計(jì)數(shù)狀態(tài)。

(3)在上述實(shí)驗(yàn)基礎(chǔ)上,設(shè)計(jì)一個(gè)秒脈沖發(fā)生器,用LED指示秒脈沖的發(fā)生。

5.3寄存器和移位寄存器實(shí)驗(yàn)

寄存器是數(shù)字系統(tǒng)中用來存儲(chǔ)二進(jìn)制數(shù)據(jù)的邏輯器件,在數(shù)字系統(tǒng)中廣泛使用。當(dāng)然,每個(gè)觸發(fā)器都可以寄存一位二進(jìn)制數(shù),但寄存器一般是指可以存儲(chǔ)多位二進(jìn)制數(shù)的邏輯電路或器件。寄存器內(nèi)部包含多個(gè)觸發(fā)器,待保存的數(shù)據(jù)在外部時(shí)鐘脈沖統(tǒng)一控制下存入觸發(fā)器中。如果觸發(fā)器的輸出經(jīng)三態(tài)門接到寄存器的引腳,則寄存器為三態(tài)輸出。寄存器電路按邏輯功能可分為并行寄存器、串行寄存器和串并行寄存器。并行寄存器是指其輸入輸出都是并行的,沒有移位功能,通常簡稱為寄存器。串行寄存器具有移位功能,因此也稱為移位寄存器。5.3.1寄存器實(shí)驗(yàn)和預(yù)習(xí)內(nèi)容

(1)根據(jù)所學(xué)的HDL語言,分析下面的程序代碼中各個(gè)參數(shù)含義以及實(shí)現(xiàn)的邏輯功能,并對(duì)代碼進(jìn)行仿真驗(yàn)證。

moduleregister

#(parameterN=8)

(inputwireload,

inputwireclk,

inputwireclr,

inputwire[N-1:0]d,

outputreg[N-1:0]q

);

always@(posedgeclkorposedgeclr)

if(clr==1)

q<=0;

elseif(load==1)

q<=d;

endmodule

LibraryIEEE

useIEEE.std_logic_1164.all

entityreg12is

PORT(

d:INSTDLOGICVECTOR(11DOWNTO0);

clk:INSTDLOGIC;

q:OUTSTDLOGIC_VECTOR(11DOWNTO0));

endreg12;

architectureBehavioralofreg12is

begin

ifclk'eventandclk='1'then

q<=d;

endif;

ENDPROCESS;

endBehavioral;

(2)試設(shè)計(jì)一個(gè)帶有異步清零和置數(shù)信號(hào)(置數(shù)為全邏輯1)的4位寄存器,并在開發(fā)板上驗(yàn)證。5.3.2移位寄存器實(shí)驗(yàn)和預(yù)習(xí)內(nèi)容

(1)使用ISE軟件和FPGA分別實(shí)現(xiàn)圖5-6所示的4位移位寄存器功能。

圖5-6的Verilog設(shè)計(jì)參考源代碼如下,在開發(fā)板上驗(yàn)證該功能。學(xué)習(xí)VHDL的讀者分析下面的VHDL代碼完成什么功能。修改代碼完成圖5-6的功能。圖5-64位移位寄存器

moduleShiftReg(

inputwireclk,

inputwireclr,

inputwiredata_in,

outputreg[3:0]q

);

reg[24:0]q1;

//25位計(jì)數(shù)器進(jìn)行分頻

always@(posedgeclkorposedgeclr)

begin

if(clr==1)

q1<=0;

else

q1<=q1+1;

end

assignmclk=q1[24];//1.5Hz

//4位移位寄存器

always@(posedgemclkorposedgeclr)

begin

if(clr==1)

q<=0;

else

begin

q[3]<=data_in;

q[2:0]<=q[3:1];

end

end

endmodule

libraryieee;

useieee.std_logic_1164.all;

useieee.std_logic_arith.all;

useieee.std_logic_unsigned.all;

entityShiftRegis

port( data:instd_logic_vector(3downto0);

left_da,right_da,reset,clk:instd_logic;

mode:instd_logic_vector(1downto0);

qout:bufferstd_logic_vector(3downto0));

endShiftReg;

architecturebehaveofShiftRegis

begin

process

begin

waituntilrising_edge(clk);

if(reset='1')then

qout<="0000";

else

casemodeis

when"01"=qout<=right_da&qout(3downto1);

when"10"=>qout<=qout(2downto0)&left_da;

when"11"=>qout<=data;

whenothers=>null;

endcase;

endif;

endprocess;

endbehave;建立Verilog代碼的仿真文件,在文件模板中加入以下激勵(lì)代碼:

//Addstimulushere

#100;clr<=1;data_in<=1;clk<=0;

#100;clr<=1;data_in<=1;clk<=1;

#100;clr<=0;data_in<=1;clk<=0;

#100;clr<=0;data_in<=1;clk<=1;

#100;clr<=0;data_in<=1;clk<=0;

#100;clr<=0;data_in<=1;clk<=1;

#100;clr<=0;data_in<=1;clk<=0;

#100;clr<=0;data_in<=1;clk<=1;

#100;clr<=0;data_in<=1;clk<=0;

#100;clr<=0;data_in<=1;clk<=1;

#100;clr<=1;data_in<=1;clk<=0;圖5-7是Verilog代碼實(shí)現(xiàn)的圖5-6的功能仿真圖,由圖可見clr的高電平異步清0作用和數(shù)據(jù)移位功能。圖5-7圖5-6所示移位寄存器仿真圖移位寄存器的VHDL設(shè)計(jì)參考源代碼如下:

libraryieee;

useieee.std_logic_1164.all;

useieee.std_logic_arith.all;

useieee.std_logic_unsigned.all;

entityShiftRegis

port(data:instd_logic_vector(3downto0);

left_da,right_da,reset,clk:instd_logic;

mode:instd_logic_vector(1downto0);

qout:bufferstd_logic_vector(3downto0));

endShiftReg;

architecturebehaveofShiftRegis

begin

process

begin

waituntilrising_edge(clk);

if(reset='1')then

qout<="0000";

else

casemodeis

when"01"=>qout<=right_da&qout(3downto1);

when"10"=>qout<=qout(2downto0)&left_da;

when"11"=>qout<=data;

whenothers=>null;

endcase;

endif;

endprocess;

endbehave;

如果約束文件規(guī)定如下,程序下載到開發(fā)板。當(dāng)clr為高電平寄存器清0,LD7~LD4熄滅,當(dāng)clr為低電平,SW7撥上為高電平,則可見LD7到LD4依次點(diǎn)亮,SW7為0,則依次熄滅。說明移位的正確性。#Basys2約束文件:

NET"clk"LOC="B8";

NET"data_in"LOC="N3";//SW7

NET"clr"LOC="P11"; //SW0

NET"q[3]"LOC="G1";//LD7

NET"q[2]"LOC="P4";//LD6

NET"q[1]"LOC="N4";//LD5

NET"q[0]"LOC="N5";//LD4#Basys2約束文件:

NET"clk"LOC="B8";

NET"data_in"LOC="N3";//SW7

NET"clr"LOC="P11"; //SW0

NET"q[3]"LOC="G1";//LD7

NET"q[2]"LOC="P4";//LD6

NET"q[1]"LOC="N4";//LD5

NET"q[0]"LOC="N5";//LD4

(2)圖5-8所示的4位移位器是在算術(shù)運(yùn)算單元(ALU)中用到的多種邏輯或算術(shù)移位。用HDL描述電路功能,給出仿真波形,并在開發(fā)板上驗(yàn)證。

圖5-8的VerilogHDL參考代碼如下:

moduleshift4(

inputwire[3:0]d,

inputwire[2:0]s,

outputreg[3:0]y

);

always@(*)

case(s)

0:y=d; //noshift

1:y={1'b0,d[3:1]}; //shr

2:y={d[2:0],1'b0}; //shl

3:y={d[0],d[3:1]}; //ror

4:y={d[2:0],d[3]}; //rol

5:y={d[3],d[3:1]}; //asr

6:y={d[1:0],d[3:2]}; //ror2

7:y=d; //noshift

default:y=d;

endcase

endmodule圖5-8多種邏輯或算術(shù)移位圖5-8的VHDL程序如下:

entityshift4is

Port(d:inSTD_LOGIC_VECTOR(3downto0);

s:inSTD_LOGIC_VECTOR(2downto0);

y:outSTD_LOGIC_VECTOR(3downto0));

endshift4;

architectureBehavioralofshift4is

begin

process(d,s)

begin

casesis

when“000”=>y<=d;

when"001"=>y<='0'&d(3downto1);

when"010"=>y<=d(2downto0)&'0';

when"011"=>y<=d(0)&d(3downto1);

when"100"=>y<=d(2downto0)&d(3);

when"101"=>y<=d(3)&d(3downto1);

when"110"=>y<=d(1downto0)&d(3downto2);

whenothers=>null;

endcase;

endprocess;

endBehavioral;5.3.3寄存器和簡單外設(shè)綜合實(shí)驗(yàn)

使用ISE軟件和FPGA設(shè)計(jì)一個(gè)可以把4個(gè)SW開關(guān)的內(nèi)容存儲(chǔ)到一個(gè)4位寄存器的電路,并在開發(fā)板的最右邊的七段顯示管上顯示這個(gè)寄存器中的十六進(jìn)制數(shù)字。設(shè)計(jì)頂層原理圖如圖5-9所示。分頻模塊clkdiv用以產(chǎn)生模塊clock_pulse和x7segbc的時(shí)鐘信號(hào)clk190(該模塊輸入mclk為50MHz,輸出為190Hz);clock_pulse為按鍵去抖動(dòng)模塊,btn[0]是clock_pulse輸入信號(hào);寄存器模塊register用btn[1]作為加載信號(hào)load;x7segbc為七段數(shù)碼管的譯碼和控制模塊。圖5-9設(shè)計(jì)頂層原理圖圖5-9的VerilogHDL參考設(shè)計(jì)源代碼如下:

//頂層設(shè)計(jì):

modulesw2regtop(

inputwiremclk,

inputwireclr,

inputwire[1:0]btn,

inputwire[3:0]sw,

outputwire[3:0]ld,

outputwire[6:0]a_to_g,

outputwire[3:0]an,

outputwiredp

);

wire[3:0]q;

wireclk190,clkp;

wire[3:0]x;

assignx=q;

assignld=sw;

clkdivU1(.mclk(mclk),

.clr(clr),

.clk190(clk190)

);

clock_pulseU2(.inp(btn[0]),

.cclk(clk190),

.clr(clr),

.outp(clkp)

);

register#(.N(4))

U3(.load(btn[1]),

.clk(clkp),

.clr(clr),

.d(sw),

.q(q)

);

x7segbcU4(.x(x),

.a_to_g(a_to_g),

.an(an),

.dp(dp)

);

endmodule

//分頻模塊:

moduleclkdiv(

inputwiremclk,

inputwireclr,

outputwireclk190

);

reg[17:0]q;

//18位計(jì)數(shù)器

always@(posedgemclkorposedgeclr)

begin

if(clr==1)

q<=0;

else

q<=q+1;

end

assignclk190=q[17];//190Hz

endmodule

//去抖動(dòng)模塊:

moduleclock_pulse(

inputwireinp,

inputwirecclk,

inputwireclr,

outputwireoutp

);

regdelay1;

regdelay2;

regdelay3;

always@(posedgecclkorposedgeclr)

begin

if(clr==1)

begin

delay1<=0;

delay2<=0;

delay3<=0;

end

else

begin

delay1<=inp;

delay2<=delay1;

delay3<=delay2;

end

end

assignoutp=delay1&delay2&~delay3;

endmodule

//寄存器模塊:

moduleregister

#(parameterN=4)

(inputwireload,

inputwireclk,

inputwireclr,

inputwire[N-1:0]d,

outputreg[N-1:0]q

);

always@(posedgeclkorposedgeclr)

if(clr==1)

q<=0;

elseif(load==1)

q<=d;

endmodule

//七段數(shù)碼管譯碼和控制模塊(輸入時(shí)鐘信號(hào)cclk應(yīng)為190Hz):

modulex7segbc(

inputwire[3:0]x,

outputreg[6:0]a_to_g,

outputwire[3:0]an,

outputwiredp

);

assigndp=1; //小數(shù)點(diǎn)不顯示

assignan=4'b1110; //使能最右邊的數(shù)碼管

//七段解碼器:hex7seg

always@(*)

case(x)

0:a_to_g=7'b0000001;

1:a_to_g=7'b1001111;

2:a_to_g=7'b0010010;

3:a_to_g=7'b0000110;

4:a_to_g=7'b1001100;

5:a_to_g=7'b0100100;

6:a_to_g=7'b0100000;

7:a_to_g=7'b0001111;

8:a_to_g=7'b0000000;

9:a_to_g=7'b0000100;

'hA:a_to_g=7'b0001000;

'hB:a_to_g=7'b1100000;

'hC:a_to_g=7'b0110001;

'hD:a_to_g=7'b1000010;

'hE:a_to_g=7'b0110000;

'hF:a_to_g=7'b0111000;

default:a_to_g=7'b0000001;//0

endcase

endmodule圖5-9的VHDL參考設(shè)計(jì)源代碼如下:

頂層程序如下:

entitysw2regtopis

Port(mclk:instd_logic;

clr:instd_logic;

btn:instd_logic_vector(1downto0);

sw:instd_logic_vector(3downto0);

ld1:outstd_logic_vector(3downto0);

a_to_g:outstd_logic_vector(6downto0);

an:outstd_logic_vector(3downto0);

dp:outstd_logic

);

endsw2regtop;

architectureBehavioralofsw2regtopis

componentclkdivis

Port(mclk:inSTD_LOGIC;

clr:inSTD_LOGIC;

clk190:outSTD_LOGIC);

endcomponent;

componentclock_pulseis

Port(inp:inSTD_LOGIC;

cclk:inSTD_LOGIC;

clr:inSTD_LOGIC;

outp:outSTD_LOGIC);

endcomponent;

componentx7segbcis

Port(x:inSTD_LOGIC_VECTOR(3downto0);

a_to_g:outSTD_LOGIC_VECTOR(6downto0);

an:outSTD_LOGIC_VECTOR(3downto0);

dp:outSTD_LOGIC);

endcomponent;

componentregis

Port(load:inSTD_LOGIC;

clk:inSTD_LOGIC;

clr:inSTD_LOGIC;

d:inSTD_LOGIC_VECTOR(3downto0);

q:outSTD_LOGIC_VECTOR(3downto0));

endcomponent;

signalclk190,clkp:STD_LOGIC;

--signalx:std_logic_vector(3downto0);

signalq:std_logic_vector(3downto0);

begin

--x<=q;

ld1<=sw;

c_div:clkdiv

portmap(mclk,clr,clk190);

c_pulse:clock_pulse

portmap(btn(0),clk190,clr,clkp);

c_reg:reg

portmap(btn(1),clkp,clr,sw,q);

c_dis:x7segbc

portmap(q,a_to_g,an,dp);

endBehavioral;

//分頻模塊:

entityclkdivis

Port(mclk:inSTD_LOGIC;

clr:inSTD_LOGIC;

clk190:outSTD_LOGIC);

endclkdiv;

architectureBehavioralofclkdivis

signalq:std_logic_vector(17downto0);

begin

process(mclk,clr)

begin

if(clr='1')then

q<="000000000000000000";

elsif(mclk'eventandmclk='1')then

q<=q+1;

endif;

endprocess;

clk190<=q(17);

endBehavioral;

//去抖動(dòng)模塊:

entityclock_pulseis

Port(inp:inSTD_LOGIC;

cclk:inSTD_LOGIC;

clr:inSTD_LOGIC;

outp:outSTD_LOGIC);

endclock_pulse;

architectureBehavioralofclock_pulseis

signaldelay1:std_logic;

signaldelay2:std_logic;

signaldelay3:std_logic;

begin

process(clr,cclk,inp)

begin

if(clr='1')then

delay1<='0';

delay2<='0';

delay3<='0';

elsif(cclk'eventandcclk='1')then

delay1<=inp;

delay2<=delay1;

delay3<=delay2;

endif;

endprocess;

outp<=delay1anddelay2and(notdelay3);

endBehavioral;

//寄存器模塊:

entityregis

Port(load:inSTD_LOGIC;

clk:inSTD_LOGIC;

clr:inSTD_LOGIC;

d:inSTD_LOGIC_VECTOR(3downto0);

q:outSTD_LOGIC_VECTOR(3downto0));

endreg;

architectureBehavioralofregis

begin

process(clk,load,clr,d)

begin

if(clr='1')then

q<="0000";

elsif(clk'eventandclk='1')then

if(load='1')then

q<=d;

endif;

endif;

endprocess;

endBehavioral;

//7段數(shù)碼管譯碼和控制模塊(輸入時(shí)鐘信號(hào)cclk應(yīng)為190Hz):

libraryIEEE;

useIEEE.STD_LOGIC_1164.ALL;

--Uncommentthefollowinglibrarydeclarationifusing

--arithmeticfunctionswithSignedorUnsignedvalues

--useIEEE.NUMERIC_STD.ALL;

--Uncommentthefollowinglibrarydeclarationifinstantiating

--anyXilinxprimitivesinthiscode.

--libraryUNISIM;

--useUNISIM.VComponents.all;

entityx7segbcis

Port(x:inSTD_LOGIC_VECTOR(3downto0);

a_to_g:outSTD_LOGIC_VECTOR(6downto0);

an:outSTD_LOGIC_VECTOR(3downto0);

dp:outSTD_LOGIC);

endx7segbc;

architectureBehavioralofx7segbcis

begin

an<="1110";

dp<='1';

process(x)

begin

casexis

when"0000"=>a_to_g<="0000001";

when"0001"=>a_to_g<="1001111";

when"0010"=>a_to_g<="0010010";

when"0011"=>a_to_g<="0000110";

when"0100"=>a_to_g<="1001100";

when"0101"=>a_to_g<="0100100";

when"0110"=>a_to_g<="0100000";

when"0111"=>a_to_g<="0001111";

when"1000"=>a_to_g<="0000000";

when"1001"=>a_to_g<="0000100";

when"1010"=>a_to_g<="0001000";

when"1011"=>a_to_g<="1100001";

when"1100"=>a_to_g<="0110001";

when"1101"=>a_to_g<="1000010";

when"1110"=>a_to_g<="0110000";

when"1111"=>a_to_g<="0111000";

whenothers=>a_to_g<="0000001";

endcase;

endprocess;

endBehavioral;約束文件規(guī)定如下:

#Nexys3約束文件:

NET“mclk”LOC=“V10”;

NET“clr”LOC=“T10”;

NET“btn[1]”LOC=“V9”;

NET“btn[0]”LOC=“M8”;

NET“sw[3]”LOC=“T5”;

NET“sw[2]”LOC=“V8”;

NET“sw[1]”LOC=“U8”;

NET“sw[0]”LOC=“N8”;

NET“l(fā)d[3]”LOC=“T11”;

NET“l(fā)d[2]”LOC=“R11”;

NET“l(fā)d[1]”LOC=“N11”;

NET“l(fā)d[0]”LOC=“M11”;

NET"a_to_g[0]"LOC="L14";//G#Basys2約束文件:

NET"mclk"LOC="B8";//時(shí)鐘引腳

NET"clr"LOC="P11"; //SW0

NET"btn[1]"LOC="K3";//SW2

NET"btn[0]"LOC="B4";//SW3

NET"sw[3]"LOC="N3";//SW7

NET"sw[2]"LOC="E2";//SW6

NET"sw[1]"LOC="F3";//SW5

NET"sw[0]"LOC="G3";//SW4

NET"ld[3]"LOC="G1";//LD7

NET"ld[2]"LOC="P1"; //LD6

NET"ld[1]"LOC="N4";//LD5

NET"ld[0]"LOC="N5";//LD4

NET"a_to_g[0]"LOC="M12"; //G

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(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ǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論