📄 threelift.vhd
字号:
--- 三层电梯控制器
--- 樊伟良
--- 2007-5-22
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity threelift is
port(clk : in std_logic; --全局10M时钟
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); --电梯内部各层指示灯
doorlight : out std_logic; --开门指示灯
seg : out std_logic_vector (6 downto 0); --数码管显示所处楼层
upsiglight : out std_logic;
dnsiglight : out std_logic); --上升下降模式
end entity;
architecture art of threelift is
component fen is
port(clk : in std_logic; --10MHz信号
clk_1Hz,clk_100Hz: out std_logic);
end component;
component disp is
port(clk_100Hz : in std_logic;
data : in integer range 1 to 3;
segout : out std_logic_vector(6 downto 0));
end component;
signal udsig : std_logic;
signal position : integer range 1 to 3 :=1; --所处的层次
signal buttonclk : std_logic; --按键处理时钟10Hz
signal liftclk : std_logic; --1Hz信号
type lift_state is --电梯运行十种状态
(stopon1,dooropen,doorclose,doorwait1,doorwait2,doorwait3,doorwait4,up,down,stop);
signal mylift:lift_state;
signal clearup,cleardn :std_logic;--用于清除上升,下降请求指示灯
begin
u1: fen port map (clk,liftclk,buttonclk);
u2: disp port map (buttonclk,position,seg);
upsiglight<= not udsig;
dnsiglight<= udsig;
ctrlift : process(reset,liftclk)
variable pos : integer range 3 downto 1;
begin
if reset='0' then --全局复位,电梯回到一层
mylift<=stopon1;
clearup<='0';
cleardn<='0';
else
if liftclk'event and liftclk='1' then
case mylift is --状态机的实现
when stopon1 =>
doorlight<='1'; --开门指示灯亮
position<=1;pos:=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 --上升模式
if position=3 then
if stoplight="000" and fuplight="000" and fdnlight="000" then
udsig<='1';
mylift<=doorclose;
else
udsig<='1';mylift<=down;
end if;
elsif position=2 then
if stoplight="000" and fuplight="000" and fdnlight="000" then
udsig<='0';
mylift<=doorclose;
elsif
stoplight(3)='1' or (stoplight(3)='0' and fdnlight(3)='1') then
udsig<='0';
mylift<=up;
else
udsig<='1';
mylift<=down;
end if;
elsif position=1 then
if stoplight="000" and fuplight="000" and fdnlight="000" then
udsig<='0';
mylift<=doorclose;
else
udsig<='0';
mylift<=up;
end if;
end if;
elsif udsig='1' then --下降模式
if position=1 then
if stoplight="000" and fuplight="000" and fdnlight="000" then
udsig<='0';
mylift<=doorclose;
else
udsig<='0';
mylift<=up;
end if;
elsif position=2 then
if stoplight="000" and fuplight="000" and fdnlight="000" 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="000" and fuplight="000" and fdnlight="000" then
udsig<='1';
mylift<=doorclose;
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)='1' or fuplight(pos)='1') then
mylift<=stop;
elsif pos=3 and (stoplight(3)='1' or fdnlight(3)='1') then
mylift<=stop;
elsif pos=2 and fdnlight(2)='1' and fdnlight(3)='0' then
mylift<=stop;
else
mylift<=doorclose;
end if;
when down =>
position<=position-1;
pos:=pos-1;
if pos>1 and (stoplight(pos)='1' or fdnlight(pos)='1') then
mylift<=stop;
elsif pos=1 and (stoplight(pos)='1' or fuplight(pos)='1') then
mylift<=stop;
else
mylift<=doorclose;
end if;
when stop =>
mylift<=dooropen;
when dooropen =>
doorlight<='1';
if udsig='0' then
if position<=2 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;
ctrlight: process(reset,buttonclk)
begin
if reset='0' then
stoplight<="000";
fuplight <="000";
fdnlight <="000";
else
if buttonclk'event and buttonclk='1' then
if clearup='1' then
stoplight(position)<='0';
fuplight (position)<='0';
else
if f1upbutton='0' then
fuplight(1)<='1';
elsif f2upbutton='0' then
fuplight(2)<='1';
end if;
end if;
if cleardn='1' then
stoplight(position)<='0';
fdnlight (position)<='0';
else
if f2dnbutton='0' then
fdnlight(2)<='1';
elsif f3dnbutton='0' then
fdnlight(3)<='1';
end if;
end if;
if stop1button='0' then stoplight(1)<='1';
elsif stop2button='0' then stoplight(2)<='1';
elsif stop3button='0' then stoplight(3)<='1';
end if;
end if;
end if;
end process ctrlight;
end art;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -