LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY GUOGE IS
PORT(CLK,rst:in std_logic;
out_bit:OUT STD_LOGIC);
END GUOGE;
ARCHITECTURE PVJ OF GUOGE IS
SIGNAL time:STD_LOGIC_VECTOR(24 DOWNTO 0);
signal music21:STD_LOGIC_VECTOR(8 DOWNTO 0);
SIGNAL L5,L6,L7,M1,M2,M3,M4,M5,M6:STD_LOGIC_VECTOR(16 DOWNTO 0);
SIGNAL LL5,LL6,LL7,C,MM1,MM2,MM3,MM4,MM5,MM6,X0:STD_LOGIC;
BEGIN
process(clk,rst)
begin
X0<='1';
if clk'event and clk='1' then time<=time+1; --將20M頻率6分頻
if L5=12755 then L5<="00000000000000000";LL5<=not LL5;
else L5<=L5+1;
end if;
if L6=11363 then L6<="00000000000000000";LL6<=not LL6;
else L6<=L6+1;
end if;
if L7=10124 then L7<="00000000000000000";LL7<=not LL7;
else L7<=L7+1;
end if;
if M1=9555 then M1<="00000000000000000";MM1<=not MM1;
else M1<=M1+1;
end if;
if M2=8513 then M2<="00000000000000000";MM2<=not MM2;
else M2<=M2+1;
end if;
if M3=7584 then M3<="00000000000000000";MM3<=not MM3;
else M3<=M3+1;
end if;
if M5=6377 then M5<="00000000000000000";MM5<=not MM5;
else M5<=M5+1;
end if;
if M6=5681 then M6<="00000000000000000";MM6<=not MM6;
else M6<=M6+1;
end if;
if time=2499999 then time<="0000000000000000000000000";c<='1';
else c<='0';
end if;
end if;
end process;
PROCESS(c,RST)
BEGIN
if c'event and c='1' then music21<=music21+1;
if music21=255 then music21<="000000000";
end if;
end if;
if music21>=0 and music21<=1 then out_bit<=LL5;END IF;--低音5 起
if music21>=2 and music21<=7 then out_bit<=MM1;END IF;--中音1 來
if music21>=8 and music21<=9 then out_bit<=MM1;END IF;--中音1 不
if music21>=10 and music21<=13 then out_bit<=MM1;END IF;--中音1 愿做
if music21>=14 and music21<=15 then out_bit<=LL5;END IF;--低音5 奴
if music21=16 then out_bit<=LL6;END IF; --低音6 隸
if music21=17 then out_bit<=LL7;END IF; --低音7 的
if music21>=18 and music21<=25 then out_bit<=MM1;END IF;--中音1 人們
if music21>=26 and music21<=27 then out_bit<=X0;END IF;--休止符
if music21>=28 and music21<=29 then out_bit<=MM3;END IF;--中音3 把
if music21>=30 and music21<=31 then out_bit<=MM1;END IF;--中音1 我
if music21=32 then out_bit<=MM2;END IF; --中音2 們
if music21=33 then out_bit<=MM3;END IF; --中音3 的
if music21>=34 and music21<=41 then out_bit<=MM5;END IF;--中音5 血肉
if music21>=42 and music21<=45 then out_bit<=MM3;END IF;--中音3 筑成
if music21>=46 and music21<=47 then out_bit<=MM1;END IF;--中音1 我
if music21=48 then out_bit<=MM3;END IF; --中音3 們
if music21>=49 and music21<=51 then out_bit<=MM5;END IF;--中音5 新
if music21=52 then out_bit<=MM3;END IF; --中音3 的
if music21>=53 and music21<=56 then out_bit<=MM2;END IF;--中音2 長
if music21>=57 and music21<=64 then out_bit<=MM2;END IF;--中音2 城
if music21>=65 and music21<=68 then out_bit<=MM6;END IF;--中音6 中
if music21>=69 and music21<=72 then out_bit<=MM5;END IF;--中音5 華
if music21>=73 and music21<=76 then out_bit<=MM2;END IF;--中音2 民
if music21>=77 and music21<=80 then out_bit<=MM3;END IF;--中音3 族
if music21>=81 and music21<=82 then out_bit<=MM5;END IF;--中音5 到
if music21>=83 and music21<=86 then out_bit<=MM3;END IF;--中音3 了
if music21>=87 and music21<=88 then out_bit<=MM5;END IF;--中音5 最
if music21>=89 and music21<=90 then out_bit<=MM3;END IF;--中音3 危
if music21=91 then out_bit<=MM2;END IF; --中音2 險
if music21=92 then out_bit<=MM3;END IF; --中音3 的
if music21>=93 and music21<=94 then out_bit<=MM1;END IF;--中音1 時
if music21=95 then out_bit<=MM2;END IF; --中音2
if music21>=96 and music21<=99 then out_bit<=MM3;END IF;--中音3 候
if music21>=100 and music21<=103 then out_bit<=X0;END IF; --休止符
if music21>=104 and music21<=106 then out_bit<=MM5;END IF;--中音5 每
if music21=107 then out_bit<=MM6;END IF; --低音6 個
if music21>=108 and music21<=111 then out_bit<=MM1;END IF;--中音1 人被
if music21>=112 and music21<=115 then out_bit<=MM3;END IF;--中音3 迫著
if music21>=116 and music21<=119 then out_bit<=MM5;END IF;--中音5 發出
if music21>=120 and music21<=123 then out_bit<=MM2;END IF;--中音2 最后的
if music21>=124 and music21<=127 then out_bit<=LL6;END IF;--低音6 吼
if music21>=128 and music21<=133 then out_bit<=MM2;END IF;--中音2 聲
if music21>=134 and music21<=135 then out_bit<=LL5;END IF;--低音5 起
if music21>=136 and music21<=143 then out_bit<=MM1;END IF;--中音1 來!起
if music21>=144 and music21<=151 then out_bit<=MM3;END IF;--中音3 來!起
if music21>=152 and music21<=167 then out_bit<=MM5;END IF; --中音5 來!
if music21>=168 and music21<=170 then out_bit<=MM1;END IF; --中音1 我
if music21=171 then out_bit<=MM3;END IF; --中音3 們
if music21>=172 and music21<=175 then out_bit<=MM5;END IF; --中音5 萬眾
if music21>=176 and music21<=179 then out_bit<=MM6;END IF; --中音6 一
if music21>=180 and music21<=183 then out_bit<=MM5;END IF;--中音5 心
if music21>=184 and music21<=186 then out_bit<=MM3;END IF;--中音3 冒
if music21=187 then out_bit<=MM1;END IF; --中音1 著
if music21>=188 and music21<=191 then out_bit<=MM5;END IF;--中音5 敵人的
if music21>=192 and music21<=193 then out_bit<=MM3;END IF;----中音3 炮
if music21>=194 and music21<=195 then out_bit<=X0;END IF;
if music21>=196 and music21<=197 then out_bit<=MM1;END IF;--中音1 火
if music21>=198 and music21<=199 then out_bit<=X0;END IF;
if music21>=200 and music21<=203 then out_bit<=LL5;END IF;--低音5 前
if music21>=204 and music21<=207 then out_bit<=MM1;END IF;--中音1 進
if music21>=208 and music21<=210 then out_bit<=MM3;END IF;--低音3 冒
if music21=211 then out_bit<=MM1;END IF; --中音1 著
if music21>=212 and music21<=215 then out_bit<=MM5;END IF;--中音5 敵人的
if music21>=216 and music21<=217 then out_bit<=MM3;END IF;--中音3 炮
if music21>=218 and music21<=219 then out_bit<=X0;END IF;
if music21>=220 and music21<=221 then out_bit<=MM1;END IF;--中音1 火
if music21>=222 and music21<=223 then out_bit<=X0;END IF;
if music21>=224 and music21<=227 then out_bit<=LL5;END IF;--中音5 前
if music21>=228 and music21<=231 then out_bit<=MM1;END IF;--中音1 進
if music21>=232 and music21<=235 then out_bit<=LL5;END IF;--中音5 前
if music21>=236 and music21<=239 then out_bit<=MM1;END IF;--中音1 進
if music21>=240 and music21<=243 then out_bit<=LL5;END IF;--中音5 前
if music21>=244 and music21<=247 then out_bit<=MM1;END IF;--中音1 進
if music21>=248 and music21<=251 then out_bit<=MM1;END IF;--中音1 進
if music21>=252 and music21<=255 then out_bit<=X0;END IF;
IF RST='0' THEN MUSIC21<="000000000";END IF;
END PROCESS;
end;
交通信號燈程序:
十字路口的紅綠黃除以紅綠黃三色燈顯示通行信號外,現在還增加了以紅黃綠三色數字顯示等待和持續的時間。以下程序經調試即可實現既有三色燈,又有三色數字顯示的紅綠燈交通信號。
Library ieee;
use ieee.std_logic_1164.all;
use ieee.std_LOGIC_unsigned.all;
entity JTRGY is
port( clk,T:in std_logic;
EWR,EWG,EWY,SNR,SNG,SNY:OUT STD_LOGIC;
XS:OUT STD_LOGIC_VECTOR(6 DOWNTO 0);
EWRR0,EWGG0,EWYY0,SNRR0,SNGG0,SNYY0:OUT STD_LOGIC;
EWRR1,EWGG1,EWYY1,SNRR1,SNGG1,SNYY1:OUT STD_LOGIC);
END JTRGY;
ARCHITECTURE JT OF JTRGY IS
SIGNAL DISPLAY,Q0,Q1:STD_LOGIC_VECTOR(3 DOWNTO 0);
SIGNAL QQ:STD_LOGIC_VECTOR(5 DOWNTO 0);
SIGNAL C,EWRR,EWGG,EWYY,SNRR,SNGG,SNYY:STD_LOGIC;
SIGNAL XSJS:STD_LOGIC_VECTOR(7 DOWNTO 0);
SIGNAL TIME0:STD_LOGIC_VECTOR(24 DOWNTO 0);
BEGIN
PROCESS(CLK,T)
BEGIN
IF CLK'EVENT AND CLK='1' THEN TIME0<=TIME0+1;XSJS<=XSJS+1;
IF TIME0=9999999 THEN TIME0<="0000000000000000000000000";C<=NOT C;
END IF;
END IF;
----------------以下26秒計時---------------------------------
IF C'EVENT AND C='1' THEN Q0<=Q0-1;QQ<=QQ+1;
IF Q0=0 THEN Q0<="1001";Q1<=Q1-1;
END IF;
IF Q0=0 AND Q1=0 THEN Q0<="0101";Q1<="0010";
END IF;
--------------------以下52秒計時-----------------
IF QQ=51 THEN QQ<="000000";
END IF;
END IF;
--------------------------以下紅綠燈控制南北方向-------------------------------
IF QQ>=1 AND QQ<=26 THEN SNR<='1';SNG<='0';SNY<='0';SNRR<='0';ELSE SNRR<='1';END IF;
IF QQ>=27 AND QQ<=47 THEN SNR<='0';SNG<='1';SNY<='0';SNGG<='0';else SNGG<='1';END IF;
IF (QQ>=48 AND QQ<=51)or(QQ=0) THEN SNR<='0';SNG<='0';SNY<='1';SNYY<='0';
ELSE SNYY<='1';END IF;
--------------------------以下紅綠燈控制東西方向---------------------------
IF QQ>=1 AND QQ<=21 THEN EWR<='0';EWG<='1';EWY<='0';EWGG<='0';ELSE EWGG<='1';END IF;
IF QQ>=22 AND QQ<=26 THEN EWR<='0';EWG<='0';EWY<='1';EWYY<='0';ELSE EWYY<='1';END IF;
IF (QQ>=27 AND QQ<=51)OR(QQ=0) THEN EWR<='1';EWG<='0';EWY<='0';EWRR<='0';
ELSE EWRR<='1';END IF;
---------------------以下緊急情況雙向顯示紅燈----------------------------------
IF T='0' THEN
SNR<='1';SNG<='0';SNY<='0';
EWR<='1';EWG<='0';EWY<='0';
EWRR0<='0';EWGG0<='1';EWYY0<='1';SNRR0<='0';SNGG0<='1';SNYY0<='1';
EWRR1<='0';EWGG1<='1';EWYY1<='1';SNRR1<='0';SNGG1<='1';SNYY1<='1';
else
------------------南北方向紅黃綠燈--------------------------------
IF XSJS>=0 AND XSJS<127 THEN DISPLAY<=Q0;SNRR0<=('0' OR SNRR);
ELSE SNRR0<=('1' OR SNRR);
END IF;
IF XSJS>127 AND XSJS<255 THEN DISPLAY<=Q1;SNRR1<=('0' OR SNRR);
ELSE SNRR1<=('1' OR SNRR);
END IF;
IF XSJS>=0 AND XSJS<127 THEN DISPLAY<=Q0;SNGG0<=('0' OR SNGG);
ELSE SNGG0<=('1' OR SNGG);
END IF;
IF XSJS>127 AND XSJS<255 THEN DISPLAY<=Q1;SNGG1<=('0' OR SNGG);
ELSE SNGG1<=('1' OR SNGG);
END IF;
IF XSJS>=0 AND XSJS<127 THEN DISPLAY<=Q0;SNYY0<=('0' OR SNYY);
ELSE SNYY0<=('1' OR SNYY);
END IF;
IF XSJS>127 AND XSJS<255 THEN DISPLAY<=Q1;SNYY1<=('0' OR SNYY);
ELSE SNYY1<=('1' OR SNYY);
END IF;
-----------------------------------------------------------------
---------------東西方向紅黃綠燈--------------------------------
IF XSJS>=0 AND XSJS<127 THEN DISPLAY<=Q0;EWRR0<=('0' OR EWRR);
ELSE EWRR0<=('1' OR EWRR);
END IF;
IF XSJS>127 AND XSJS<255 THEN DISPLAY<=Q1;EWRR1<=('0' OR EWRR);
ELSE EWRR1<=('1' OR EWRR);
END IF;
IF XSJS>=0 AND XSJS<127 THEN DISPLAY<=Q0;EWGG0<=('0' OR EWGG);
ELSE EWGG0<=('1' OR EWGG);
END IF;
IF XSJS>127 AND XSJS<255 THEN DISPLAY<=Q1;EWGG1<=('0' OR EWGG);
ELSE EWGG1<=('1' OR EWGG);
END IF;
IF XSJS>=0 AND XSJS<127 THEN DISPLAY<=Q0;EWYY0<=('0' OR EWYY);
ELSE EWYY0<=('1' OR EWYY);
END IF;
IF XSJS>127 AND XSJS<255 THEN DISPLAY<=Q1;EWYY1<=('0' OR EWYY);
ELSE EWYY1<=('1' OR EWYY);
END IF;
end if;
---------------------------------------------------------------
CASE DISPLAY IS
WHEN "0000"=>XS<="0000001";
WHEN "0001"=>XS<="1001111";
WHEN "0010"=>XS<="0010010";
WHEN "0011"=>XS<="0000110";
WHEN "0100"=>XS<="1001100";
WHEN "0101"=>XS<="0100100";
WHEN "0110"=>XS<="0100000";
WHEN "0111"=>XS<="0001111";
WHEN "1000"=>XS<="0000000";
WHEN "1001"=>XS<="0000100";
WHEN OTHERS=>XS<="0000001";
END CASE;
END PROCESS;
END;