Advertisement
Guest User

mama

a guest
May 17th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 6.63 KB | None | 0 0
  1. Library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4.  
  5. entity LIFT is
  6.     port(
  7.     iOUT: in std_logic_vector(3 downto 0) := (others => '0');   --outside input
  8.     iIN: in std_logic_vector(3 downto 0)  := (others => '0');   --inside elevator car input
  9.     DIR: in std_logic;                                          --FPGA button to switch for outside input (if you either want to go up/down)
  10.                                                                 -- 0 down, 1 up
  11.        
  12.     FLOOR: out std_logic_vector(6 downto 0) :="1111110";
  13.     an: out std_logic_vector(3 downto 0):="1110"; --output lcd 
  14.     DOOR: out std_logic := '1';                   --output door
  15.     WEIGHT: in std_logic;
  16.     DOOR_SENSOR: in std_logic := '0';
  17.     CLK: in std_logic);
  18. end LIFT;
  19.  
  20. architecture behavioral of LIFT is
  21.  
  22. type lift_states is (initial, waiting, check, openDoor, closeDoor, move);                 --states that the elevator has
  23. signal curr_state :  lift_states;
  24. signal next_state :  lift_states;
  25.  
  26. signal INTERIOR : std_logic_vector (13 downto 0) := (others => '0');    -- interior commands
  27. signal UP       : std_logic_vector (13 downto 0) := (others => '0');    -- exterior commands for going up
  28. signal DOWN     : std_logic_vector (13 downto 0) := (others => '0');    -- exterior commands for going down
  29. signal empty    : std_logic_vector (13 downto 0) := (others => '0');    -- empty vector used for comparison
  30.    
  31. signal iempty   : std_logic_vector (3 downto 0)  := (others => '0');    -- empty vector used for input comparison
  32.    
  33. signal position  : integer := 0;       --lift's floor
  34. signal direction : std_logic := '1';   --direction, up = 1 / down = 0;       
  35.  
  36. signal GOu, GOd :  std_logic;                                       -- checks if there are commands lower/higher relative to position    
  37.  
  38. signal i,k      :  natural;                 -- used to determine commands relative to position
  39.  
  40. signal counter  :  natural;
  41.  
  42. signal freq     :  std_logic := '0';
  43.  
  44.  
  45.                                                    
  46. begin
  47.    
  48. proccy: process(CLK)
  49. begin
  50.         curr_state <= next_state;  
  51. end process proccy;
  52.  
  53.  
  54. commands: process(CLK)
  55. begin
  56.        
  57.         if(iIN /= iempty) then
  58.             INTERIOR(to_integer(unsigned(iIN)))<='1';
  59.         end if;
  60.        
  61.         if(iOUT /= iempty) then
  62.             if( DIR = '1') then
  63.                 UP(to_integer(unsigned(iOUT))) <= '1';
  64.             end if;
  65.             if( DIR = '0') then
  66.                 DOWN(to_integer(unsigned(iOUT))) <= '1';
  67.             end if;
  68.         end if;
  69.         INTERIOR(position)<='0';
  70.         UP(position)<='0';
  71.         DOWN(position)<='0';
  72. end process commands;
  73.  
  74.  
  75. states_lift: process(clk)
  76.  
  77.  
  78. begin
  79. case curr_state is
  80.    
  81.     when initial =>     position   <= 1;                   --initialize everything
  82.             -- +door sensor, weight sensor;
  83.            
  84.     next_state <= waiting;
  85.     --waiting for stuff
  86.        
  87.     when waiting =>     if (UP /= empty or DOWN /= empty or INTERIOR /= empty) then
  88.                             next_state <= check;
  89.                         end if;
  90.    
  91.     --rough w8ing process
  92.    
  93.     when check =>        GOu   <= '0';
  94.                          GOd   <= '0';
  95.                          if(WEIGHT /= '1') then
  96.                             if (UP /= empty) then
  97.                                 next_state <= move;
  98.                             end if;
  99.                          
  100.                             if (DOWN /= empty) then
  101.                                  next_state <= move;
  102.                             end if;
  103.                          
  104.                             if (INTERIOR /= empty) then
  105.                                  next_state <= move;
  106.                             end if;
  107.                          end if;
  108.                          if(UP(position) = '1' or DOWN(position) = '1' or INTERIOR(position) = '1') then
  109.                              next_state <= openDoor;
  110.                          end if;
  111.                            
  112.     --the checking state
  113.    
  114.     when move =>         if(direction = '1') then
  115.                             if(i < 14 and GOu /= '1' and GOd /='1') then
  116.                                 if(i > position) then
  117.                                     if(UP(i) = '1' or INTERIOR(i) = '1')    then
  118.                                         GOu <= '1';
  119.                                     else     
  120.                                         i <= i + 1;
  121.                                     end if;
  122.                                 else
  123.                                     if(UP(i) = '1' or DOWN(i) = '1' or INTERIOR(i) = '1') then
  124.                                         GOd <= '1';
  125.                                     else
  126.                                         i <= i + 1;
  127.                                     end if;
  128.                                 end if;
  129.                             else if(GOu = '1') then
  130.                                
  131.                                     if(UP(position) = '1' or INTERIOR(position) = '1')  then
  132.                                         next_state <= openDoor;
  133.                                     else if(next_state /= openDoor) then
  134.                                             position <= position + 1;
  135.                                          end if;
  136.                                     end if;
  137.                                    
  138.                                  else if(GOd = '1') then
  139.                                         if(UP(position) = '1' or INTERIOR(position) = '1' or DOWN(position) = '1') then
  140.                                             next_state <= openDoor;
  141.                                         else  if(next_state /= openDoor) then
  142.                                                 position <= position - 1;
  143.                                               end if;
  144.                                         end if;
  145.                                       else
  146.                                         direction <= '0';
  147.                                         i <= 13;
  148.                                       end if;
  149.                                  end if;
  150.                                  
  151.                             end if;
  152.                            
  153.                           else
  154.                              
  155.                             if(i > 0 and GOd /= '1' and GOu /= '1') then
  156.                                 if(i < position) then
  157.                                     if(DOWN(i) = '1' or INTERIOR(i) = '1') then
  158.                                         GOd <= '1';
  159.                                     else
  160.                                         i <= i - 1;
  161.                                     end if;
  162.                                 else
  163.                                     if(DOWN(i) = '1' or INTERIOR(i) = '1') then
  164.                                         GOu <= '1';
  165.                                     else
  166.                                         i <= i - 1;
  167.                                     end if;
  168.                                 end if;
  169.                                        
  170.                              else
  171.                                  
  172.                                  if(GOd = '1')    then
  173.                                        
  174.                                     if(DOWN(position) = '1' or INTERIOR(position) = '1') then
  175.                                         next_state <= openDoor;
  176.                                     else if(next_state /= openDoor) then
  177.                                             position <= position - 1;
  178.                                          end if;
  179.                                     end if;
  180.                                          
  181.                                  else if(GOu = '1') then
  182.                                             if(DOWN(position) = '1' or INTERIOR(position) = '1') then
  183.                                                 next_state <= openDoor;
  184.                                             else if(next_state /= openDoor) then
  185.                                                     position <= position + 1;
  186.                                                  end if;
  187.                                             end if;
  188.                                        else
  189.                                            direction <= '1';
  190.                                        end if;
  191.                                   end if;
  192.                                    
  193.                               end if;
  194.                            end if;  
  195.                                      
  196.     when openDoor => --opens door, checks sensor for weight, waits a period of time
  197.        
  198.                         DOOR <= '1';
  199.                             next_state <= closeDoor;
  200.                        
  201.     when closeDoor => --closes door
  202.    
  203.                             DOOR <= '0';
  204.                             next_state <= waiting;
  205.                            
  206.  
  207.    
  208. end case;
  209.  
  210. end process states_lift;
  211.  
  212. display: process(freq)
  213. begin
  214.     case position is
  215.         when 1  => floor <= "1111110";
  216.         when 2  => floor <= "0110000";
  217.         when 3  => floor <= "1101101";
  218.         when 4  => floor <= "1111001";
  219.         when 5  => floor <= "0110011";
  220.         when 6  => floor <= "1011011";
  221.         when 7  => floor <= "1011111";
  222.         when 8  => floor <= "1110000";
  223.         when 9  => floor <= "1111111";
  224.         when 10 => floor <= "1111011";
  225.         when 11 => floor <= "1110111";--a
  226.         when 12 => floor <= "0011111";--b
  227.         when 13 => floor <= "1001110";--c
  228.         when others => floor <= "1111111";
  229.     end case;
  230. end process display;
  231.  
  232. frequency: process(clk)
  233. begin
  234.     if(k = 200) then
  235.         k <= 0;
  236.         freq <= not freq;
  237.     else
  238.         k <= k + 1;
  239.     end if;
  240. end process frequency;
  241.  
  242. end architecture behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement