Advertisement
ddeexxiikk

PSC - Unsigned

Apr 1st, 2025 (edited)
939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 5.59 KB | Source Code | 0 0
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4.  
  5. entity lights is
  6.     port(
  7.         clk: in std_logic;
  8.         reset: in std_logic;
  9.         red: out std_logic;
  10.         orange: out std_logic;
  11.         green: out std_logic;
  12.         Cathodes: out std_logic_vector(6 downto 0);
  13.         AN0: out std_logic_vector(3 downto 0)
  14.     );
  15. end entity lights;
  16.  
  17. architecture Behaviour of lights is
  18.     type state_type is (RED_STATE, ORANGE_STATE, GREEN_STATE);
  19.     signal state: state_type := RED_STATE;
  20.  
  21.     -- Zwiększamy na 33 bity dla pewności
  22.     signal timer: unsigned(32 downto 0) := to_unsigned(4200000000, 33);
  23.     signal diff: unsigned(32 downto 0) := to_unsigned(4200000000, 33);
  24.    
  25.     signal time_10: integer range 0 to 9 := 0;
  26.     signal time_1: integer range 0 to 9 := 0;
  27.     signal Seven_Segment_10: std_logic_vector(6 downto 0);
  28.     signal Seven_Segment_1: std_logic_vector(6 downto 0);
  29.     signal DivAnods: integer range 0 to 1000000;
  30.     signal clkAN: std_logic := '0';
  31.  
  32.     -- Typ tablicy zmieniony na unsigned z 33 bitami
  33.     type t_vector is array (0 to 2) of unsigned(32 downto 0);
  34.     signal times_vector: t_vector := (to_unsigned(4000000000, 33),
  35.                                       to_unsigned(300000000, 33),
  36.                                       to_unsigned(4100000000, 33));
  37.     signal times_vector_rev: t_vector := (to_unsigned(4200000000 - 4000000000, 33),
  38.                                           to_unsigned(4200000000 - 300000000, 33),
  39.                                           to_unsigned(4200000000 - 4100000000, 33));
  40.  
  41. begin
  42.     Divclk: process(clk, Reset)
  43.     begin
  44.         if Reset = '1' then
  45.             DivAnods <= 0;
  46.             clkAn <= '0';
  47.         elsif rising_edge(clk) then
  48.             DivAnods <= DivAnods + 1;
  49.             if DivAnods = 100000 then
  50.                 DivAnods <= 0;         
  51.             elsif DivAnods > 49999 then
  52.                 clkAn <= '1';
  53.             else
  54.                 clkAn <= '0';
  55.             end if;
  56.         end if;
  57.     end process;
  58.  
  59.     process(clkAn, timer)
  60.     begin
  61.         -- Konwersja unsigned na integer dla wyświetlania
  62.         time_10 <= to_integer((timer - diff) / 1000000000);
  63.         time_1 <= to_integer(((timer - diff) / 100000000) mod 10);
  64.  
  65.         case time_10 is
  66.             when 0 => Seven_Segment_10 <= "1000000"; -- 0
  67.             when 1 => Seven_Segment_10 <= "1111001"; -- 1
  68.             when 2 => Seven_Segment_10 <= "0100100"; -- 2
  69.             when 3 => Seven_Segment_10 <= "0110000"; -- 3
  70.             when 4 => Seven_Segment_10 <= "0011001"; -- 4
  71.             when 5 => Seven_Segment_10 <= "0010010"; -- 5
  72.             when 6 => Seven_Segment_10 <= "0000010"; -- 6
  73.             when 7 => Seven_Segment_10 <= "1111000"; -- 7
  74.             when 8 => Seven_Segment_10 <= "0000000"; -- 8
  75.             when 9 => Seven_Segment_10 <= "0010000"; -- 9
  76.             when others => Seven_Segment_10 <= "1000000"; -- null
  77.         end case;
  78.  
  79.         case time_1 is
  80.             when 0 => Seven_Segment_1 <= "1000000"; -- 0
  81.             when 1 => Seven_Segment_1 <= "1111001"; -- 1
  82.             when 2 => Seven_Segment_1 <= "0100100"; -- 2
  83.             when 3 => Seven_Segment_1 <= "0110000"; -- 3
  84.             when 4 => Seven_Segment_1 <= "0011001"; -- 4
  85.             when 5 => Seven_Segment_1 <= "0010010"; -- 5
  86.             when 6 => Seven_Segment_1 <= "0000010"; -- 6
  87.             when 7 => Seven_Segment_1 <= "1111000"; -- 7
  88.             when 8 => Seven_Segment_1 <= "0000000"; -- 8
  89.             when 9 => Seven_Segment_1 <= "0010000"; -- 9
  90.             when others => Seven_Segment_1 <= "1000000"; -- null
  91.         end case;
  92.  
  93.         if clkAn = '1' then
  94.             Cathodes <= Seven_Segment_1;
  95.             AN0 <= "1110";
  96.         else
  97.             Cathodes <= Seven_Segment_10;
  98.             AN0 <= "1101";
  99.         end if;
  100.     end process;
  101.  
  102.     process(clk, reset)
  103.         variable temp_diff: unsigned(32 downto 0); -- Zmienna pomocnicza do obliczeń
  104.     begin
  105.         if reset = '1' then
  106.             timer <= to_unsigned(4200000000, 33);
  107.             state <= RED_STATE;
  108.         elsif rising_edge(clk) then
  109.             timer <= timer - 1;
  110.             case state is
  111.                 when RED_STATE =>
  112.                     red <= '1';
  113.                     orange <= '0';
  114.                     green <= '0';
  115.                     temp_diff := to_unsigned(4200000000, 33) - times_vector(0) - times_vector(1);
  116.                     diff <= temp_diff;
  117.                     if timer = times_vector_rev(0) then
  118.                         timer <= to_unsigned(4200000000, 33);
  119.                         state <= ORANGE_STATE;
  120.                     end if;
  121.                 when ORANGE_STATE =>
  122.                     red <= '1';
  123.                     orange <= '1';
  124.                     green <= '0';
  125.                     temp_diff := to_unsigned(4200000000, 33) - times_vector(1);
  126.                     diff <= temp_diff;
  127.                     if timer = times_vector_rev(1) then
  128.                         timer <= to_unsigned(4200000000, 33);
  129.                         state <= GREEN_STATE;
  130.                     end if;
  131.                 when GREEN_STATE =>
  132.                     red <= '0';
  133.                     orange <= '0';
  134.                     green <= '1';
  135.                     temp_diff := to_unsigned(4200000000, 33) - times_vector(2);
  136.                     diff <= temp_diff;
  137.                     if timer = times_vector_rev(2) then
  138.                         timer <= to_unsigned(4200000000, 33);
  139.                         state <= RED_STATE;
  140.                     end if;
  141.             end case;
  142.         end if;
  143.     end process;
  144. end Behaviour;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement