【正文】
圖4-13。直到鬧鈴設(shè)定時(shí)間到達(dá),則警示燈led_alarm亮,此信號(hào)可用來(lái)觸發(fā)鬧鈴。信號(hào)仿真結(jié)果如圖412所示。 當(dāng)時(shí)間進(jìn)行到7s時(shí),設(shè)定stop=’1’,sec_tune=’1’,因此輸出時(shí)間second變成定時(shí)器設(shè)定時(shí)間s_sec,直到5s時(shí)ok=’1’,仍然顯示定時(shí)器時(shí)間,直到s_sec為0,此時(shí)led_stop=’1’,而時(shí)間顯示則還原為n_sec=18s。 u18: scan2 port map(rst=rst,clk=clk,a=hour7_one, b=hour7_ten,mux_out=seg2,pa=p5,pb=p6)。 u16:bin2led port map(bin=hour_ten,led=hour7_ten)。 u14:bin2led port map(bin=min_ten,led=min7_ten)。 u12:bin2led port map(bin=sec_ten,led=sec7_ten)。 block模塊的程序代碼。 掃描多路輸出功能模塊 為了節(jié)省IC的輸出引腳及耗電量,可以將4組數(shù)字輸出或2組數(shù)字輸出作為多路輸出,故所需引腳數(shù)由28降至7個(gè),14降至7個(gè);另外使用四合一型七段顯示器與視覺(jué)暫留效應(yīng)可降低顯示耗電量3/4。 u8:i24bcd port map(interg=hour,ten=hour_ten,one=hour_one)。tranformed to bcd format u6:i60bcd port map(interg=second,ten=sec_ten,one=sec_one)。 end if。 hour=n_hour。 else second=n_sec。 minute=a_min。 hour=s_hour。 輸出時(shí)間的顯示條件 output block的VHDL碼 to decide which time displayed and transformed to bcd format output:block input:clk,stop_disp,alarm_disp input:s_sec,s_min,s_hour,a_sec,a_min,a_hour,n_sec, n_min, n_hour output:second,minute,hour begin process(clk,stop_disp,alarm_disp) begin if rising_edge(clk) then if stop_disp=’1’ then second=s_sec。轉(zhuǎn)換成BCD形式。另外,在定時(shí)器計(jì)時(shí)過(guò)程中,為了觀察進(jìn)行時(shí)間,必須顯示正在計(jì)時(shí)的剩余時(shí)間。 end block alarm_setting。 end if。 p2:process(rst,alarm_index) begin if rst=’1’ then led_alarm=’0’。 alarm_disp=(alarm and not ok) and not alarm_index。 end if。 elsif alarm=’1’ and ok=’1’ then if (a_sec=n_sec and a_min=n_min and a_hour=n_hour) then alarm_index=’1’。 alarm_setting block的VHDL碼 alarm_setting:block input : rst,sec_tune,min_tune,hour_tune,alarm,ok output: a_sec,a_min,a_hour,alarm_index,led_alarm begin u5:alarm_set port map(rst=rst,hz1=hz1,alarm=alarm,ok=ok,sec_tune= sec_tune,min_tune=min_tune,hour_tune= hour_tune,sec=a_sec,min=a_min,hour=a_hour)。 鬧鐘設(shè)定與時(shí)間對(duì)比功能模塊 在此模塊中,然后確認(rèn)指針開(kāi)關(guān)ok處于on(’1’)時(shí),開(kāi)始進(jìn)行目前正常時(shí)間與鬧鐘設(shè)定時(shí)間的對(duì)比動(dòng)作,對(duì)比完成即設(shè)定alarm_index為1,然后再進(jìn)一步標(biāo)示出鬧鐘設(shè)定狀態(tài)led_alarm = ’1’。 end process p1。 elsif rising_edge(stop_index) then led_stop=’1’。 stop_watch block的VHDL碼 stop watch setting and down_counting stop_w: block input:rst,hz1,stop,ok,sec_tune,min_tune,hour_tune output:s_sec,s_min,s_hour,stop_index begin u4:stop_watch port map(rst=rst,hz1=hz1,stop=stop,ok=ok,sec_tune= sec_tune,min_tune=min_tune,hour_tune=hour_tune,stop_sec= s_sec,stop_min=s_min,stop_hour=s_hour,index= stop_index,disp=stop_disp)。 定時(shí)器設(shè)定與計(jì)時(shí)功能模塊 在此模塊中,并執(zhí)行計(jì)時(shí)動(dòng)作,取得計(jì)時(shí)終止指針,然后由計(jì)時(shí)終止指針標(biāo)示出計(jì)時(shí)終止?fàn)顟B(tài)led_stop =’1’。 to count from 0 to 24 hours and get full_hour u3:count24 port map(rst=rst,carry=full_min,times=n_hour,full=full_hour)。 to count from 0 to 60 seconds and get full_sec u1:count60 port map(rst=rst,carry=hz1,times=n_sec,full=full_sec)。 index of 60 minutes fully counted signal full_hour:std_logic。 normal_counting block的VHDL碼 normal counting from 00:00:00 to 23:59:59 normal_counting:block input : rst,clk output: hz1, n_sec,n_min,n_hour signal full_sec:std_logic。首先將1024Hz的系統(tǒng)輸入脈沖除以1024得到1Hz的基本秒輸入,然后除以60得分?jǐn)?shù),再除以60得小時(shí)數(shù),再除以24得一個(gè)滿日指針,節(jié)。 signal alarm_disp,stop_disp : std_logic。 signal hour7_one,hour7_ten: std_logic_vector(6 downto 0)。 signal hour_one,hour_ten: std_logic_vector(3 downto 0)。 signal hour :integer range 0 to 23。 signal s_hour: integer range 0 to 23。 signal a_hour: integer range 0 to 23。 signal n_hour: integer range 0 to 23。 architecture模塊的VHDL碼 architecture arch of clock is global signals flowing among different circuit blocks signal hz1:std_logic。諸如: hz1: 表示1s的時(shí)鐘; n_sec, n_min: 表示正常持續(xù)不斷的計(jì)數(shù)時(shí)間——分與秒,整數(shù)形式; n_hour: 表示正常持續(xù)不斷的計(jì)數(shù)時(shí)間——小時(shí),整數(shù)形式; a_sec, a_min: 表示鬧鐘的計(jì)數(shù)時(shí)間——分與秒,整數(shù)形式; a_hour: 表示鬧鐘的計(jì)數(shù)時(shí)間——時(shí),整數(shù)形式; s_sec, s_min: 表示定時(shí)器的計(jì)數(shù)時(shí)間——分與秒,整數(shù)形式; s_hour: 表示定時(shí)器的計(jì)數(shù)時(shí)間——小時(shí),整數(shù)形式; second, minute: 表示被選擇輸出的計(jì)數(shù)時(shí)間——分與秒,整數(shù)形式; hour: 表示被選擇輸出的計(jì)數(shù)時(shí)間——時(shí),整數(shù)形式; sec_one,sec_ten,min_one,min_ten:表示被選擇輸出的計(jì)數(shù)時(shí)間——分與秒的個(gè)位數(shù)與十位數(shù),BCD形式; hour_one, hour_ten:表示被選擇輸出的計(jì)數(shù)時(shí)間——小時(shí)的個(gè)位數(shù)與十位數(shù),BCD形式; sec7_one, sec7_ten, min7_one, min7_ten:表示被選擇輸出的計(jì)數(shù)時(shí)間——分與秒的個(gè)位數(shù)與十位數(shù),七段顯示器形式;hour7_one,hour7_ten:表示被選擇輸出的計(jì)數(shù)時(shí)間——分與秒的個(gè)位數(shù)與十位數(shù),七段顯示器形式; stop_index: 表示定時(shí)器計(jì)時(shí)終了的指針; alarm_index:表示鬧鐘的設(shè)定時(shí)間結(jié)束的指針。 architecture模塊 此處,我們定義一些功能模塊(blocks)間整體共享的傳遞信號(hào),以整合所有塊的功能。display hours p5,p6: out std_logic)。 display seconds and minutes p1,p2,p3,p4:out std_logic。 led to show alarm time reached led_stop: out std_logic。pushing button to tune minutes hour_tune: in std_logic。 push button to confirrn any setting operation sec_tune: in std_logic。dip switch for alarm setting stop: in std_logic。power reset to initialize clk: in std_logic。 use 。 use 。 entity模塊的VHDL碼 libray and package declaraction library ieee。 另外還有許多組件放在my_pkg程序包中,故應(yīng)加上一行: use 。 use 。 library ieee。 led_alarm: 表示鬧鐘設(shè)定時(shí)間已到; led_stop: 表示計(jì)時(shí)器計(jì)時(shí)結(jié)束; seg4: 分與秒的4個(gè)數(shù)字多路掃描輸出,可顯示在七段顯示器上; p1,p2,p3,p4: 分與秒4個(gè)數(shù)字的