Advertisement
Guest User

Untitled

a guest
Oct 7th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 2.31 KB | None | 0 0
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3. use IEEE.STD_LOGIC_ARITH.ALL;
  4. use IEEE.STD_LOGIC_UNSIGNED.ALL;
  5.  
  6. entity clkdiv is
  7. Generic (in_clk:  integer := 100000000;
  8.          out_clk: integer := 512000
  9.          );
  10.          
  11. port (clk: in std_logic;    
  12.       CKI: out std_logic;
  13.       SDO: out std_logic
  14.      );
  15. end clkdiv;
  16.  
  17. architecture Behavioral of clkdiv is
  18.  
  19. signal cntr : integer range 0 to in_clk/out_clk := 0;
  20. signal CKIOut : std_logic := '0';
  21. signal CKI_1 : std_logic := '0';
  22. signal start : std_logic := '0';
  23. signal data_cntr : integer range 0 to 151 := 0;
  24. signal bit_cntr : integer range 0 to 31 := 0;
  25.  
  26. begin
  27.  
  28. CKI <= CKIOut;  -- Output CKI
  29. start <= CKI_1 and (not CKIOut);
  30.  
  31. process(clk)
  32. begin
  33.     if clk= '1' and clk'event then
  34.         --Generate Clock
  35.         if (cntr < (in_clk/out_clk)/2) then
  36.             CKIOut <= '1';
  37.             cntr <= cntr + 1;
  38.         elsif (cntr = in_clk/out_clk) then
  39.             cntr <= 0;
  40.         else
  41.             cntr <= cntr + 1;
  42.             CKIOut <= '0';
  43.         end if;
  44.        
  45.         CKI_1 <= CKIOut; --delay CKI for 1 clock cycle (used for start pulse)
  46.     end if;
  47.    
  48.     if clk= '1' and clk'event then
  49.         if start = '1' then
  50.             if data_cntr = 0 then
  51.                 SDO <= '0';
  52.                 bit_cntr <= bit_cntr + 1;
  53.                 if bit_cntr = 31 then
  54.                     bit_cntr <= 0;
  55.                     data_cntr <= data_cntr + 1;
  56.                 end if;
  57.             elsif data_cntr > 0 and data_cntr < 151 then
  58.                 bit_cntr <= bit_cntr + 1;
  59.                 if bit_cntr < 8 then
  60.                     SDO <= '1'; --start
  61.                 elsif bit_cntr >= 8 and bit_cntr < 16 then
  62.                     SDO <= '1'; --brightness + blue
  63.                 elsif bit_cntr = 31 then
  64.                     bit_cntr <= 0;
  65.                     data_cntr <= data_cntr + 1;
  66.                 else
  67.                     SDO <= '0';
  68.                 end if;
  69.             elsif data_cntr = 151 then
  70.                 SDO <= '1';
  71.                 bit_cntr <= bit_cntr + 1;
  72.                 if bit_cntr = 31 then
  73.                     bit_cntr <= 0;
  74.                     data_cntr <= 0;
  75.                 end if;            
  76.             end if;
  77.      
  78.                
  79.         end if;
  80.     end if;
  81.    
  82.  
  83.  
  84. end process;
  85.  
  86. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement