以代碼的形式進行樓梯運行控制系統的模擬
點亮led表示開門- library ieee; -- 庫的說明
- use ieee.std_logic_1164.all; -- 程序包的說明
- use ieee.std_logic_unsigned.all;
- use ieee.std_logic_arith.all;
- entity SDC is -- 實體
- port(buttonclk:in std_logic; -- 按鍵時鐘信號
- liftclk:in std_logic; -- 電梯時鐘信號
- reset:in std_logic; -- 異步復位端口
- f1upbutton:in std_logic; -- 一層上升請求端口
- 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;-- 停站請求端口
- stoplight:buffer std_logic_vector(3 downto 1); -- 停站請求寄存信號
- position:buffer integer range 1 to 3;-- 電梯位置信號
- doorlight:out std_logic; -- 開關門信號
- udsig:buffer std_logic); -- 電梯模式(上升或下降)信號
- end SDC;architecture art of SDC 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; -- 下降和停站請求清除信號
- begin
- controlift:process(reset,liftclk) -- 狀態機進程
- variable pos:integer range 3 downto 1;
- --******************************--
- --**********電梯運行控制*********--
- --******************************--
- begin
- if reset='1' then
- 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;
- mylift <= doorwait1; -- 電梯等待 4s
- when doorwait1 => mylift <=doorwait2;
- when doorwait2 =>clearup<='0';cleardn<='0';mylift <=doorwait3;
- when doorwait3 => mylift <=doorwait4;
- when doorwait4 => mylift <= 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';
- mylift <= doorclose;
- elsif fdnlight(3)='0' or stoplight(3)='0' then-- 如果本層有請求信號時,電梯開門
- udsig<='1';
- mylift<=dooropen;
- else --否則下降udsig<='1';
- mylift<=down;
- end if;
- elsif position=2 then
- if stoplight="111" and fuplight="111" and fdnlight="111" then
- udsig<='0';
- mylift<=doorclose;
- elsif fuplight(2)='0' or stoplight(2)='0' then-- 本層有上升或停站請求時時,電梯開門
- udsig<='0';
- mylift<=dooropen;
- elsif fuplight="111" and stoplight="111" and fdnlight="101" then--只有二層有下降請求時,電梯開門
- udsig<='1';
- mylift<=dooropen;
- elsif stoplight(3)='0' or fdnlight(3)='0' then-- 三層有停站請求或下降請求,則上升
- udsig<='0';
- mylift<=up;
- else udsig<='1';
- mylift<=down;
- 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' then
- udsig<='0';
- mylift<=dooropen;
- else
- udsig<='0';
- mylift<=up;
- end if;
- end if;
-
-
- elsif udsig='1' then-- 如果電梯處在下降模式
- 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
- udsig<='0';
- mylift<=dooropen;
- else udsig<='0';
- mylift<=up;
- end if;
- elsif position=2 then
- if stoplight="111" and fuplight="111" and fdnlight="111" then
- udsig<='1';
- mylift<=doorclose;
- elsif fdnlight(2)='0' or stoplight(2)='0' then
- udsig<='1';
- mylift<=dooropen;
- elsif fdnlight="111" and stoplight="111" and fuplight="101" then
- udsig<='0';
- mylift<=dooropen;
- elsif stoplight(1)='0' or fuplight(1)='0' then
- udsig<='1';
- mylift<=down;
- else udsig<='0';
- mylift<=up;
- end if;
- elsif position=3 then
- if stoplight="111" and fuplight="111" and fdnlight="111" then
- udsig<='1';
- mylift<=doorclose;
- elsif fdnlight(3)='0' or stoplight(3)='0' then
- udsig<='1';
- mylift<=dooropen;
- else udsig<='1';
- mylift<=down;
- end if;
- end if;
- end if;
-
-
- --******************************--
- --**********上升模式*************--
- --******************************--
- 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(pos)='0')
- then
- mylift <= stop; -- 電梯處在三層,并且有三層停站或下降請求,則停止
- else
- mylift <= doorclose;
- end if;
- --******************************--
- --**********下降模式*************--
- --******************************--
- when down => -- 電梯處在下降狀態
- position <=position-1;-- 電梯樓層數減一
- pos:=pos-1;
- if pos >1 and (stoplight(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 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';
- 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';
- end 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 and 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;
- 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;
復制代碼
|