标题:
Quartus II实现FPGA模拟4层楼电梯控制程序
[打印本页]
作者:
1233222
时间:
2020-5-14 21:37
标题:
Quartus II实现FPGA模拟4层楼电梯控制程序
以代码的形式进行楼梯运行控制系统的模拟
点亮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;
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1