SHARE
TWEET

Lab4_DeanNguyen

a guest Oct 9th, 2019 129 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Author: Dean Nguyen
  2.  
  3. LIBRARY ieee;
  4. USE ieee.std_logic_1164.ALL;
  5. USE ieee.std_logic_unsigned.ALL;
  6. USE ieee.numeric_std.ALL;
  7.  
  8. Entity servo_controller IS
  9.     PORT(
  10.         clk                 : in std_logic;                             -- 50 Mhz system clock
  11.         reset_n             : in std_logic;                             -- active low system rese
  12.         write                   : in std_logic;                             -- active high write enable
  13.         writedata           : in std_logic_vector(31 downto 0); --data from the CPU to be stored in the component
  14.         address             : in std_logic;                             --address of register to be written to (from CPU)
  15.        
  16.         out_wave_export : out std_logic;
  17.         irq                     : out std_logic                         --signal to interrupt the processor
  18.         );
  19. End Entity servo_controller;
  20.  
  21. Architecture behavioral of servo_controller is
  22.    
  23.     Type servo_loc is array(1 downto 0) of std_logic_vector(31 downto 0);
  24.     Signal Registers : servo_loc;
  25.    
  26.     Type state_type is (Sweep_Right, Int_Right, Sweep_Left, Int_Left);
  27.     Signal Current_state, Next_state : state_type;
  28.    
  29.     Signal out_wave : std_logic;
  30.    
  31.     constant FORTY_FIVE             : std_logic_vector(31 downto 0) := x"0000C350";
  32.     constant ONE_THIRTY_FIVE    : std_logic_vector(31 downto 0) := x"000186A0";
  33.    
  34.     signal min_angle_count  : std_logic_vector(31 downto 0);
  35.     signal max_angle_count  : std_logic_vector(31 downto 0);
  36.     signal angle_count      : std_logic_vector(31 downto 0);
  37.     signal pulse_count      : integer;
  38.     signal servo_direction  : integer;
  39.  
  40. Begin
  41.  
  42.     out_wave_export <= out_wave;
  43.    
  44.     --this process loads data from the CPU.  The CPU provides the address,
  45.     --the data and the write enable signal
  46.     PROCESS(clk, reset_n, min_angle_count, max_angle_count, angle_count)
  47.     BEGIN
  48.         IF (reset_n = '0') THEN
  49.             --Registers <= (OTHERS => "00000000000000000000000000000000");
  50.             Registers(0) <= FORTY_FIVE;     -- 50000     (45 degrees)
  51.             Registers(1) <= ONE_THIRTY_FIVE;    -- 100000 (135 degrees)
  52.         ELSIF (clk'event AND clk = '1') THEN
  53.             IF (write = '1') THEN
  54.                 if (address = '0') then
  55.                     Registers(0) <= writedata;
  56.                 elsif (address = '1') then
  57.                     Registers(1) <= writedata;
  58.                 end if;
  59.             --Registers(to_integer(unsigned(address))) <= writedata;
  60.             --when write enable is active, the ram location at the given address
  61.             --is loaded with the input data
  62.             END IF;
  63.         END IF;
  64.         min_angle_count <= Registers(0); -- sets value of min angle
  65.         max_angle_count <= Registers(1); -- sets value of max angle
  66.     END PROCESS;
  67.    
  68.     counter_proc : process(clk, reset_n, out_wave, pulse_count, angle_count, max_angle_count, min_angle_count, servo_direction)
  69.     begin
  70.         if(reset_n = '0') then
  71.             angle_count <= min_angle_count;     -- sets the angle count to value of min angle
  72.             servo_direction <= 0;
  73.             pulse_count <= 0;
  74.         elsif(rising_edge(clk) AND clk = '1') then
  75.             pulse_count <= pulse_count + 1;
  76.             if(500000 = pulse_count) then
  77.                 if((angle_count <= max_angle_count) and (servo_direction = 0)) then
  78.                     angle_count <= angle_count + 10000;
  79.                     if(angle_count = max_angle_count) then
  80.                         servo_direction <= 1;
  81.                     end if;
  82.                 elsif((angle_count >= min_angle_count) and (servo_direction = 1)) then
  83.                     angle_count <= angle_count - 10000;
  84.                     if(angle_count = min_angle_count) then
  85.                         servo_direction <= 0;
  86.                     end if;
  87.                 end if;
  88.                 pulse_count <= 0;
  89.             end if;
  90.         end if;
  91.        
  92.         if(pulse_count < angle_count) then
  93.             out_wave <= '1';
  94.         else
  95.             out_wave <= '0';
  96.         end if;
  97.     end process;
  98.    
  99.     sync_states : Process(clk,reset_n)
  100.     begin
  101.         If (reset_n = '0') then
  102.             Current_state <= Sweep_Right;
  103.         Elsif (rising_edge(clk)) then
  104.             Current_state <= Next_state;
  105.         End If;
  106.     end process;
  107.    
  108.     state_order : process(write, angle_count, max_angle_count, min_angle_count)
  109.     begin
  110.         Case(Current_state) is
  111.             When Sweep_Right =>
  112.                 If(angle_count >= max_angle_count) then
  113.                     Next_state <= Int_Right;
  114.                 else
  115.                     Next_state <= Sweep_Right;
  116.                 End if;
  117.             When Int_Right =>
  118.                 If(write = '1') then
  119.                     Next_state <= Sweep_Left;
  120.                 Else
  121.                     Next_state <= Int_Right;
  122.                 End if;
  123.             When Sweep_Left =>
  124.                 If(angle_count <= min_angle_count) then
  125.                     Next_state <= Int_Left;
  126.                 Else
  127.                     Next_state <= Sweep_Left;
  128.                 End if;
  129.             When Int_Left =>
  130.                 If(write = '1') then
  131.                     Next_state <= Sweep_Right;
  132.                 Else
  133.                     Next_state <= Int_Left;
  134.                 End if;
  135.         End Case;
  136.     end process;
  137.    
  138.     state_do : Process(Next_state, reset_n, clk)
  139.     begin
  140.         if(rising_edge(clk)) then
  141.             Case(Next_state) is
  142.                 When(Sweep_Right) =>
  143.                     irq <= '0';
  144.                 When(Int_Right) =>
  145.                     irq <= '1';
  146.                 When(Sweep_Left) =>
  147.                     irq <= '0';
  148.                 When(Int_Left) =>
  149.                     irq <= '1';
  150.             End Case;
  151.         end if;
  152.     end process;
  153.        
  154. End Architecture behavioral;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top