【正文】
:in std_logic。 S1:in std_logic_vector(7 downto 0)。 ―― 置數(shù)端(秒) Sec:buffer std_logic_vector(7 downto 0)。 ―― 秒輸出端 Ensec:out std_logic)。 ― 秒計時器的進位,用來驅(qū)動分計時器 End。 Architecture a of second1 is Begin Process(clk,reset,set,s1) Begin If reset=39。039。 then sec=00000000。 ―― 對秒計時器清 0 Elsif set=39。039。 then sec=s1。 ―― 對秒計時器置 s1的數(shù) Elsif clk39。event and clk=39。139。 then if sec=59 then sec=00000000。ensec=39。139。 ―― 重復計數(shù)并產(chǎn)生進位 else sec=sec+1。ensec=39。039。 以驅(qū)動下一級 end if。 end if。 End process。 End。 分模塊程序 Library ieee。 Use 。 Use 。 7 Use 。 Entity minute1 is Port(clk,set,reset:in std_logic。 m1:in std_logic_vector(7 downto 0)。 ―― 置數(shù)端(分) min:buffer std_logic_vector(7 downto 0)。 ―― 分輸出端 Ensec:out std_logic)。 ― 分計時器的進位,用來驅(qū)動時計時器 End。 Architecture a of minute1 is Begin Process(clk,reset,set,m1) Begin If reset=39。039。 then sec=00000000。 ―― 對分計時器清 0 Elsif set=39。039。 then sec=m1。 ―― 對分計時器置 m1的數(shù) Elsif clk39。event and clk=39。139。 then if sec=59 then sec=00000000。ensec=39。139。 ―― 重復計數(shù)并產(chǎn)生進位 else min=sec+1。ensec=39。039。 以驅(qū)動下一級 end if。 end if。 End process。 End。 時模塊程序 Library ieee。 Use 。 Use 。 Use 。 Entity hour1 is Port(clkh,set,reset:in std_logic。 h1:in std_logic_vector(7 downto 0)。 ―― 置數(shù)端(時) hour:buffer std_logic_vector(7 downto 0)。 ―― 時輸出端 Enhour:out std_logic)。 ―― 時計時器的進位,用來驅(qū)動星期計時器 End。 Architecture a of hour1 is Begin Process(clkh,reset,set,h1) Begin If reset=39。039。 then hour=00000000。 ―― 對時計時器清 0 Elsif set=39。039。 then hour=h1。 ―― 對時計時器置 h1 的數(shù) Elsif clkh39。event and clkh=39。139。 then if hour=23 then hour=00000000。enhour=39。139。―― 重復計數(shù) else hour=hour+1。enhour=39。039。 并產(chǎn)生進位以驅(qū)動下一級 end if。 end if。 8 End process。 End。 星期模塊程序 Library ieee。 Use 。 Use 。 Use 。 Entity day1 is Port(clkd,set,reset:in std_logic。 d1:in std_logic_vector(2 downto 0)。 ―― 置數(shù)端(星期) day:buffer std_logic_vector(2 downto 0))。 ―― 星期輸出端 end。 Architecture a of day1 is Begin Process(clkd,reset,set,d1) Begin If reset=39。039。 then day=000。 ―― 對星期計時器清 0 Elsif set=39。039。 then day=d1。 ―― 對星期計時器置 d1的數(shù) Elsif clkd39。event and clkd=39。139。 then If day=6 then day=000。 ―― 重復計數(shù) Else day=day+1。 End if。 End if。 End process。 End。 報時模塊程序 Library ieee。 Use 。 Use 。 Use 。 Entity alarm1 is Port(reset:in std_logic。 Min:in std_logic_vector(7 downto 0)。 Alarm:out std_logic)。 ―― 輸出的報時信號 End。 Architecture a of alarm1 is Begin Alarm=39。139。 when min=00000000 and reset=39。139。 else ―― 當分為 0且清 0 39。039。 信號無效時,輸出高電平并持續(xù)至分不為 0 end。 系統(tǒng)設計 9 將上述 5個程序作為底層文件,存放在同一個文件夾中,然后按下面的圖將這幾個文件連接起來,并用元件例化語句編寫頂層文件的程序,如下 : Library ieee。 Use 。 Use 。 Use 。 Entity topclock is Port(clk,reset,set:in std_logic。 S1,m1,h1:in std_logic_vector(7 downto 0)。 D1: in std_logic_vector(2 downto 0)。 Alarm:out std_logic。 Sec,min,hour:buffer std_logic_vector(7 downto 0)。 Day:out std_logic_vector(2 downto 0))。 End。 Architecture one of topclock is Component second1 ―― 秒元件的例化 Port(clk,reset,set: in std_logic。 S1: in std_logic_vector(7 downto 0)。 Sec:buffer std_logic_vector(7 downto 0)。 Ensec:out std_logic)。 End Component。 Component minute1 ―― 分元件的例化 Port(clkm,reset,set: in std_logic。 m1: in std_logic_vector(7 downto 0)。 min:buffer std_logic_vector(7 downto 0)。 Enmin:out std_logic)。 End Component。 Component hour1 ―― 時元件的例化 Port(clkh,reset,set: in std_logic。 h1: in std_logic_vector(7 downto 0)。 hour:buffer std_logic_vector(7 downto 0)。 Enhour:out std_logic)。 End Component。 Component day1 ―― 星期元件的例化 Port(clkd,reset,set: in std_logic。 10 d1: in std_logic_vector(2 downto 0)。 day:buffer std_logic_vector(2 downto 0))。 End Component。 Component alarm1 ―― 報時元件的 例化 Port(reset: in std_logic。 min: in std_logic_vector(7 downto 0)。 alarm:out std_logic)。 End Component。 signal enm,enh,enda:std_logic。 ―― 秒分、分時、時星期之間的連接信號 signal ena:std_logic_vector(7 downto 0)。 ―― 分與報時之間 的連接信號 begin u1:second1 port map(reset=reset,set=set,s1=s1, sec=sec,clk=clk, ensec=enm)。 u2:minute1 port map(reset=reset,set=set,m1=m1, min=min, clkm=enm,enmin=enh)。 u3:hour1 port map(reset=reset,set=set,h1=h1, hour=hour, clkh=enh,enhour=enda)。 u4:day1 port map(reset=reset,set=set,d1=d1,day=day,clkd=enda)。 u5:alarm1 port map(reset=reset,min=min,alarm=alarm)。 end。 6 調(diào)試過程 秒模塊調(diào)試 在秒計時器的 clk輸入一個周期為 5ns的時鐘信號;清 0端( reset)前面一小段( 100ns)為低電平,后面均為高電平;置數(shù)端( set)前面一小段( 200ns)為低電平,后面均為高電平;秒重置端( s1)可設置數(shù)值為 50秒,保存波形圖,進行仿真,產(chǎn)生如下波形: 由上述波形可以清楚的看到:當清 0信號( reset)無效時,秒計時器置數(shù),從 50秒開 11 始計數(shù),到 59秒時回到 0,并且從 ensec輸出一個高電平。 分模塊調(diào)試 在分計時器的 clkm輸入一個周期為 5ns的時鐘信號;清 0端( reset)前面一小段( 100ns)為低電平,后面均為高電平;置數(shù)端( set)前面一小段( 200ns)為低電平,后面均為高電平;分重置端( m1)可設置數(shù)值為 50分,保存波形圖,進行仿真,產(chǎn)生如下波形: 由上述波形可以清楚的看到:當清 0信號( reset)無效時,分計時器置數(shù),從 50分開始計數(shù),到 59秒時回到 0,并且從 enmin輸出一個高電平。 時模塊調(diào)試 在時計時器的 clkh輸入一個周期為