本帖最后由 臣天男 于 2013-12-4 15:53 编辑
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY DIANTI IS
PORT( CLK:IN STD_LOGIC;
UP1,UP2:IN STD_LOGIC; 上楼请求
DOWN2,DOWN3:IN STD_LOGIC; 下楼请求
STOP1,STOP2,STOP3:IN STD_LOGIC; 出去请求
CLOSE:IN STD_LOGIC; 提前关门
DUAN:IN STD_LOGIC; 关门中断
DOOR:OUT STD_LOGIC; 门的状况
FLOOR:OUT STD_LOGIC_VECTOR(6 DOWNTO 0); 用数码管显示楼层
MODE:OUT STD_LOGIC_VECTOR(1 DOWNTO 0)); 电梯状态,00:上升;01:下降;10:停止;
END DIANTI;
ARCHITECTURE ONE OF DIANTI IS
TYPE STATE_TYPE IS(C1,C2,C3,OPEN_DOOR,ZQ); 状态:在第一层,在第二层,再第三层,开门,停止状态
SIGNAL UP:STD_LOGIC_VECTOR(3 DOWNTO 1); 上升信号存储
SIGNAL DOWN:STD_LOGIC_VECTOR(3 DOWNTO 1); 下降信号存储
SIGNAL M:STD_LOGIC_VECTOR(1 DOWNTO 0); 电梯状态
SIGNAL S:STD_LOGIC_VECTOR(3 DOWNTO 1); 出去请求存储
SIGNAL F:STD_LOGIC_VECTOR(6 DOWNTO 0); 楼层
SIGNAL D:STD_LOGIC; 门状况
SIGNAL STATE:STATE_TYPE;
SIGNAL OPEN_COUNT:INTEGER RANGE 0 TO 4; 开门到关门的时间延迟
SIGNAL Q:STD_LOGIC_VECTOR(3 DOWNTO 1); 所有请求存储
BEGIN
PROCESS(CLK,UP1,UP2,DOWN2,DOWN3,DUAN,STOP1,STOP2,STOP3,CLOSE,
UP,DOWN,M,S,F,D,STATE,OPEN_COUNT,Q)
BEGIN
IF CLK'EVENT AND CLK='1' THEN
IF UP1='1' THEN
UP(1)<='1';
END IF;
IF UP2='1' THEN
UP(2)<='1';
END IF;
UP(3)<='0';
DOWN(1)<='0';
IF DOWN2='1' THEN
DOWN(2)<='1';
END IF;
IF DOWN3='1' THEN
DOWN(3)<='1';
END IF;
IF STOP1='1' THEN
S(1)<='1';
END IF;
IF STOP2='1' THEN
S(2)<='1';
END IF;
IF STOP3='1' THEN
S(3)<='1';
END IF;
Q<=UP OR DOWN OR S;
CASE STATE IS
WHEN C1 => F<="0110000";
IF S(1)='1' OR UP(1)='1' THEN
STATE<=OPEN_DOOR;
S(1)<='0';UP(1)<='0';
ELSIF Q>"001" THEN
STATE<=C2;M<="00";
ELSE STATE<=ZQ;
END IF;
WHEN C2 => F<="1101101";
IF M="00" THEN
IF S(2)='1' OR UP(2)='1' THEN
STATE<=OPEN_DOOR;
S(2)<='0';UP(2)<='0';
ELSIF Q>"011" THEN
STATE<=C3;M<="00";
ELSIF Q="000" THEN
STATE<=ZQ;
ELSIF Q<"010" THEN
STATE<=C1;M<="01";
END IF;
ELSIF M="01" THEN
IF S(2)='1' OR DOWN(2)='1' THEN
STATE<=OPEN_DOOR;
S(2)<='0';DOWN(2)<='0';
ELSIF Q="000" THEN
STATE<=ZQ;
ELSIF Q<"010" THEN
STATE<=C1;M<="01";
ELSIF Q>"011" THEN
STATE<=C3;M<="00";
END IF;
ELSIF M="10" THEN
IF S(2)='1' OR DOWN(2)='1' THEN
STATE<=OPEN_DOOR;
S(2)<='0';DOWN(2)<='0';
ELSIF Q="000" THEN
STATE<=ZQ;
ELSIF Q<"010" THEN
STATE<=C1;M<="01";
ELSIF Q>"011" THEN
STATE<=C3;M<="00";
END IF;
END IF;
WHEN C3 => F<="1111001";
IF S(3)='1' OR DOWN(3)='1' THEN
STATE<=OPEN_DOOR;
S(3)<='0';DOWN(3)<='0';
ELSIF Q="000" THEN
STATE<=ZQ;
ELSIF Q<"100" THEN
STATE<=C2;M<="01";
END IF;
WHEN OPEN_DOOR=> D<='1';
OPEN_COUNT<=0;
IF CLOSE='1' THEN
OPEN_COUNT<=4;
ELSIF DUAN='1' THEN
OPEN_COUNT<=0;
END IF;
IF OPEN_COUNT<4 THEN
OPEN_COUNT<=OPEN_COUNT+1;
ELSE OPEN_COUNT<=0;
D<='0';
IF F="0110000" THEN
STATE<=C1;
ELSIF F="1101101" THEN
STATE<=C2;
ELSIF F="1111001" THEN
STATE<=C3;
END IF;
END IF;
WHEN ZQ => M<="10"; D<='0';
IF F="0110000" THEN
STATE<=C1;
ELSIF F="1101101" THEN
STATE<=C2;
ELSIF F="1111001" THEN
STATE<=C3;
END IF;
WHEN OTHERS => STATE<=ZQ;
END CASE;
DOOR<=D;
FLOOR<=F;
MODE<=M;
END IF;
END PROCESS;
END ONE;
|