Advertisement
bremenpl

LFSR

Nov 16th, 2016
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 5.46 KB | None | 0 0
  1. -- Projekt 1
  2. -- Łukasz Przeniosło, EiT3
  3. -- 26.12.2012
  4.  
  5. library IEEE;
  6. use IEEE.STD_LOGIC_1164.ALL;
  7. use IEEE.STD_LOGIC_ARITH.ALL;
  8. use IEEE.STD_LOGIC_UNSIGNED.ALL;
  9. use IEEE.NUMERIC_STD.ALL;
  10.  
  11. entity proj1counter is
  12.     port (
  13.              -- OUT's
  14.              leds : out STD_LOGIC_VECTOR (6 downto 0);
  15.              anodes : out STD_LOGIC_VECTOR (3 downto 0);
  16.              colon : out STD_LOGIC;
  17.              
  18.              debug_led : out STD_LOGIC_VECTOR (0 to 5);
  19.              
  20.              -- IN's
  21.              pb1 : in STD_LOGIC;
  22.              pb2 : in STD_LOGIC;
  23.              CLK : in STD_LOGIC
  24.           );
  25. end proj1counter;
  26.  
  27. architecture Behavioral of proj1counter is
  28.  
  29. -- types:
  30. type led_array is array (15 downto 0) of STD_LOGIC_VECTOR (6 downto 0);
  31.  
  32. -- signals:
  33. signal base_prescaler : STD_LOGIC_VECTOR (24 downto 0); -- base frequency prescaler
  34. signal display_prescaler : STD_LOGIC_VECTOR (24 downto 0); -- display frequency prescaler
  35. signal display_nr : STD_LOGIC_VECTOR (1 downto 0); -- used to chose active display
  36. signal key_lock, key_lock2 : BIT; -- debounce help signal.
  37.  
  38. -- flag signals:
  39. signal counter_full : BIT := '1'; -- 1 means full;
  40. signal digit_clear : BIT; -- 1 means clear.
  41. signal lcd_stop : BIT; -- 1 means lcd stop.
  42. signal db_pb1, db_pb2 : BIT;
  43.  
  44. signal digit0, digit1, digit2, digit3 : integer range 0 to 15;
  45. signal lfsr0 : STD_LOGIC_VECTOR (3 downto 0) := "0001"; -- Linear feedback shift register 0
  46. signal lfsr1 : STD_LOGIC_VECTOR (3 downto 0) := "1110"; -- Linear feedback shift register 1
  47. signal lfsr2 : STD_LOGIC_VECTOR (3 downto 0) := "1100"; -- Linear feedback shift register 2
  48. signal lfsr3 : STD_LOGIC_VECTOR (3 downto 0) := "1010"; -- Linear feedback shift register 3
  49.                                                
  50. constant my_led_array : led_array := ("0001110", -- F
  51.                                                   "0000110", -- E
  52.                                                   "0100001", -- d
  53.                                                   "1000110", -- C
  54.                                                   "0000011", -- b
  55.                                                   "0001000", -- A
  56.                                                   "0010000", -- 9
  57.                                                   "0000000", -- 8
  58.                                                   "1111000", -- 7
  59.                                                   "0000010", -- 6
  60.                                                   "0010010", -- 5
  61.                                                   "0011001", -- 4
  62.                                                   "0110000", -- 3
  63.                                                   "0100100", -- 2
  64.                                                   "1111001", -- 1
  65.                                                   "1000000"); --0
  66.  
  67. -- DEBOUNCE
  68. type State_Type is (S0, S1);
  69. -- 1                                                 
  70. signal State : State_Type := S0;
  71. signal DPB, SPB : STD_LOGIC;
  72. signal DReg : STD_LOGIC_VECTOR (7 downto 0);
  73.  
  74. -- 2
  75. signal State2 : State_Type := S0;
  76. signal DPB2, SPB2 : STD_LOGIC;
  77. signal DReg2 : STD_LOGIC_VECTOR (7 downto 0);
  78.  
  79. -- ###########################################################################################
  80.  
  81. begin
  82.    
  83.     -- switches the lcd panels, controls the dot, freezes/ defreezes the lcd.
  84.     display_switch : process (CLK)
  85.         begin
  86.                
  87.             if rising_edge(CLK) then
  88.            
  89.                 -- Fibonacci LFSR
  90.                 lfsr3(3) <= (lfsr1(1) xor (lfsr0(3) xor (lfsr0(0) xor lfsr0(2))));
  91.                 lfsr3(2) <= lfsr3(3);
  92.                 lfsr3(1) <= lfsr3(2);
  93.                 lfsr3(0) <= lfsr3(1);
  94.                 lfsr2(3) <= lfsr3(0);
  95.                 lfsr2(2) <= lfsr2(3);
  96.                 lfsr2(1) <= lfsr2(2);
  97.                 lfsr2(0) <= lfsr2(1);
  98.                 lfsr1(3) <= lfsr2(0);
  99.                 lfsr1(2) <= lfsr1(3);
  100.                 lfsr1(1) <= lfsr1(2);
  101.                 lfsr1(0) <= lfsr1(1);
  102.                 lfsr0(3) <= lfsr0(0);
  103.                 lfsr0(2) <= lfsr0(3);
  104.                 lfsr0(1) <= lfsr0(2);
  105.                 lfsr0(0) <= lfsr0(1);
  106.            
  107.                 display_prescaler <= display_prescaler + 1;
  108.                
  109.                 if (display_prescaler = 100000) then
  110.                     display_prescaler <= (others => '0');
  111.                    
  112.                     case display_nr is
  113.                         when "00" =>
  114.                                         leds <= my_led_array(digit0);
  115.                                         anodes <= "0111";
  116.                                         colon <= '1';
  117.                         when "01" =>
  118.                                         leds <= my_led_array(digit1);
  119.                                         anodes <= "1011";
  120.                                         colon <= '1';
  121.                         when "10" =>
  122.                                         leds <= my_led_array(digit2);
  123.                                         anodes <= "1101";
  124.                                         colon <= '1';
  125.                         when "11" =>
  126.                                         leds <= my_led_array(digit3);
  127.                                         anodes <= "1110";
  128.                                         colon <= '1';
  129.                         when others => null;
  130.                     end case;
  131.            
  132.                     display_nr <= display_nr + 1; -- self pb2 after display_nr = 3.
  133.            
  134.                 end if;
  135.             end if;
  136.            
  137.         end process display_switch;
  138.            
  139. -- ###########################################################################################
  140.    
  141.     -- debounce elimination for pb1.
  142.     debouncing1 : process (CLK, pb1)
  143.         variable SDC : integer;
  144.         constant Delay : integer := 50000;
  145.         begin
  146.        
  147.             if CLK'Event and CLK = '1' then
  148.             -- Double latch input signal
  149.                 DPB <= SPB;
  150.                 SPB <= pb1;
  151.  
  152.                 case State is
  153.                     when S0 =>
  154.                         DReg <= DReg(6 downto 0) & DPB;
  155.  
  156.                         SDC := Delay;
  157.  
  158.                         State <= S1;
  159.                     when S1 =>
  160.                         SDC := SDC - 1;
  161.  
  162.                         if SDC = 0 then
  163.                             State <= S0;
  164.                         end if;
  165.                     when others =>
  166.                         State <= S0;
  167.                 end case;
  168.  
  169.                 if DReg = X"FF" then
  170.                     db_pb1 <= '1';
  171.                 elsif DReg = X"00" then
  172.                     db_pb1 <= '0';
  173.                 end if;
  174.             end if;
  175.     end process debouncing1;
  176.    
  177. -- ###########################################################################################
  178.    
  179.     -- buttons actions.
  180.     buttons : process (CLK)
  181.         begin          
  182.             if CLK'Event and CLK = '1' then
  183.                 if key_lock = '0' and db_pb1 = '1' then
  184.                     key_lock <= '1';
  185.                     debug_led <= "100000";
  186.                    
  187.                     digit0 <= conv_integer(unsigned(lfsr3));
  188.                     digit1 <= conv_integer(unsigned(lfsr2));
  189.                     digit2 <= conv_integer(unsigned(lfsr1));
  190.                     digit3 <= conv_integer(unsigned(lfsr0));
  191.                    
  192.                     -- btn pressed
  193.                    
  194.                 elsif key_lock = '1' and db_pb1 = '0' then
  195.                     key_lock <= '0';
  196.                     debug_led <= "000000";
  197.                    
  198.                     -- btn released
  199.                        
  200.                 end if;
  201.             end if;
  202.     end process buttons;
  203.    
  204.  
  205. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement