【正文】
設定 clk 信號的震蕩頻率為 1024HZ,定義輸出全局信號 sample 并在 block 內定義一個局部信號 q 為 5 計數(shù)器,作為除以 2^5=32 的分額器。 sample ﹤ =q(4)and notdelay。 end if。osc/2^5=32hz(). begin process(clk). begin if rising_edge(clk) then. delay=q(4)。 declaraction of self_built sampling pulse from clock. begin sample signal generation. sampling_signal:block. input:clk. output:sample. signal delay :std_logic。下面列出sampling_signal 模塊的程序代碼。 definition of signal_flow in entity debounce. 我們假定一般按鍵信號的主脈沖寬度為 100ms 左右,而機械按鍵抖動噪聲寬度不大于 30ms,為了計算真正的按鍵次數(shù),必須過濾機械抖動噪聲。push button signal about 100ms period. push_out:out std_logic)。 i/o signal declaraction. entity debounce is. pro(clk:in std_logicl。 use 。 Deboune 模塊的 VHDL 碼 content definition of ponent “ debounce” . library ieee。信號仿真結果如圖 410 所示: 38 圖 出貨并計算存貨電路仿真圖 debouncing 電路模塊 抖動消除電路的程序 debouncing被放在 my_pkg的程序包中,當作組件調用 ,在連接合成時,必須同時附掛在 cola 主程序下才能正常運行。另外,局部信號 no_cola 與 no_diet 僅用來記錄現(xiàn)存飲料數(shù)量,并不輸出到模塊外。 end process。 end if。 to show diet empty status . else led_diet_ok=’ 1’ 。 elsif rising_edge(diet_out) then . no_diet=no_diet1。 diet:process(reset_out)to book the bottle remaining diet. begin . if reset=’ 1’ then no_diet=20。 37 eng if。 end if。 if no_cola=0 then led_cola_ok=’ 0’ 。 led_cola_ok=’ 1’ 。 to deliver diet if nesscessary. led_diet_out=diet_out。 to deliver cola if necessary. led_cola_out=cola_out。 bottle remaining cola. signalno_diet : integer range 0 to 20。 give_check 模塊的 VHDL 碼 to deliver the selected drink and book the remaining number give_check:block. 36 input:,. output 。系統(tǒng)輸入信號 reset為所有模塊的重置信號。如果操作期間按 了取消鍵,則退幣燈全亮,并立即退還所有硬幣,信號仿真結果如圖 ,圖 所示: 35 圖 退幣處理電路仿真圖 圖 找零處理電路仿真圖 出貨并計算存貨電路模塊 ( give_check) 當輸入全局信號 ok,money_ok 與 cola_choice/diet_choice 均為 1時,則確定輸出全局信號 cola_out/diet_out 表示 cola/diet 可出貨,并點亮系統(tǒng)輸出信號 led_cola_out/led_diet_out。而且也退出一枚 5 元硬幣( led_five_retuen0 =’ 1’)。另外,局部信號 total_amount 僅用來暫存投入硬幣的總金額,并不輸出到模塊外,正常交易時的系統(tǒng)反應。 end block。 end if。 led_ten_return(1)=return_clk。 led_five_return(1)= return_clk。 when25=led_ten_returen(0)= return_clk。 when15=null。 for i in 0 to 1 loop. led_ten_return(i)=return_clk。 elsif(diet_out=’ 1’ or cola_out=’ 1’ ) then. return coins after drink delivered. case total_amount is. 34 when 0 to 14 =for i in 0 to 2 loop. led_five_return(i)=return_clk。 for i in 0 to 1 loop. led_ten_return(i)=return_clk。 if(cancel=’ 1’ ) then if canceled_return all coins mmediately. for i in 0 to 2 loop. led_five_return(i)=return_clk。 else money_ok=’ 0’ 。 elsif rising_edge(clk) then. total_amount=total_amount_ten+total_amount_five。 led_five_return=(others=’ 0’ )。 begin process(). begin if reset=’ 1’ then total_amount=’ 0’ 。下面列出 coin_rerumed 模塊的程序代碼。定義全局信號return_clk 控制退幣警示燈。此信號 total_amount 為全局信號,僅供此處判斷而不做輸出,若投入金額超過 15 元,則必須輸出全局信號 money_ok,以便其他行為的判別。模塊輸入有 clk與 reset 兩個系統(tǒng)輸入信號, led_five_return 與 led_ten_return 為系統(tǒng)輸入信號,顯示被退飲料種類對應的 LED 燈及數(shù)量。 32 設定 ok_buy,cancel_buy 與 reset 三個系統(tǒng)輸入信號,可觀察到系統(tǒng)輸出信號 led_ok,led_cancel 與輸出全局信號 ok 與 cancel 的波形, ok 與 cancel 信號用來維持 ok_buy 與 cancel_buy 按鍵操作的狀態(tài)。 end if1. end process。 elsif rising_edge(cancel_buy) then. cancel=’ 1’ 。 ok or cancle 模塊的 VHDL 碼 to check confiming or canceling buying ok or cancle block input reset ok buy cancle buy output led buy ok led cancel cancel begin pl: process( reset ok buy) to maintain the confiming status begin if reset=‘ 1’ than ok﹤ =0 led buy ﹤ =0 els if rising dege( ok buy`) then ok﹤ =1 led buy ﹤ =1 end if; end prcess p2 process ( reset cancle buy ) tomaitain the canceling status begin if reset﹤ =1。同時還需要輸入系統(tǒng)信號 reset ok buy 與 cancle buy,然 31 后設定輸出系統(tǒng)信號 led buy 與 led cancle,用來對外顯示購買或取消狀態(tài)。 圖 飲料選擇處理電路仿真圖 確認與取消處理電路模塊( ok or cancle) 按了購買確認鍵或取消鍵后,系統(tǒng)將據(jù)此處理交易與反應。同理,下次的飲料選取按鍵總是可以取代前一次的選擇。當按下 select cola 鍵彈回時,讓 cola choice 維持 1,作為選定 cola 的指針,同時 led cola sel 燈亮,表示 cola 被選中。下面列出色了傳統(tǒng) drink模塊的程序代碼。模塊有 clk reset select_cola 與 select_diet 四個系統(tǒng)輸入信號, led cola sel 與 led diet sel為系統(tǒng)輸出信號,顯示該飲料被選。 if no coin five then led ten(no coin five)﹤ =‘ 1’; no coin five﹤ =3; end if; end if; end process; end block; 設定 clk, reset 與 coin 5 三個系統(tǒng)輸入信號后,可觀察到系統(tǒng)輸出信號 29 led five 以及全局信號 total amount 5 與 no coin five 3 位 led five 信號為“ 000”﹥“ 001”﹥“ 011”﹥“ 111” ,3 次投幣點亮了 3 個 LED,投入金額分別表示 0,5 元 ,10 元和 15 元。下面列出 coin 5counting 模塊的程序代碼。模塊輸入信號有 clk 與 coin 5 兩個系統(tǒng)輸入信號, led five 為系統(tǒng)輸出信號,輸出 tot