Advertisement
Guest User

Untitled

a guest
May 16th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 7.29 KB | None | 0 0
  1. https://pastebin.com/5phbyeVALibrary 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;-- := initial;
  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 press  : natural;                                                   --It's 1 for ext. commands and 2 for interior commands and 0 for no commands
  34.  
  35. signal position  : integer := 0;       --lift's floor
  36. signal direction : std_logic := '1';   --direction, up = 1 / down = 0;       
  37.  
  38. signal GOu, GOd :  std_logic;                                       -- checks if there are commands lower/higher relative to position    
  39.  
  40. signal i,k      :  natural;                 -- used to determine commands relative to position
  41.  
  42. signal counter  :  natural;
  43.  
  44. signal freq     :  std_logic := '0';
  45.  
  46.  
  47.                                                    
  48. begin
  49.    
  50. proccy: process(CLK)
  51. begin
  52.     if(rising_edge(CLK)) then
  53.         curr_state <= next_state;  
  54.     end if;
  55. end process proccy;
  56.  
  57.  
  58. commands: process(freq)
  59. begin
  60.     if(rising_edge(freq)) then
  61.        
  62.         if(iIN /= iempty) then
  63.             press <= 2;
  64.         end if;
  65.        
  66.         if(iOUT /= iempty) then
  67.             press <= 1;
  68.         end if;
  69.        
  70.         if( press = 1 ) then
  71.             if( DIR = '1') then
  72.                 UP(to_integer(unsigned(iOUT))) <= '1';
  73.                 press <= 0;
  74.             end if;
  75.            
  76.             if( DIR = '0') then
  77.                 DOWN(to_integer(unsigned(iOUT))) <= '1';
  78.                 press <= 0;
  79.             end if;
  80.          elsif(press = 2) then
  81.                 INTERIOR(to_integer(unsigned(iIN))) <= '1';
  82.                 press <=0;
  83.          end if;
  84.          
  85.          if(position /= to_integer(unsigned(iOUT)) and position /= to_integer(unsigned(iIN))) then
  86.             UP(position)        <= '0';
  87.             DOWN(position)      <= '0';
  88.             INTERIOR(position)  <= '0';
  89.          end if;
  90.     end if;
  91. end process commands;
  92.  
  93.  
  94. states_lift: process(freq)
  95.  
  96.  
  97. begin
  98. case curr_state is
  99.    
  100.     when initial =>     position   <= 0;                   --initialize everything
  101.             -- +door sensor, weight sensor;
  102.            
  103.     next_state <= closeDoor;
  104.     --waiting for stuff
  105.        
  106.     when waiting =>     if (UP /= empty or DOWN /= empty or INTERIOR /= empty) then
  107.                             next_state <= check;
  108.                         end if;
  109.    
  110.     --rough w8ing process
  111.    
  112.     when check =>        GOu   <= '0';
  113.                          GOd   <= '0';
  114.                          if(WEIGHT /= '1') then
  115.                             if (UP /= empty) then
  116.                                 next_state <= move;
  117.                             end if;
  118.                          
  119.                             if (DOWN /= empty) then
  120.                                  next_state <= move;
  121.                             end if;
  122.                          
  123.                             if (INTERIOR /= empty) then
  124.                                  next_state <= move;
  125.                             end if;
  126.                          end if;
  127.                          if(UP(position) = '1' or DOWN(position) = '1' or INTERIOR(position) = '1') then
  128.                              next_state <= openDoor;
  129.                          end if;
  130.                            
  131.     --the checking state
  132.    
  133.     when move =>         if(direction = '1') then
  134.                             if(i < 14 and GOu /= '1' and GOd /='1') then
  135.                                 if(i > position) then
  136.                                     if(UP(i) = '1' or INTERIOR(i) = '1')    then
  137.                                         GOu <= '1';
  138.                                     else     
  139.                                         i <= i + 1;
  140.                                     end if;
  141.                                 else
  142.                                     if(UP(i) = '1' or DOWN(i) = '1' or INTERIOR(i) = '1') then
  143.                                         GOd <= '1';
  144.                                     else
  145.                                         i <= i + 1;
  146.                                     end if;
  147.                                 end if;
  148.                             else if(GOu = '1') then
  149.                                
  150.                                     if(UP(position) = '1' or INTERIOR(position) = '1')  then
  151.                                         next_state <= OpenDoor;
  152.                                     else if(next_state /= OpenDoor) then
  153.                                             position <= position + 1;
  154.                                          end if;
  155.                                     end if;
  156.                                    
  157.                                  else if(GOd = '1') then
  158.                                         if(UP(position) = '1' or INTERIOR(position) = '1' or DOWN(position) = '1') then
  159.                                             next_state <= OpenDoor;
  160.                                         else  if(next_state /= OpenDoor) then
  161.                                                 position <= position - 1;
  162.                                               end if;
  163.                                         end if;
  164.                                       else
  165.                                         direction <= '0';
  166.                                         i <= 13;
  167.                                       end if;
  168.                                  end if;
  169.                                  
  170.                             end if;
  171.                            
  172.                           else
  173.                              
  174.                             if(i > 0 and GOd /= '1' and GOu /= '1') then
  175.                                 if(i < position) then
  176.                                     if(DOWN(i) = '1' or INTERIOR(i) = '1') then
  177.                                         GOd <= '1';
  178.                                     else
  179.                                         i <= i - 1;
  180.                                     end if;
  181.                                 else
  182.                                     if(DOWN(i) = '1' or INTERIOR(i) = '1') then
  183.                                         GOu <= '1';
  184.                                     else
  185.                                         i <= i - 1;
  186.                                     end if;
  187.                                 end if;
  188.                                        
  189.                              else
  190.                                  
  191.                                  if(GOd = '1')    then
  192.                                        
  193.                                     if(DOWN(position) = '1' or INTERIOR(position) = '1') then
  194.                                         next_state <= OpenDoor;
  195.                                     else if(next_state /= OpenDoor) then
  196.                                             position <= position - 1;
  197.                                          end if;
  198.                                     end if;
  199.                                          
  200.                                  else if(GOu = '1') then
  201.                                             if(DOWN(position) = '1' or INTERIOR(position) = '1') then
  202.                                                 next_state <= OpenDoor;
  203.                                             else if(next_state /= OpenDoor) then
  204.                                                     position <= position + 1;
  205.                                                  end if;
  206.                                             end if;
  207.                                        else
  208.                                            direction <= '1';
  209.                                        end if;
  210.                                   end if;
  211.                                    
  212.                               end if;
  213.                            end if;  
  214.                                      
  215.     when openDoor => --opens door, checks sensor for weight, waits a period of time
  216.        
  217.                         DOOR <= '1';
  218.                         if(counter = 3) then
  219.                             next_state <= closeDoor;
  220.                         else
  221.                             counter <= counter + 1;
  222.                         end if;
  223.    
  224.     when closeDoor => --closes door
  225.    
  226.                         counter <= 0;
  227.                         if(door_sensor = '0') then
  228.                             DOOR <= '0';
  229.                             next_state <= waiting;
  230.                         else
  231.                             next_state <= openDoor;
  232.                         end if;
  233.  
  234.    
  235. end case;
  236.  
  237. end process states_lift;
  238.  
  239. display: process(freq)
  240. begin
  241.     case position is
  242.         when 1  => floor <= "1111110";
  243.         when 2  => floor <= "0110000";
  244.         when 3  => floor <= "1101101";
  245.         when 4  => floor <= "1111001";
  246.         when 5  => floor <= "0110011";
  247.         when 6  => floor <= "1011011";
  248.         when 7  => floor <= "1011111";
  249.         when 8  => floor <= "1110000";
  250.         when 9  => floor <= "1111111";
  251.         when 10 => floor <= "1111011";
  252.         when 11 => floor <= "1110111";--a
  253.         when 12 => floor <= "0011111";--b
  254.         when 13 => floor <= "1001110";--c
  255.         when others => floor <= "1111111";
  256.     end case;
  257. end process display;
  258.  
  259. frequency: process(clk)
  260. begin
  261.     if(k = 200) then
  262.         k <= 0;
  263.         freq <= not freq;
  264.     else
  265.         k <= k + 1;
  266.     end if;
  267. end process frequency;
  268.  
  269. end architecture behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement