📄 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 + -