1、library ieee;--库的说明 use ieee.std_logic_1164.all;--程序包的说明 use ieee.std_logic_unsigned.all; use ieee.std_logic_arith.all; entity threelift is --实体 port(buttonclk:in std_logic;--按键时钟信号 liftclk:in std_logic;--电梯时钟信号 reset:in std_logic;--异步复位端口 f1upbutton:in std_logic;--一层上升请求
2、端口 f2upbutton:in std_logic;--二层上升请求端口 f2dnbutton:in std_logic;--二层下降请求端口 f3dnbutton:in std_logic;--三层下降请求端口 fuplight:buffer std_logic_vector(3 downto 1);--上升请求寄存信号 fdnlight:buffer std_logic_vector(3 downto 1);--下降请求寄存信号 stop1button,stop2button,stop3button:in std_logic;-
3、停站请求端口 stoplight:buffer std_logic_vector(3 downto 1);--停站请求寄存信号 position:buffer integer range 1 to 3;--电梯位置信号 doorlight:out std_logic;--开关门信号 udsig:buffer std_logic);--电梯模式(上升或下降)信号 end threelift; architecture art of threelift is--结构体 type lift_state is--定义十个状态 (stopon1,do
4、oropen,doorclose,doorwait1,doorwait2,doorwait3,doorwait4,up,down,stop); signal mylift:lift_state; signal clearup:std_logic;--上升和停站请求清除信号 signal cleardn:std_logic;--下降和停站请求清除信号 begin controlift:process(reset,liftclk)--状态机进程 variable pos:integer range 3 downto 1; begin if reset='1' then
5、 mylift <= stopon1;--异步复位,电梯的初始状态为一层开门状态 clearup <= '0'; cleardn <= '0'; pos:=1; position<=1; else if liftclk'event and liftclk='1' then case mylift is when stopon1 => doorlight <= '0'; position <= 1; pos:=1;
6、 mylift <= doorwait1;--电梯等待4s when doorwait1 => mylift <=doorwait2; when doorwait2 => clearup<='0'; cleardn<='0'; mylift <=doorwait3; when doorwait3 => mylift <=doorwait4; when doorwait4 => mylift
7、<= doorclose; when doorclose => doorlight <= '1';--关门,判定电梯下一个运行方式 if udsig='0' then--电梯处在上升模式 if position=3 then if stoplight="111" and fuplight="111" and fdnlight="111" then--没有请求信号时,电梯停在当前层 udsig <= '1';
8、 mylift <= doorclose; elsif fdnlight(3)='0' or stoplight(3)='0' then--本层有请求信号是,电梯开门 udsig<='1'; mylift<=dooropen; else --否则下降 udsi
9、g<='1'; mylift<=down; end if; elsif position=2 then if stoplight="111" and fuplight="111" and fdnlight="111" then udsig<='0'; mylift<=doorclose;
10、 elsif fuplight(2)='0' or stoplight(2)='0' then--本层有上升或停站请求时时,电梯开门 udsig<='0'; mylift<=dooropen; elsif fuplight="111" and stoplight="111" and fdnlight="101" then--只有二层有下降请求时,电梯开门 udsig<=
11、'1'; mylift<=dooropen; elsif stoplight(3)='0' or fdnlight(3)='0' then--三层有停站请求或下降请求,则上升 udsig<='0'; mylift<=up; else udsig<='1'; mylift<=down;
12、 end if; elsif position=1 then if stoplight="111" and fuplight="111" and fdnlight="111" then udsig<='0'; mylift<=doorclose; elsif stoplight(1)='0' or fuplight(1)='0'
13、then udsig<='0'; mylift<=dooropen; else udsig<='0'; mylift<=up; end if; end if; elsif udsig='1' then--电梯处在
14、下降模式 if position=1 then if stoplight="111" and fuplight="111" and fdnlight="111" then udsig<='0'; mylift<=doorclose; elsif stoplight(1)='0' or fuplight(1)='0' then
15、 udsig<='0'; mylift<=dooropen; else udsig<='0'; mylift<=up; end if; elsif position=2 then if stoplight="111" and fuplight="111" and fdnlight
16、"111" then udsig<='1'; mylift<=doorclose; elsif fdnlight(2)='0' or stoplight(2)='0' then udsig<='1'; mylift<=dooropen; elsif fdnlight="111" and stopl
17、ight="111" and fuplight="101" then udsig<='0'; mylift<=dooropen; elsif stoplight(1)='0' or fuplight(1)='0' then udsig<='1'; mylift<=down; else udsig<=
18、'0'; mylift<=up; end if; elsif position=3 then if stoplight="111" and fuplight="111" and fdnlight="111" then udsig<='1'; mylift<=doorclose;
19、 elsif fdnlight(3)='0' or stoplight(3)='0' then udsig<='1'; mylift<=dooropen; else udsig<='1'; mylift<=down; end if; end if; end if;
20、 when up=> --电梯处于上升状态 position<=position+1;--电梯楼层数加一 pos:=pos+1; if pos <3 and (stoplight(pos)='0' or fuplight(pos)='0') then mylift <= stop;--电梯在一层或二层,本层有停站或上升请求时,则停止 elsif pos=3 and (stoplight(pos)='0' or fdnlight
21、pos)='0') then mylift <= stop;--电梯处在三层,并且有三层停站或下降请求,则停止 else mylift <= doorclose; end if; when down =>--电梯处在下降状态 position <=position-1;--电梯楼层数减一 pos:=pos-1; if pos >1 and (stoplig
22、ht(pos)='0' or fdnlight(pos)='0') then mylift <= stop; elsif pos=1 and (stoplight(pos)='0' or fuplight(pos)='0') then mylift <= stop; else mylift <= doorclose; end if; when stop => mylift <= dooropen; when
23、dooropen => doorlight <= '0'; if udsig='0' then if position<3 and (stoplight(pos)='0' or fuplight(pos)='0') then clearup <= '1'; --清除当前层上升和停站请求 else clearup<='1'; cleardn<='1';
24、 end if; elsif udsig='1' then if position >1 and (stoplight(pos)='0' or fdnlight(pos)='0') then cleardn <= '1';--清除当前层下降和停站请求 else clearup <= '1'; cleardn <= '1'; e
25、nd if; end if; mylift <= doorwait1; end case; end if; end if; end process controlift; controlight:process(reset,buttonclk) begin if reset='1' then stoplight <= "111"; fuplight <= "111"; fdnlight <= "111"; else if buttonclk'event a
26、nd buttonclk='1' then if clearup='1' then--上升和停站请求清零 stoplight(position) <= '1'; fuplight(position) <= '1'; else if f1upbutton='1' then --记忆各层上升请求 fuplight(1)<='0'; elsif f2upbutton='1' then fuplight(2)<='0'; end if;
27、 end if; if cleardn='1' then stoplight(position) <= '1'; fdnlight(position) <= '1'; else if f2dnbutton='1' then --记忆各层下降请求 fdnlight(2)<='0'; elsif f3dnbutton='1' then fdnlight(3)<='0'; end if; end if; if stop1button='1' then --记忆各层停站请求 stoplight(1)<='0'; elsif stop2button='1' then stoplight(2)<='0'; elsif stop3button='1' then stoplight(3)<='0'; end if; end if; end if; end process controlight; end art;






