Estoy codificando para un acelerador y un velocímetro. Actualmente estoy tratando de evitar que el velocímetro y la transmisión disminuyan por debajo de 0 y permanezcan en 0. Creo que puedo crear sentencias if que lo mantendrán por encima de 0, pero hay una manera más eficiente de hacerlo. También estoy tratando de asignar el velocímetro a dos pantallas de 7 segmentos usando el código para la operación en un FPGA. Si puedes ayudar, te lo agradecería. Gracias. CÓDIGO EDITADO
LIBRARY IEEE;
USE IEEE.NUMERIC_STD.ALL;
USE IEEE.STD_LOGIC_1164.all;
USE IEEE.STD_LOGIC_UNSIGNED.all;
ENTITY GearCounterFirst IS
PORT(
CLK: IN STD_LOGIC;
gas: IN STD_LOGIC;
up: IN STD_LOGIC;
down: IN STD_LOGIC;
trans_o: OUT STD_LOGIC_VECTOR (2 downto 0);
speed_o: OUT STD_LOGIC_VECTOR (7 downto 0);
acc_o: OUT STD_LOGIC_VECTOR (4 downto 0));
END GearCounterFirst;
ARCHITECTURE Behavior OF GearCounterFirst IS
signal trans: std_logic_vector(2 downto 0) := "000";
signal speed: std_LOGIC_VECTOR(7 downto 0) := "00000000";
signal acc : std_LOGIC_VECTOR(4 downto 0) := "00000";
BEGIN
PROCESS (CLK)
BEGIN
if (rising_edge(CLK)) then
if (up = '1') then
if (unsigned(trans) < 5) then
trans <= STD_LOGIC_VECTOR(unsigned(trans) + 1);
elsif (unsigned(trans) = 5) then
trans <= trans;
end if;
elsif (down = '1') then
if (unsigned (trans) > 0) then
trans <= STD_LOGIC_VECTOR(unsigned(trans) - 1);
elsif (unsigned (trans) = 0) then
trans <= trans;
end if;
else
trans <= trans;
end if;
case trans is
when "000" =>
if (gas = '1') then
if (unsigned(speed) > 0) then
speed <= STD_LOGIC_VECTOR(unsigned(speed) - 2);
acc <= "00000";
elsif (unsigned(speed) = 0 ) then
speed <= speed;
acc <= "00000";
end if;
elsif (gas = '0') then
if (unsigned(speed) > 0) then
speed <= STD_LOGIC_VECTOR(unsigned(speed) - 2);
acc <= "00000";
elsif (unsigned(speed) = 0 ) then
speed <= speed;
acc <= "00000";
end if;
end if;
when "001" => -- Trans in 1st gear
if (gas = '1') then --gas is presed
if (unsigned(speed) < 15) then -- Increase until 15
speed <= STD_LOGIC_VECTOR(unsigned(speed) + 5);
acc <= "11111";
end if;
elsif (gas = '0') then -- gas is not pressed
if (unsigned(speed) > 0) then -- decrease until speed is 0
speed <= STD_LOGIC_VECTOR(unsigned(speed) - 2);
acc <= "00000";
elsif (unsigned(speed) = 0) then -- at 0 speed, stay at 0
speed <= "00000000";
acc <= "00000";
end if;
end if;
when "010" => -- Trans in 2nd gear
if (gas = '1') then
if (unsigned(speed) < 35) then
speed <= STD_LOGIC_VECTOR(unsigned(speed) + 4);
acc <= "01111";
end if;
elsif (gas = '0') then
speed <= STD_LOGIC_VECTOR(unsigned(speed) - 2);
acc <= "00000";
end if;
when "011" => -- Trans in 3rd gear
if (gas = '1') then
if (speed < 60) then
speed <= STD_LOGIC_VECTOR(unsigned(speed) + 3);
acc <= "00111";
end if;
elsif (gas = '0') then
if (speed > 0) then
speed <= STD_LOGIC_VECTOR(unsigned(speed) - 2);
acc <= "00000";
end if;
end if;
when "101" => -- Trans in 4th gear
if (gas = '1') then
speed <= STD_LOGIC_VECTOR(unsigned(speed) + 2);
acc <= "00011";
elsif (gas = '0') then
speed <= STD_LOGIC_VECTOR(unsigned(speed) - 2);
acc <= "00000";
end if;
when "110" => -- Trans in 5th gear
if (gas = '1') then
speed <= STD_LOGIC_VECTOR(unsigned(speed) + 1);
acc <= "00001";
elsif (gas = '0') then
if (unsigned(speed) > 0) then
speed <= STD_LOGIC_VECTOR(unsigned(speed) - 2);
acc <= "00000";
elsif (unsigned(speed) = 0 ) then
speed <= speed;
acc <= "00000";
end if;
end if;
when others =>
if (gas = '1') then
if (unsigned(speed) > 0) then
speed <= STD_LOGIC_VECTOR(unsigned(speed) - 2);
acc <= "00000";
elsif (unsigned(speed) = 0 ) then
speed <= speed;
acc <= "00000";
end if;
elsif (gas = '0') then
if (unsigned(speed) > 0) then
speed <= STD_LOGIC_VECTOR(unsigned(speed) - 2);
acc <= "00000";
elsif (unsigned(speed) = 0 ) then
speed <= speed;
acc <= "00000";
end if;
end if;
end case;
end if;
trans_o <= trans;
acc_o <= acc;
speed_o <= speed;
END PROCESS;
End Behavior;