资源描述
LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE ieee.std_logic_arith.all;
USE ieee;std_logic_unsigned.all;
ENTITY lift IS
port(buttonclk : IN STD_LOGIC; --按键时钟
liftclk : IN STD_LOGIC; --电梯时钟
reset : in STD_LOGIC; --异步复位
input : in std_logic;
up : in std_logic_vector(3 downto 0);
down : in std_logic_vector(3 downto 0);
stop: in std_logic_vector(3 downto 0);
fuplight : buffer std_logic_vector(8 downto 1);--电梯外部上升请求指示灯
fdnlight: buffer std_logic_vector(8 downto 1);--电梯外部下降请求指示灯
stoplight: buffer std_logic_vector(8 downto 1);--电梯内部各层请求指示灯
position: buffer integer range 1 to 8;--电梯位置指示
doorlight: out std_logic;--电梯开关指示灯
udsig: buffer std_logic);--电梯升降指示
end lift;
architecture a of lift is
TYPE lift_state is
(stopon1,dooropen,doorclose,doorwait1,doorwait2,doorwait3,doorwait4,up,down,stop);
signal mylift: lift_state;
signal clearup: STD_LOGIC;--用于清除上升请求指示灯的信号
signal cleardn: STD_LOGIC;--用于清除下降请求指示灯的信号
signal f1upbutton: std_logic;
signal f2upbutton: std_logic;
signal f2dnbutton: std_logic;
signal f3dnbutton: std_logic;
signal f3upbutton: std_logic;
signal f4dnbutton: std_logic;
signal f4upbutton: std_logic;
signal f5dnbutton: std_logic;
signal f5upbutton: std_logic;
signal f6dnbutton: std_logic;
signal f6upbutton: std_logic;
signal f7dnbutton: std_logic;
signal f7upbutton: std_logic;
signal f8dnbutton: std_logic;
signal stop1button,stop2button,stop3button,stop4button,stop5button,stop6button,stop7button,stop8button: std_logic;
begin
ctrlift: process(reset,liftclk)--控制电梯状态的进程
variable poss: integer range 8 downto 1;--变量poss用于表示电梯的位置
begin
if reset='1' then --异步复位信号如果为1时电梯的状态
mylift<=stopon1; --如果电梯停留在第一层待机
clearup<='0'; --清除上升信号
cleardn<='0'; --清除下降信号
else --电梯处于正常工作情况
if liftclk'event and liftclk='1' then --电梯时钟上升沿触发
case mylift IS
when stopon1 => --处于电梯停留在一层的状态
doorlight<='1'; --开门指示灯亮表示开门
position<=1;poss:=1; --电梯位置为一层
mylift<=doorwait1; --状态转移到开门等待第一秒状态
when doorwait1 => --处于开门等待第一秒状态
mylift<=doorwait2; --状态转移到开门等待第二秒状态
when doorwait2 => --处于开门等待第二秒状态
clearup<='0'; --清除本层上升请求
cleardn<='0'; --清除本层下降请求
mylift<=doorwait3; --状态转移到开门等待第三秒状态
when doorwait3 => --处于开门等待第三秒状态
mylift<=doorwait4; --状态转移到开门等待第四秒状态
when doorwait4 => --处于开门等待第四秒状态
mylift<=doorclose; --状态转移到关门状态
when doorclose => --处于关门状态
doorlight<='0'; --开门指示灯灭,表示关门
if udsig='0' then --udsig=0表示上升模式
if position=8 then --如果电梯在第八层
if (stoplight="00000000" and fuplight="00000000") and fdnlight="00000000" then --没有任何请求信号
udsig<='1'; --udsig置1
mylift<=doorclose; --电梯处于关门状态
else udsig<='1'; mylift<=down;--无论什么请求电梯都要下降
end if;
elsif position=7 then --如果电梯在第七层
if stoplight="00000000" and fuplight="00000000" and fdnlight="00000000" then --没有任何请求信号
udsig<='0'; mylift<=doorclose; --电梯回关门状态等待升降请求
elsif stoplight(8)='1' OR (stoplight(8)='0' and fdnlight(8)='1') then --如果内部有8层停站请求或有8层下降请求
udsig<='0'; mylift<=up; --电梯上升
else udsig<='1'; mylift<=down; --其他任何情况udsig=1,电梯下降
end if;
elsif position=6 then --如果电梯在第六层
if stoplight="00000000" and fuplight="00000000" and fdnlight="00000000" then udsig<='0'; --如果没有任何请求信号,电梯处于上升模式
mylift<=doorclose; --电梯回关门状态等待升降请求
elsif stoplight(7)='1' OR (stoplight(7)='0' and (fdnlight(7)='1' or fuplight(7)='1')) then --如果内部有7层停站请求或有7层上升、下降请求
udsig<='0'; mylift<=up; --电梯上升
elsif stoplight(8)='1' OR (stoplight(8)='0' and fdnlight(8)='1') then --如果内部有8层停站请求或有8层下降请求
udsig<='0';mylift<=up; --电梯上升
else udsig<='1'; mylift<=down; --其他任何情况udsig=1,电梯下降
end if;
elsif position=5 then --如果电梯在第五层
if stoplight="00000000" and fuplight="00000000" and fdnlight="00000000" then udsig<='0'; --如果没有任何请求信号,电梯处于上升模式
mylift<=doorclose; --电梯回关门状态等待升降请求
elsif stoplight(6)='1' OR (stoplight(6)='0' and (fdnlight(6)='1' or fuplight(6)='1')) then --如果内部有6层停站请求或有6层上升、下降请求
udsig<='0';mylift<=up; --电梯上升
elsif stoplight(7)='1' OR (stoplight(7)='0' and (fdnlight(7)='1' or fuplight(7)='1')) then --如果内部有7层停站请求或有7层上升、下降请求
udsig<='0';mylift<=up; --电梯上升
elsif stoplight(8)='1' OR (stoplight(8)='0' and fdnlight(8)='1') then --如果内部有8层停站请求或有8层下降请求
udsig<='0';mylift<=up; --电梯上升
else udsig<='1'; mylift<=down; --其他任何情况udsig=1,电梯下降
end if;
elsif position=4 then --如果电梯在第四层
if stoplight="00000000" and fuplight="00000000" and fdnlight="00000000" then udsig<='0'; --如果没有任何请求信号,电梯处于上升模式
mylift<=doorclose; --电梯回关门状态等待升降请求
elsif stoplight(5)='1' OR (stoplight(5)='0' and (fdnlight(5)='1' or fuplight(5)='1')) then --如果内部有5层停站请求或有5层上升、下降请求
udsig<='0';mylift<=up; --电梯上升
elsif stoplight(6)='1' OR (stoplight(6)='0' and (fdnlight(6)='1' or fuplight(6)='1')) then --如果内部有6层停站请求或有6层上升、下降请求
udsig<='0';mylift<=up; --电梯上升
elsif stoplight(7)='1' OR (stoplight(7)='0' and (fdnlight(7)='1' or fuplight(7)='1')) then --如果内部有7层停站请求或有7层上升、下降请求
udsig<='0';mylift<=up; --电梯上升
elsif stoplight(8)='1' OR (stoplight(8)='0' and fdnlight(8)='1')then --如果内部有8层停站请求或有8层下降请求
udsig<='0';mylift<=up; --电梯上升
else udsig<='1'; mylift<=down; --其他任何情况udsig=1,电梯下降
end if;
elsif position=3 then --如果电梯在第四层
if stoplight="00000000" and fuplight="00000000" and fdnlight="00000000" then
udsig<='0';
mylift<=doorclose;
elsif stoplight(4)='1' OR (stoplight(4)='0' and (fdnlight(4)='1' or fuplight(4)='1')) then
udsig<='0';
mylift<=up;
elsif stoplight(5)='1' OR (stoplight(5)='0' and (fdnlight(5)='1' or fuplight(5)='1')) then
udsig<='0';
mylift<=up;
elsif stoplight(6)='1' OR (stoplight(6)='0' and (fdnlight(6)='1' or fuplight(6)='1')) then
udsig<='0';
mylift<=up;
elsif stoplight(7)='1' OR (stoplight(7)='0' and (fdnlight(7)='1' or fuplight(7)='1')) then
udsig<='0';
mylift<=up;
elsif stoplight(8)='1' OR (stoplight(8)='0' and fdnlight(8)='1') then
udsig<='0';
mylift<=up;
else udsig<='1'; mylift<=down;
end if;
elsif position=2 then
if stoplight="00000000" and fuplight="00000000" and fdnlight="00000000" then
udsig<='0';
mylift<=doorclose;
elsif stoplight(3)='1' OR (stoplight(3)='0' and (fdnlight(3)='1' or fuplight(3)='1')) then
udsig<='0';
mylift<=up;
elsif stoplight(4)='1' OR (stoplight(4)='0' and (fdnlight(4)='1' or fuplight(4)='1')) then
udsig<='0';
mylift<=up;
elsif stoplight(5)='1' OR (stoplight(5)='0' and (fdnlight(5)='1' or fuplight(5)='1')) then
udsig<='0';
mylift<=up;
elsif stoplight(6)='1' OR (stoplight(6)='0' and (fdnlight(6)='1' or fuplight(6)='1')) then
udsig<='0';
mylift<=up;
elsif stoplight(7)='1' OR (stoplight(7)='0' and (fdnlight(7)='1' or fuplight(7)='1')) then
udsig<='0';
mylift<=up;
elsif stoplight(8)='1' OR (stoplight(8)='0' and fdnlight(8)='1') then
udsig<='0';
mylift<=up;
else udsig<='1'; mylift<=down;
end if;
elsif position=1 then
if stoplight="00000000" and fuplight="00000000" and fdnlight="00000000" then
udsig<='0';
mylift<=doorclose;
else udsig<='0'; mylift<=up;
end if;
end if;
else ------------------------
if position=1 then
if stoplight="00000000" and fuplight="00000000" and fdnlight="00000000" then
udsig<='0';
mylift<=doorclose;
else udsig<='0'; mylift<=up;
end if;
elsif position=2 then
if stoplight="00000000" and fuplight="00000000" and fdnlight="00000000" then
udsig<='1';
mylift<=doorclose;
elsif stoplight(1)='1' OR (stoplight(1)='0' and fuplight(1)='1')
then
udsig<='1';
mylift<=down;
else udsig<='0'; mylift<=up;
end if;
elsif position=3 then
if stoplight="00000000" and fuplight="00000000" and fdnlight="00000000" then
udsig<='1';
mylift<=doorclose;
elsif stoplight(2)='1' OR (stoplight(2)='0' and (fuplight(2)='1' or fdnlight(2)='1'))
then udsig<='1';mylift<=down;
elsif stoplight(1)='1' OR (stoplight(1)='0' and fuplight(1)='1')
then udsig<='1';mylift<=down;
else udsig<='0'; mylift<=up;
end if;
elsif position=4 then
if stoplight="00000000" and fuplight="00000000" and fdnlight="00000000" then
udsig<='1';
mylift<=doorclose;
elsif stoplight(3)='1' OR (stoplight(3)='0' and (fuplight(3)='1' or fdnlight(3)='1'))
then udsig<='1';mylift<=down;
elsif stoplight(2)='1' OR (stoplight(2)='0' and (fuplight(2)='1' or fdnlight(2)='1'))
then udsig<='1';mylift<=down;
elsif stoplight(1)='1' OR (stoplight(1)='0' and fuplight(1)='1')
then udsig<='1';mylift<=down;
else udsig<='0'; mylift<=up;
end if;
elsif position=5 then
if stoplight="00000000" and fuplight="00000000" and fdnlight="00000000" then
udsig<='1';
mylift<=doorclose;
elsif stoplight(4)='1' OR (stoplight(4)='0' and (fuplight(4)='1' or fdnlight(4)='1'))
then udsig<='1';mylift<=down;
elsif stoplight(3)='1' OR (stoplight(3)='0' and (fuplight(3)='1' or fdnlight(3)='1'))
then udsig<='1';mylift<=down;
elsif stoplight(2)='1' OR (stoplight(2)='0' and (fuplight(2)='1' or fdnlight(2)='1'))
then udsig<='1';mylift<=down;
elsif stoplight(1)='1' OR (stoplight(1)='0' and fuplight(1)='1')
then udsig<='1';mylift<=down;
else udsig<='0'; mylift<=up;
end if;
elsif position=6 then
if stoplight="00000000" and fuplight="00000000" and fdnlight="00000000" then
udsig<='1';
mylift<=doorclose;
elsif stoplight(5)='1' OR (stoplight(5)='0' and (fuplight(5)='1' or fdnlight(5)='1'))
then udsig<='1';mylift<=down;
elsif stoplight(4)='1' OR (stoplight(4)='0' and (fuplight(4)='1' or fdnlight(4)='1'))
then udsig<='1';mylift<=down;
elsif stoplight(3)='1' OR (stoplight(3)='0' and (fuplight(3)='1' or fdnlight(3)='1'))
then udsig<='1';mylift<=down;
elsif stoplight(2)='1' OR (stoplight(2)='0' and (fuplight(2)='1' or fdnlight(2)='1'))
then udsig<='1';mylift<=down;
elsif stoplight(1)='1' OR (stoplight(1)='0' and fuplight(1)='1')
then udsig<='1';mylift<=down;
else udsig<='0'; mylift<=up;
end if;
elsif position=7 then
if stoplight="00000000" and fuplight="00000000" and fdnlight="00000000" then
udsig<='1';
mylift<=doorclose;
elsif stoplight(6)='1' OR (stoplight(6)='0' and (fuplight(6)='1' or fdnlight(6)='1'))
then udsig<='1';mylift<=down;
elsif stoplight(5)='1' OR (stoplight(5)='0' and (fuplight(5)='1' or fdnlight(5)='1'))
then udsig<='1';mylift<=down;
elsif stoplight(4)='1' OR (stoplight(4)='0' and (fuplight(4)='1' or fdnlight(4)='1'))
then udsig<='1';mylift<=down;
elsif stoplight(3)='1' OR (stoplight(3)='0' and (fuplight(3)='1' or fdnlight(3)='1'))
then udsig<='1';mylift<=down;
elsif stoplight(2)='1' OR (stoplight(2)='0' and (fuplight(2)='1' or fdnlight(2)='1'))
then udsig<='1';mylift<=down;
elsif stoplight(1)='1' OR (stoplight(1)='0' and fuplight(1)='1')
then udsig<='1';mylift<=down;
else udsig<='0'; mylift<=up;
end if;
elsif position=8 then
if stoplight="00000000" and fuplight="00000000" and fdnlight="00000000" then
udsig<='1';
mylift<=doorclose;
else udsig<='1'; mylift<=down;
end if;
end if;
end if;------------
when up =>
position<=position+1;
poss:=poss+1;
if poss=8 and (stoplight(poss)='1' OR fdnlight(poss)='1')
then mylift<=stop;
elsif poss<8 and (stoplight(poss)='1' or fuplight(poss)='1')
then mylift<=stop;
--elsif((fdnlight(poss)='1' and fuplight(poss+1 to 8)=0 and) fdnlight(poss+1 to 8)=0)
--then mylift<=stop;
Else mylift<=doorclose;
end if;
when down =>
position<=position-1;
poss:=poss-1;
if poss>1 and (stoplight(poss)='1' OR fdnlight(poss)='1')
then mylift<=stop;
elsif poss=1 and (stoplight(poss)='1' OR fuplight(poss)='1')
then mylift<=stop;
Else mylift<=doorclose;
end if;
when stop =>
mylift<=dooropen;
when dooropen=>
doorlight<='1';
if udsig='0' then
if position<=7 and (stoplight(position)='1' OR fuplight(position)='1') then
clearup<='1';
else clearup<='1'; cleardn<='1';
end if;
elsif udsig='1' then
if position>=2 and (stoplight(position)='1' OR fdnlight(position)='1') then
cleardn<='1';
else clearup<='1'; cleardn<='1';
end if;
end if;
mylift<=doorwait1;
end CASE;
end if;
end if;
end process ctrlift;
process(reset,buttonclk)
begin
if reset='1' then
f1upbutton<='0';f2upbutton<='0';
f2dnbutton<='0'; f3dnbutton<='0'; f3upbutton<='0';
f4dnbutton<='0'; f4upbutton<='0'; f5dnbutton<='0'; f5upbutton<='0';
f6dnbutton<='0'; f6upbutton<='0'; f7dnbutton<='0'; f7upbutton<='0';
f8dnbutton<='0'; stop1button<='0';stop2button<='0';
stop3button<='0';stop4button<='0';
stop5button<='0';stop6button<='0';
stop7button<='0';stop8button<='0';
else
if buttonclk'event and buttonclk='1' then
if input='1' then
case up is
when "0001"=> f1upbutton<='1';
when "0010"=> f2upbutton<='1';
when "0011"=> f3upbutton<='1';
when "0100"=> f4upbutton<='1';
when "0101"=> f5upbutton<='1';
when "0110"=> f6upbutton<='1';
when "0111"=> f7upbutton<='1';
when others => null;
end case;
case down is
when "0010"=> f2dnbutton<='1';
when "0011"=> f3dnbutton<='1';
when "0100"=> f4dnbutton<='1';
when "0101"=> f5dnbutton<='1';
when "0110"=> f6dnbutton<='1';
when "0111"=> f7dnbutton<='1';
when "1000"=> f8dnbutton<='1';
when others => null;
end case;
case stop is
when "0001"=> stop1button<='1';
when "0010"=> stop2button<='1';
when "0011"=> stop3button<='1';
when "0100"=> stop4button<='1';
when "0101"=> stop5button<='1';
when "0110"=> stop6button<='1';
when "0111"=> stop7button<='1';
when "1000"=> stop8button<='1';
when others => null;
end case;
else
f1upbutton<='0';f2upbutton<='0';
f2dnbutton<='0'; f3dnbutton<='0'; f3upbutton<='0';
f4dnbutton<='0'; f4upbutton<='0'; f5dnbutton<='0'; f5upbutton<='0';
f6dnbutton<='0'; f6upbutton<='0'; f7dnbutton<='0'; f7upbutton<='0';
f8dnbutton<='0'; stop1button<='0';stop2button<='0';
stop3button<='0';stop4button<='0';
stop5button<='0';stop6button<='0';
stop7button<='0';stop8button<='0';
end if;
end if;
end if;
end process ;
process(reset,buttonclk)
begin
if reset='1' then
stoplight<="00000000"; fuplight<="00000000"; fdnlight<="00000000";
else
if buttonclk'event and buttonclk='1' then
if clearup='1' then
stoplight(position)<='0'; fuplight(position)<='0';
else
if f1upbutton='1' then fuplight(1)<='1';
elsif f2upbutton='1' then fuplight(2)<='1';
elsif f3upbutton='1' then fuplight(3)<='1';
elsif f4upbutton='1' then fuplight(4)<='1';
elsif f5upbutton='1' then fuplight(5)<='1';
elsif f6upbutton='1' then fuplight(6)<='1';
elsif f7upbutton='1' then fuplight(7)<='1';
end if;
end if;
if cleardn='1' then
stoplight(position)<='0'; fdnlight(position)<='0';
else
if f2dnbutton='1' then fdnlight(2)<='1';
elsif f3dnbutton='1' then fdnlight(3)<='1';
elsif f4dnbutton='1' then fdnlight(4)<='1'
展开阅读全文