?? jiaotongdeng.txt
字號(hào):
----設(shè)計(jì)一個(gè)十字路口的紅、綠、黃三色信號(hào)交通燈控制電路
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith;
entity jiaoton is
port(reset,s,clk_in:in std_logic; ------reset為置位信號(hào),clk_in為50MHZ時(shí)鐘,
-------s為特殊狀態(tài)信號(hào)
smga1,smga2,smgb1,smgb2:out std_logic_vector(6 downto 0);
outa:out std_logic_vector(5 downto 0));-------主(紅、綠、黃)和支(紅、綠、黃)
end entity ;
architecture one of jiaoton is
-------------------------------定義一個(gè)四種狀態(tài)的狀態(tài)機(jī)
TYPE FSM_ST IS(s0,s1,s2,s3);
signal current_state : FSM_ST:=s0;
----------------------------------------------------------------------
signal sma2,smb2:integer range 0 to 9;
signal sma1:integer range 0 to 5;
signal smb1:integer range 0 to 5;
signal sansuo,clk:bit;
begin
-------------------------------------50MHZ的信號(hào)進(jìn)行分頻
process(clk_in)
variable fenpin:integer range 0 to 25000000;
begin
if clk_in'event and clk_in='1' then
if fenpin=0 then fenpin:=25000000;clk<=not(clk);
else fenpin:=fenpin-1;
end if;
end if;
end process;
---------------------------------clk為分頻后的秒鐘時(shí)鐘信號(hào)
process(clk,reset)
variable tc: integer range 0 to 5;
variable tb: integer range 0 to 55;
variable ta: integer range 0 to 50;
begin
if reset='1' then ta:=50;tb:=55;tc:=0;current_state<=s0;-------開始的時(shí)候給主干的計(jì)數(shù)器賦初值---------50,給支干的計(jì)數(shù)器賦值55。
elsif clk'event and clk='1'then-------------------------------有輸入時(shí)鐘出發(fā)后,計(jì)數(shù)器開始計(jì)數(shù)。
if s='0'then
case current_state is--------如果沒有突發(fā)狀況的話,執(zhí)行下面程序
when s0 => outa<="010100";--------主干綠燈亮50秒,支干亮紅燈。然后賦給下--------個(gè)狀態(tài)值,繼續(xù)計(jì)數(shù)
if ta=0 then current_state<=s1;tc:=5;ta:=5;
else ta:=ta-1;tb:=tb-1;
end if;
when s1=>outa<="001100";------------主干亮黃燈5秒,支干繼續(xù)亮紅燈,然后------------繼續(xù)下個(gè)狀態(tài)。
if tc=0 then current_state<=s2; tb:=30;ta:=35;
else tc:=tc-1;ta:=ta-1;tb:=tb-1;
end if;
when s2=>outa<="100010"; ---------------主干亮紅燈,支干亮綠燈30秒
if tb=0 then current_state<=s3;tc:=5;tb:=5;
else tb:=tb-1;ta:=ta-1;
end if;
when s3=>outa<="100001";-----------------主干亮紅燈,支干亮黃燈5秒,然后又------把第一個(gè)狀態(tài)的值賦給crurrent_state,-------繼續(xù)循環(huán)下去
if tc=0 then current_state<=s0;ta:=50;tb:=55;
else tc:=tc-1;ta:=ta-1;tb:=tb-1;
end if;
when others=>current_state<=s0;ta:=50;tb:=55;
end case;
else outa<="100100";sansuo<=not(sansuo);------如果有突發(fā)狀況時(shí),讓等亮亮滅--------滅地不行閃爍
end if;
end if;
--------------------通過取模,取余,可以得到計(jì)數(shù)的十位和個(gè)位,然后通過數(shù)碼管來顯示時(shí)間
sma1<=ta/10;
sma2<=ta rem 10;
smb1<=tb/10;
smb2<=tb rem 10;
end process;
----------顯示主干時(shí)間的地位
process(sma2,sansuo,s)
begin
if s='1' and sansuo='1'then smga2<="1111111";
else
case sma2 is
when 0 => smga2<="1000000"; --------其中數(shù)碼管的高位在前面,地位在后面
when 1 => smga2<="1111001"; -------0表示該段數(shù)碼管亮,1表示滅
when 2 => smga2<="0100100";
when 3 => smga2<="0110000";
when 4 => smga2<="0011001";
when 5 => smga2<="0010010";
when 6 => smga2<="0000010";
when 7 => smga2<="1111000";
when 8 => smga2<="0000000";
when 9 => smga2<="0010000";
when others => smga2<="1111111";
end case;
end if;
end process;
--------------------顯示主干時(shí)間的高位
process(sma1,s,sansuo)
begin
if s='1' and sansuo='1'then smga1<="1111111";
else
case sma1 is
when 0 => smga1<="1000000";
when 1 => smga1<="1111001";
when 2 => smga1<="0100100";
when 3 => smga1<="0110000";
when 4 => smga1<="0011001";
when 5 => smga1<="0010010";
when others => smga1<="1111111";
end case;
end if;
end process;
----------------------------顯示支干時(shí)間的地位
process(smb2,s,sansuo)
begin
if s='1' and sansuo='1'then smgb2<="1111111";
else
case smb2 is
when 0 => smgb2<="1000000";
when 1 => smgb2<="1111001";
when 2 => smgb2<="0100100";
when 3 => smgb2<="0110000";
when 4 => smgb2<="0011001";
when 5 => smgb2<="0010010";
when 6 => smgb2<="0000010";
when 7 => smgb2<="1111000";
when 8 => smgb2<="0000000";
when 9 => smgb2<="0010000";
when others => smgb2<="1111111";
end case;
end if;
end process;
--------------------------------顯示支干時(shí)間的高位
process(smb1,s,sansuo)
begin
if s='1' and sansuo='1'then smgb1<="1111111";
else
case smb1 is
when 0 => smgb1<="1000000";
when 1 => smgb1<="1111001";
when 2 => smgb1<="0100100";
when 3 => smgb1<="0110000";
when 4 => smgb1<="0011001";
when 5 => smgb1<="0010010";
when others => smgb1<="1111111";
end case;
end if;
end process;
end one;
-----------------程序結(jié)束
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -