library IEEE; use IEEE.std_logic_1164.all; use IEEE.std_logic_arith.all; use IEEE.std_logic_unsigned.all; entity CICLOCOMPUTER is port ( GIROCOMPIUTO : in std_logic; CLOCK : in std_logic; RESET : in std_logic; MODE : in std_logic; VALORE : out unsigned(15 downto 0) ); end CICLOCOMPUTER; architecture A of CICLOCOMPUTER is signal cont_distanza, next_distanza, giri_sec, next_giri_sec, giri_sec_frozen : unsigned(14 downto 0); signal cicli, next_cicli : unsigned(14 downto 0); signal wave1Hz : std_logic; type modo_funzionamento is (velocita, distanza); signal current_state, next_state : modo_funzionamento; signal select_output : std_logic; begin -- contatore della distanza process(clock, reset) begin if clock'event and clock = '1' then if reset = '1' then cont_distanza <= conv_unsigned(0,15); elsif girocompiuto = '1' then cont_distanza <= next_distanza; end if; end if; end process; next_distanza <= cont_distanza + conv_unsigned(1,15); -- contatore della velocita, ogni secondo azzero il conteggio process(clock, reset) begin if clock'event and clock = '1' then if reset = '1' or wave1Hz = '1' then giri_sec <= conv_unsigned(0,15); elsif girocompiuto = '1' then giri_sec <= next_giri_sec; end if; end if; end process; next_giri_sec <= giri_sec + conv_unsigned(1,15); -- ogni secondo vado a campionare i giri calcolati process(clock, reset) begin if clock'event and clock = '1' then if reset = '1' then giri_sec_frozen <= conv_unsigned(0,15); elsif wave1Hz = '1' then giri_sec_frozen <= giri_sec; end if; end if; end process; -- macchina a stati per commutare tra distanza e velocita process(clock, reset) begin if clock'event and clock = '1' then if reset = '1' then current_state <= distanza; else current_state <= next_state; end if; end if; end process; process(current_state, mode) begin case current_state is when velocita => if mode = '1' then next_state <= distanza; else next_state <= velocita; end if; select_output <= '0'; when distanza => if mode = '1' then next_state <= velocita; else next_state <= distanza; end if; select_output <= '1'; when others => next_state <= distanza; select_output <= '0'; end case; end process; process(clock, reset) begin if clock'event and clock = '1' then if reset = '1' then cicli <= conv_unsigned(0,15); else cicli <= next_cicli; end if; end if; end process; next_cicli <= conv_unsigned(0,15) when cicli = conv_unsigned(9,15) else cicli + conv_unsigned(1,15); wave1Hz <= '1' when cicli = conv_unsigned(0,15) else '0'; -- seleziono il valore da portare in uscita. Devo convertire da giri a metri, -- ma essendo 1 giro = 2 metri, basta shiftare a sinistra di 1; valore(0) <= '0'; valore(15 downto 1) <= cont_distanza when select_output = '0' else giri_sec_frozen; end A;