library IEEE; use IEEE.std_logic_1164.all; use IEEE.std_logic_arith.all; use IEEE.std_logic_unsigned.all; entity CONTROLLO_TRENINO is port ( LEVA : in std_logic_vector(3 downto 0); PULSANTE : in std_logic; SPEED : out std_logic; SUONO : out std_logic; CLK : in std_logic; RESET : in std_logic ); end CONTROLLO_TRENINO; architecture A of CONTROLLO_TRENINO is type stato_type is (normale, maxspeed, riscaldamento); signal conteggio, nconteggio : unsigned(1 downto 0); signal wave0, wave1, wave2, wave3, wave4 : std_logic; signal manuale, n_manuale : std_logic; signal surriscaldamento : std_logic; signal stato, stato_successivo : stato_type; signal timeout_count, next_timeout_count : unsigned(15 downto 0); signal reset_timeout_count, timeout : std_logic; begin SUONO <= PULSANTE; process (clk) begin if clk'event and clk = '1' then if reset_timeout_count = '1' then timeout_count <= conv_unsigned(0,16); else timeout_count <= next_timeout_count; end if; end if; end process; next_timeout_count <= timeout_count + conv_unsigned(1,16) when timeout = '0' else conv_unsigned(0,16); timeout <= '1' when timeout_count = conv_unsigned(10,16) else '0'; process(clk) begin if clk'event and clk = '1' then conteggio <= nconteggio; end if; end process; nconteggio <= conteggio + conv_unsigned(1,2); wave0 <= '0'; wave4 <= '1'; process(conteggio) variable cont_int : integer; begin cont_int := conv_integer(conteggio); if cont_int = 0 then wave1 <= '1'; wave2 <= '1'; wave3 <= '1'; elsif cont_int = 1 then wave1 <= '0'; wave2 <= '1'; wave3 <= '1'; elsif cont_int = 2 then wave1 <= '0'; wave2 <= '0'; wave3 <= '1'; elsif cont_int = 3 then wave1 <= '0'; wave2 <= '0'; wave3 <= '0'; else wave1 <= '1'; wave2 <= '1'; wave3 <= '1'; end if; end process; process(clk) begin if clk'event and clk = '1' then stato <= stato_successivo; end if; end process; process(stato,leva(3)) begin case stato is when normale => if LEVA(0)='1' then SPEED <= wave1; elsif LEVA(1) = '1' then SPEED <= wave2; elsif LEVA(2) = '1' then SPEED <= wave3; elsif LEVA(3) = '1' then SPEED <= wave4; else SPEED <= '0'; end if; if LEVA(3) = '1' then stato_successivo <= maxspeed; else stato_successivo <= normale; end if; reset_timeout_count <= '1'; when maxspeed => reset_timeout_count <= '0'; if timeout = '1' then stato_successivo <= riscaldamento; elsif LEVA(3) = '1' then stato_successivo <= maxspeed; else stato_successivo <= normale; end if; SPEED <= wave4; when riscaldamento => reset_timeout_count <= '0'; if timeout = '1' then stato_successivo <= normale; else stato_successivo <= riscaldamento; end if; if leva(0) = '1' or leva(1) = '1' or leva(2) = '1' or leva(3) = '1' then SPEED <= wave1; else SPEED <= wave0; end if; when others => SPEED <= wave0; stato_successivo <= normale; end case; end process; end A;