找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 3256|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

Quartus II實(shí)現(xiàn)FPGA模擬4層樓電梯控制程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:748680 發(fā)表于 2020-5-14 21:37 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
以代碼的形式進(jìn)行樓梯運(yùn)行控制系統(tǒng)的模擬
點(diǎn)亮led表示開(kāi)門
  1. library ieee;                                               -- 庫(kù)的說(shuō)明
  2. use ieee.std_logic_1164.all;                                -- 程序包的說(shuō)明
  3. use ieee.std_logic_unsigned.all;
  4. use ieee.std_logic_arith.all;
  5. entity SDC is                                                                         -- 實(shí)體
  6. port(buttonclk:in std_logic;                                -- 按鍵時(shí)鐘信號(hào)
  7.         liftclk:in std_logic;                                        -- 電梯時(shí)鐘信號(hào)
  8.         reset:in std_logic;                                                -- 異步復(fù)位端口
  9.         f1upbutton:in std_logic;                                -- 一層上升請(qǐng)求端口
  10.         f2upbutton:in std_logic;                                -- 二層上升請(qǐng)求端口
  11.         f2dnbutton:in std_logic;                                -- 二層下降請(qǐng)求端口
  12.         f3dnbutton:in std_logic;                                -- 三層下降請(qǐng)求端口
  13.         fuplight:buffer std_logic_vector(3 downto 1);    -- 上升請(qǐng)求寄存信號(hào)
  14.         fdnlight:buffer std_logic_vector(3 downto 1);    -- 下降請(qǐng)求寄存信號(hào)
  15.         stop1button,stop2button,stop3button:in std_logic;-- 停站請(qǐng)求端口
  16.         stoplight:buffer std_logic_vector(3 downto 1);         -- 停站請(qǐng)求寄存信號(hào)
  17.         position:buffer integer range 1 to 3;-- 電梯位置信號(hào)
  18.         doorlight:out std_logic;                                 -- 開(kāi)關(guān)門信號(hào)
  19.         udsig:buffer std_logic);                                 -- 電梯模式(上升或下降)信號(hào)
  20. end SDC;architecture art of SDC is                 -- 結(jié)構(gòu)體
  21. type lift_state is                                                         -- 定義十個(gè)狀態(tài)
  22.         (stopon1,dooropen,doorclose,doorwait1,doorwait2,doorwait3,doorwait4,up,down,stop);
  23.         signal mylift:lift_state;
  24.         signal clearup:std_logic;                                 -- 上升和停站請(qǐng)求清除信號(hào)
  25.         signal cleardn:std_logic;                                 -- 下降和停站請(qǐng)求清除信號(hào)
  26. begin
  27.         controlift:process(reset,liftclk)         -- 狀態(tài)機(jī)進(jìn)程
  28.         variable pos:integer range 3 downto 1;
  29.         --******************************--
  30.         --**********電梯運(yùn)行控制*********--
  31.         --******************************--
  32. begin
  33.         if reset='1' then
  34.                 mylift <= stopon1;                                         -- 異步復(fù)位,電梯的初始狀態(tài)為一層開(kāi)門狀態(tài)
  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';-- 關(guān)門,判定電梯下一個(gè)運(yùn)行方式
  49.         if udsig='0' then                                                          -- 如果電梯處在上升模式
  50.                 if position=3 then
  51.                         if stoplight="111" and fuplight="111" and fdnlight="111" then-- 沒(méi)有請(qǐng)求信號(hào)時(shí),電梯停在當(dāng)前層
  52.                                         udsig <= '1';
  53.                                         mylift <= doorclose;
  54.                         elsif fdnlight(3)='0' or stoplight(3)='0' then-- 如果本層有請(qǐng)求信號(hào)時(shí),電梯開(kāi)門
  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-- 本層有上升或停站請(qǐng)求時(shí)時(shí),電梯開(kāi)門
  65.                                 udsig<='0';
  66.                                 mylift<=dooropen;
  67.                         elsif fuplight="111" and stoplight="111" and fdnlight="101" then--只有二層有下降請(qǐng)求時(shí),電梯開(kāi)門
  68.                                 udsig<='1';
  69.                                 mylift<=dooropen;
  70.                         elsif stoplight(3)='0' or fdnlight(3)='0' then-- 三層有停站請(qǐng)求或下降請(qǐng)求,則上升
  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=>                                          -- 電梯處于上升狀態(tài)
  135.                 position<=position+1; -- 電梯樓層數(shù)加一
  136.                 pos:=pos+1;
  137.                 if pos <3 and (stoplight(pos)='0' or fuplight(pos)='0')
  138.                  then
  139.                         mylift <= stop;         -- 電梯在一層或二層,本層有停站或上升請(qǐng)求時(shí),則停止
  140.                 elsif pos=3 and (stoplight(pos)='0' or fdnlight(pos)='0')
  141.                  then
  142.                         mylift <= stop;         -- 電梯處在三層,并且有三層停站或下降請(qǐng)求,則停止
  143.                 else
  144.                         mylift <= doorclose;
  145.                 end if;
  146.         --******************************--
  147.         --**********下降模式*************--
  148.         --******************************--
  149.         when down =>                                 -- 電梯處在下降狀態(tài)
  150.                 position <=position-1;-- 電梯樓層數(shù)減一
  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';                  -- 清除當(dāng)前層上升和停站請(qǐng)求
  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';                 -- 清除當(dāng)前層下降和停站請(qǐng)求
  170.                 else clearup <= '1';
  171.                 cleardn <= '1';
  172.                 end if;
  173.                 end if;
  174.                
  175.                 mylift <= doorwait1;  --狀態(tài)機(jī)復(fù)位
  176.                 end case;end if;
  177.                 end if;
  178.                 end process controlift;
  179.         --******************************--
  180.         --****記憶電梯外各層停站請(qǐng)求******--
  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    -- 上升和停站請(qǐng)求清零
  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       -- 記憶各層下降請(qǐng)求
  205.                         fdnlight(2)<='0';
  206.                 elsif f3dnbutton='1' then
  207.                         fdnlight(3)<='0';
  208.                 end if;
  209.                 end if;
  210.                
  211.         --******************************--
  212.         --*****記憶進(jìn)入電梯后各層停站請(qǐng)求**--
  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;
復(fù)制代碼


評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表