找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2327|回复: 0
收起左侧

Quartus II实现FPGA模拟4层楼电梯控制程序

[复制链接]
ID:748680 发表于 2020-5-14 21:37 | 显示全部楼层 |阅读模式
以代码的形式进行楼梯运行控制系统的模拟
点亮led表示开门
  1. library ieee;                                               -- 库的说明
  2. use ieee.std_logic_1164.all;                                -- 程序包的说明
  3. use ieee.std_logic_unsigned.all;
  4. use ieee.std_logic_arith.all;
  5. entity SDC is                                                                         -- 实体
  6. port(buttonclk:in std_logic;                                -- 按键时钟信号
  7.         liftclk:in std_logic;                                        -- 电梯时钟信号
  8.         reset:in std_logic;                                                -- 异步复位端口
  9.         f1upbutton:in std_logic;                                -- 一层上升请求端口
  10.         f2upbutton:in std_logic;                                -- 二层上升请求端口
  11.         f2dnbutton:in std_logic;                                -- 二层下降请求端口
  12.         f3dnbutton:in std_logic;                                -- 三层下降请求端口
  13.         fuplight:buffer std_logic_vector(3 downto 1);    -- 上升请求寄存信号
  14.         fdnlight:buffer std_logic_vector(3 downto 1);    -- 下降请求寄存信号
  15.         stop1button,stop2button,stop3button:in std_logic;-- 停站请求端口
  16.         stoplight:buffer std_logic_vector(3 downto 1);         -- 停站请求寄存信号
  17.         position:buffer integer range 1 to 3;-- 电梯位置信号
  18.         doorlight:out std_logic;                                 -- 开关门信号
  19.         udsig:buffer std_logic);                                 -- 电梯模式(上升或下降)信号
  20. end SDC;architecture art of SDC is                 -- 结构体
  21. type lift_state is                                                         -- 定义十个状态
  22.         (stopon1,dooropen,doorclose,doorwait1,doorwait2,doorwait3,doorwait4,up,down,stop);
  23.         signal mylift:lift_state;
  24.         signal clearup:std_logic;                                 -- 上升和停站请求清除信号
  25.         signal cleardn:std_logic;                                 -- 下降和停站请求清除信号
  26. begin
  27.         controlift:process(reset,liftclk)         -- 状态机进程
  28.         variable pos:integer range 3 downto 1;
  29.         --******************************--
  30.         --**********电梯运行控制*********--
  31.         --******************************--
  32. begin
  33.         if reset='1' then
  34.                 mylift <= stopon1;                                         -- 异步复位,电梯的初始状态为一层开门状态
  35.                 clearup <= '0';
  36.                 cleardn <= '0';
  37.                 pos:=1;
  38.                 position<=1;
  39.         else if liftclk'event and liftclk='1'
  40.         then
  41.         case mylift is
  42.                 when stopon1 => doorlight <= '0';position <= 1;pos:=1;
  43.                           mylift <= doorwait1;                                 -- 电梯等待 4s
  44.                 when doorwait1 => mylift <=doorwait2;
  45.                 when doorwait2 =>clearup<='0';cleardn<='0';mylift <=doorwait3;
  46.       when doorwait3 => mylift <=doorwait4;
  47.       when doorwait4 => mylift <= doorclose;
  48.       when doorclose => doorlight <= '1';-- 关门,判定电梯下一个运行方式
  49.         if udsig='0' then                                                          -- 如果电梯处在上升模式
  50.                 if position=3 then
  51.                         if stoplight="111" and fuplight="111" and fdnlight="111" then-- 没有请求信号时,电梯停在当前层
  52.                                         udsig <= '1';
  53.                                         mylift <= doorclose;
  54.                         elsif fdnlight(3)='0' or stoplight(3)='0' then-- 如果本层有请求信号时,电梯开门
  55.                                         udsig<='1';
  56.                                         mylift<=dooropen;
  57.                    else --否则下降udsig<='1';
  58.                                         mylift<=down;
  59.                         end if;
  60.                 elsif position=2 then
  61.                         if stoplight="111" and fuplight="111" and fdnlight="111" then
  62.                                 udsig<='0';
  63.                                 mylift<=doorclose;
  64.                         elsif fuplight(2)='0' or stoplight(2)='0' then-- 本层有上升或停站请求时时,电梯开门
  65.                                 udsig<='0';
  66.                                 mylift<=dooropen;
  67.                         elsif fuplight="111" and stoplight="111" and fdnlight="101" then--只有二层有下降请求时,电梯开门
  68.                                 udsig<='1';
  69.                                 mylift<=dooropen;
  70.                         elsif stoplight(3)='0' or fdnlight(3)='0' then-- 三层有停站请求或下降请求,则上升
  71.                                 udsig<='0';
  72.                                 mylift<=up;
  73.                         else udsig<='1';
  74.                                 mylift<=down;
  75.                         end if;
  76.                 elsif position=1 then
  77.                         if stoplight="111" and fuplight="111" and fdnlight="111" then
  78.                                 udsig<='0';
  79.                                 mylift<=doorclose;
  80.                         elsif stoplight(1)='0' or fuplight(1)='0' then
  81.                                 udsig<='0';
  82.                                 mylift<=dooropen;
  83.                         else
  84.                                 udsig<='0';
  85.                                 mylift<=up;
  86.                         end if;
  87.                 end if;
  88.                
  89.                
  90.         elsif udsig='1' then-- 如果电梯处在下降模式
  91.         if position=1 then
  92.         if stoplight="111" and fuplight="111" and fdnlight="111" then
  93.                 udsig<='0';
  94.                 mylift<=doorclose;
  95.         elsif stoplight(1)='0' or fuplight(1)='0' then
  96.                 udsig<='0';
  97.                 mylift<=dooropen;
  98.         else udsig<='0';
  99.                 mylift<=up;
  100.                 end if;
  101.         elsif position=2 then
  102.         if stoplight="111" and fuplight="111" and fdnlight="111" then
  103.                 udsig<='1';
  104.                 mylift<=doorclose;
  105.         elsif fdnlight(2)='0' or stoplight(2)='0' then
  106.                 udsig<='1';
  107.                 mylift<=dooropen;
  108.         elsif fdnlight="111" and stoplight="111" and fuplight="101" then
  109.                 udsig<='0';
  110.                 mylift<=dooropen;
  111.         elsif stoplight(1)='0' or fuplight(1)='0' then
  112.                 udsig<='1';
  113.                 mylift<=down;
  114.         else udsig<='0';
  115.                 mylift<=up;
  116.         end if;
  117.         elsif position=3 then
  118.         if stoplight="111" and fuplight="111" and fdnlight="111" then
  119.                 udsig<='1';
  120.                 mylift<=doorclose;
  121.         elsif fdnlight(3)='0' or stoplight(3)='0' then
  122.                 udsig<='1';
  123.                 mylift<=dooropen;
  124.         else udsig<='1';
  125.                 mylift<=down;
  126.         end if;
  127.                 end if;
  128.                         end if;
  129.                        
  130.                        
  131.         --******************************--
  132.         --**********上升模式*************--
  133.         --******************************--
  134.         when up=>                                          -- 电梯处于上升状态
  135.                 position<=position+1; -- 电梯楼层数加一
  136.                 pos:=pos+1;
  137.                 if pos <3 and (stoplight(pos)='0' or fuplight(pos)='0')
  138.                  then
  139.                         mylift <= stop;         -- 电梯在一层或二层,本层有停站或上升请求时,则停止
  140.                 elsif pos=3 and (stoplight(pos)='0' or fdnlight(pos)='0')
  141.                  then
  142.                         mylift <= stop;         -- 电梯处在三层,并且有三层停站或下降请求,则停止
  143.                 else
  144.                         mylift <= doorclose;
  145.                 end if;
  146.         --******************************--
  147.         --**********下降模式*************--
  148.         --******************************--
  149.         when down =>                                 -- 电梯处在下降状态
  150.                 position <=position-1;-- 电梯楼层数减一
  151.                 pos:=pos-1;
  152.                 if pos >1 and (stoplight(pos)='0' or fdnlight(pos)='0') then
  153.                         mylift <= stop;elsif pos=1 and (stoplight(pos)='0' or fuplight(pos)='0') then
  154.                         mylift <= stop;
  155.                 else mylift <= doorclose;
  156.                 end if;
  157.                
  158.         when stop => mylift <= dooropen;
  159.         when dooropen => doorlight <= '0';
  160.                 if udsig='0' then
  161.                 if position<3 and (stoplight(pos)='0' or fuplight(pos)='0') then
  162.                 clearup <= '1';                  -- 清除当前层上升和停站请求
  163.                 else
  164.                         clearup<='1';
  165.                         cleardn<='1';
  166.                 end if;
  167.                 elsif udsig='1' then
  168.                 if position >1 and (stoplight(pos)='0' or fdnlight(pos)='0') then
  169.                 cleardn <= '1';                 -- 清除当前层下降和停站请求
  170.                 else clearup <= '1';
  171.                 cleardn <= '1';
  172.                 end if;
  173.                 end if;
  174.                
  175.                 mylift <= doorwait1;  --状态机复位
  176.                 end case;end if;
  177.                 end if;
  178.                 end process controlift;
  179.         --******************************--
  180.         --****记忆电梯外各层停站请求******--
  181.         --******************************--
  182. controlight:process(reset,buttonclk)
  183.                 begin
  184.                
  185.                 if reset='1' then
  186.                         stoplight <= "111";
  187.                         fuplight <= "111";
  188.                         fdnlight <= "111";
  189.                 else
  190.                         if buttonclk'event and buttonclk='1' then
  191.                                 if clearup='1' then    -- 上升和停站请求清零
  192.                                         stoplight(position) <= '1';
  193.                                         fuplight(position) <= '1';
  194.                                 else if
  195.                                    f1upbutton='1' then
  196.                                    fuplight(1)<='0';
  197.                                 elsif f2upbutton='1' then
  198.                                         fuplight(2)<='0';
  199.                                 end if;
  200.                         end if;
  201.                 if cleardn='1' then stoplight(position) <= '1';
  202.                         fdnlight(position) <= '1';
  203.                 else if
  204.                         f2dnbutton='1' then       -- 记忆各层下降请求
  205.                         fdnlight(2)<='0';
  206.                 elsif f3dnbutton='1' then
  207.                         fdnlight(3)<='0';
  208.                 end if;
  209.                 end if;
  210.                
  211.         --******************************--
  212.         --*****记忆进入电梯后各层停站请求**--
  213.         --******************************--
  214.        
  215.         if stop1button='1' then      
  216.                 stoplight(1)<='0';
  217.                 elsif stop2button='1' then
  218.                         stoplight(2)<='0';
  219.                         elsif stop3button='1' then
  220.                                 stoplight(3)<='0';
  221.                         end if;
  222.                 end if;
  223.         end if;
  224. end process controlight;
  225. end art;
复制代码


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表