Advertisement
lukicdarkoo

Automati: Žmigavac sa PWM

Dec 5th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 4.58 KB | None | 0 0
  1. ----------------------------------------------------------------------------------
  2. -- Company:
  3. -- Engineer:
  4. --
  5. -- Create Date:    22:06:10 12/03/2014
  6. -- Design Name:
  7. -- Module Name:    MainModule - Behavioral
  8. -- Project Name:
  9. -- Target Devices:
  10. -- Tool versions:
  11. -- Description:
  12. --
  13. -- Dependencies:
  14. --
  15. -- Revision:
  16. -- Revision 0.01 - File Created
  17. -- Additional Comments:
  18. --
  19. ----------------------------------------------------------------------------------
  20. library IEEE;
  21. use IEEE.STD_LOGIC_1164.ALL;
  22. use IEEE.STD_LOGIC_UNSIGNED.ALL;
  23.  
  24. -- Uncomment the following library declaration if using
  25. -- arithmetic functions with Signed or Unsigned values
  26. --use IEEE.NUMERIC_STD.ALL;
  27.  
  28. -- Uncomment the following library declaration if instantiating
  29. -- any Xilinx primitives in this code.
  30. --library UNISIM;
  31. --use UNISIM.VComponents.all;
  32.  
  33. entity MainModule is
  34.     Port ( iCLK : in  STD_LOGIC;
  35.            inRST : in  STD_LOGIC;
  36.            inLEFT : in  STD_LOGIC;
  37.            inRIGHT : in  STD_LOGIC;
  38.            inHAZ : in  STD_LOGIC;
  39.            oLEFT : out  STD_LOGIC_VECTOR (2 downto 0);
  40.            oRIGHT : out  STD_LOGIC_VECTOR (2 downto 0)
  41.     );
  42. end MainModule;
  43.  
  44. architecture Behavioral of MainModule is
  45.     type tSTATES is (IDLE, L1, L2, L3, R1, R2, R3, LR3);
  46.     signal sSTATE: tSTATES;
  47.    
  48.     signal sDEVIDER: STD_LOGIC_VECTOR(23 downto 0);
  49.     signal sENABLE_CS: STD_LOGIC;
  50.     constant cDEVIDER_END: integer := 12;
  51.    
  52.     signal sPWM_DEVIDER: STD_LOGIC_VECTOR(2 downto 0);
  53.     signal sPWM_CLOCK: STD_LOGIC;
  54.     type tPWM_STATES is (P25, P50, P75);
  55.     signal sPWM_STATE: tPWM_STATES;
  56.    
  57.     signal sLEFT, sRIGHT: STD_LOGIC_VECTOR(2 downto 0);
  58. begin
  59.     -- Proces u kome je realizovan automat, sa asihronim resetom
  60.     Automat: process (iCLK, inRST) begin
  61.         if (inRST = '0') then
  62.             sSTATE <= IDLE;
  63.            
  64.         elsif (rising_edge(iCLK)) then
  65.             if (sENABLE_CS = '1') then
  66.                 case (sSTATE) is
  67.                     when IDLE =>
  68.                                     if (inHAZ = '0') then
  69.                                         sSTATE <= LR3;
  70.                                     elsif (inLEFT = '0' and inRIGHT = '0') then
  71.                                         sSTATE <= LR3;
  72.                                     elsif (inLEFT = '0') then
  73.                                         sSTATE <= L1;
  74.                                     elsif (inRIGHT = '0') then
  75.                                         sSTATE <= R1;
  76.                                     else
  77.                                         sSTATE <= IDLE;
  78.                                     end if;
  79.                    
  80.                     when L1 => sSTATE <= L2;
  81.                     when L2 => sSTATE <= L3;
  82.                     when L3 => sSTATE <= IDLE;
  83.                    
  84.                     when R1 => sSTATE <= R2;
  85.                     when R2 => sSTATE <= R3;
  86.                     when R3 => sSTATE <= IDLE;
  87.                    
  88.                     when LR3 => sSTATE <= IDLE;
  89.                    
  90.                     --when others => sSTATE <= IDLE;
  91.                 end case;
  92.             end if;
  93.         end if;
  94.     end process;
  95.  
  96.     -- Binarni djelitelj
  97.     -- 12*10^6 = 2^x; log_2(12*10^6) = x; x = 24
  98.     Divider: process (iCLK, inRST) begin
  99.         if (inRST = '0') then
  100.             sDEVIDER <= (others => '0');
  101.             sENABLE_CS <= '0';
  102.         elsif (rising_edge(iCLK)) then
  103.             if (sDEVIDER = cDEVIDER_END) then
  104.                 sDEVIDER <= (others => '0');
  105.                 sENABLE_CS <= '1';
  106.             else
  107.                 sENABLE_CS <= '0';
  108.                 sDEVIDER <= sDEVIDER + 1;
  109.             end if;
  110.         end if;
  111.     end process;
  112.    
  113.     PWMClock: process(iCLK, inRST) begin
  114.         if (inRST = '0') then
  115.             sPWM_DEVIDER <= (others => '0');
  116.             sPWM_STATE <= P25;
  117.             sPWM_CLOCK <= '0';
  118.  
  119.         elsif (rising_edge(iCLK)) then
  120.             -- 75%
  121.             if (sPWM_STATE = P75) then
  122.                 if (sPWM_DEVIDER = 0) then
  123.                     sPWM_CLOCK <= '1';
  124.                 end if;
  125.            
  126.                 if (sPWM_DEVIDER = 2) then
  127.                     sPWM_DEVIDER <= (others => '0');
  128.                     sPWM_CLOCK <= '0';
  129.                 else
  130.                     sPWM_DEVIDER <= sPWM_DEVIDER + 1;
  131.                 end if;
  132.            
  133.             -- 25%
  134.             elsif (sPWM_STATE = P25) then
  135.                 if (sPWM_DEVIDER = 0) then
  136.                     sPWM_CLOCK <= '0';
  137.                 end if;
  138.            
  139.                 if (sPWM_DEVIDER = 2) then
  140.                     sPWM_DEVIDER <= (others => '0');
  141.                     sPWM_CLOCK <= '1';
  142.                 else
  143.                     sPWM_DEVIDER <= sPWM_DEVIDER + 1;
  144.                 end if;
  145.                
  146.             -- 50%
  147.             elsif (sPWM_STATE = P50) then
  148.                 sPWM_CLOCK <= not sPWM_CLOCK;
  149.             end if;
  150.         end if;
  151.     end process;
  152.  
  153.     PWMApply: process (sLEFT, sPWM_CLOCK) begin
  154.         if (sLEFT(0) = '1') then
  155.             oLEFT(0) <= sPWM_CLOCK;
  156.         else oLEFT(0) <= '0'; end if;
  157.        
  158.         if (sLEFT(1) = '1') then
  159.             oLEFT(1) <= sPWM_CLOCK;
  160.         else oLEFT(1) <= '0'; end if;
  161.        
  162.         if (sLEFT(2) = '1') then
  163.             oLEFT(2) <= sPWM_CLOCK;
  164.         else oLEFT(2) <= '0'; end if;
  165.     end process;
  166.  
  167.     -- Dodjela ukljucivanje dioda zavisno od stanja
  168.     sLEFT <= "000" when sSTATE = IDLE else
  169.         "001" when sSTATE = L1 else
  170.         "011" when sSTATE = L2 else
  171.         "111" when sSTATE = L3 else
  172.         "111" when sSTATE = LR3 else
  173.         "000";  -- R1, R2, R3
  174.  
  175.  
  176.     sRIGHT <= "000" when sSTATE = IDLE else
  177.         "100" when sSTATE = R1 else
  178.         "110" when sSTATE = R2 else
  179.         "111" when sSTATE = R3 else
  180.         "111" when sSTATE = LR3 else
  181.         "000";  -- L1, L2, L3
  182.        
  183. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement