?? elevator.txt
字號:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
entity elevator is
port(up1,up2,up3,up4:in std_logic;--上樓信號
down2,down3,down4,down5:in std_logic;--下樓信號
b1,b2,b3,b4,b5:in std_logic;--電梯內目的樓層信號
opendoor,closedoor:in std_logic;--電梯內開關門信號
clk:in std_logic;--時鐘信號
mode:out std_logic_vector(1 downto 0);--模式
floor:out std_logic_vector(4 downto 0);--所在樓層
up,down:out std_logic;--給電機的上升或下降信號
door:out std_logic;--給電機的開或關門信號
q:out std_logic);--發音信號
end elevator;
architecture behave of elevator is
type state_type is(upward,downward,dullA,dullB);--dullA是由上升模式而來,dullB由下降模式而來
signal WLA,WLB:std_logic_vector(5 downto 1);--WLA,WLB是正在執行的工作表
variable state:state_type;
variable countA,countB:integer:=0;
signal ceng:std_logic_vector(5 downto 1);
variable x,y:std_logic;
begin
WLA(1)<='1' when up1='1' ;
WLA(2)<='1' when up2='1';
WLA(3)<='1' when up3='1';
WLA(4)<='1' when up4='1';
WLB(5)<='1' when down5='1';
WLB(4)<='1' when down4='1';
WLB(3)<='1' when down3='1';
WLB(2)<='1' when down2='1';
if b1='1' then
if ceng>"00001" then WLA(1)<='1';
else WLB(1)<='1';
end if;
end if;
if b2='1' then
if ceng>"00010" then WLA(2)<='1';
else WLB(2)<='1';
end if;
end if;
if b3='1' then
if ceng>"00100" then WLA(3)<='1';
else WLB(3)<='1';
end if;
end if;
if b4='1' then
if ceng>"01000" then WLA(4)<='1';
else WLB(4)<='1';
end if;
end if;
if b5='1' then
if ceng>"10000" then WLA(5)<='1';
else WLB(5)<='1';
end if;
end if;
process(clk)
begin
if clk'event and clk='1' then
case state is
when upward=>
if ((WLA and ceng)=ceng or WLA="00000") then--判斷是否要在當前層停
state:=dullA;
else state:=upward;
end if
if y='1' then
if ceng>0 then--底層有無上樓請求
down<='1';
if countA<2E6 then countA:=countA+1;
else countA:=0;ceng<=ceng-1;
end if;
else down<='0';
y:='0';
end if;
else
mode<="01";--顯示上升模式
up<='1';
floor<=ceng;
if countA<2E6 then --每1秒樓層數加一
countA:=countA+1;
else ceng<=ceng+1;
countA:=0;
end if;
end if;
when downward =>
if (WLB and ceng)=ceng or WLB="00000" then
state:=dullB;
else state:=downward;
end if
if x='1' then
if WLA>(ceng+ceng) then--高層有無下樓請求
up<='1';
if countA<2E6 then countA:=countA+1;
else countA:=0;ceng<=ceng+1;
end if;
else up<='0';--高層無請求,直接轉到下降模式
x:='0';
end if;
else
mode <="10";--顯示下降模式
down<='1';
if countA<2E6 then
countA:=countA+1;
else ceng<=ceng-1;
countA:=0;
end if;
end if;
floor<=ceng;
when dullA =>--由上升模式到停
if (WLA and ceng)=ceng then
mode <="00";
up<='0';
down<='0';
door<='1';
if countB<8E6 then--開門時間為四秒
countB:=countB+1;
else door<='0';
countB:=0;
end if;
if closedoor='1' then
door<='0';
end if;
if opendoor='1' then
door<='1';
countB:=0;
end if;
else
WLA<=WLA and (not ceng);--清除當前層的記錄
if WLA>=(ceng+ceng) then state:=upward;--高層有無上樓請求
else state:=downward;x:='1';
end if;
end if;
when dullB => --由下降模式到停
if(WLB and ceng)=ceng then
mode <="00";
up<='0';
down<='0';
door<='1';
if countB<8E6 then
countB:=countA+1;
else door<='0';
countB:=0;
end if;
if closedoor='1' then
door<='0';
end if;
if opendoor='1' then
door<='1';
countB:=0;
end if;
else
WLB<=WLB and (not ceng);
if ceng>0 then state:=downward;--底層有無下樓請求
else state:=upward;y:='1';
end if;
end if;
end case;
end if;
end process;
end behave;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -