how long it takes to turn a switch on and off


Nadav

Recommended Posts

hi Guys.

I am trying to solve the last challenge in chapter 10 (the binary counter) from hamster's book. I came up with the following code but it doesn't work so good:

------------------------------------------------------------------------- 

library IEEE;

use IEEE.STD_LOGIC_1164.ALL;

use IEEE.STD_LOGIC_UNSIGNED.ALL;

 

entity binary_up_counter is

Port ( switches : in STD_LOGIC_VECTOR(7 downto 0);

LEDs : out STD_LOGIC_VECTOR(7 downto 0);

clk : in STD_LOGIC

);

end binary_up_counter;

 

architecture Behavioral of binary_up_counter is

 

signal counter : STD_LOGIC_VECTOR(29 downto 0) := (others => '0');

signal sec_counter : STD_LOGIC_VECTOR(7 downto 0) := (others => '0');

begin

 

clk_proc: process(clk)

begin

if rising_edge(clk) then

counter <= counter+1;

if (counter = 32000000) then

if switches(0) = '1' then

sec_counter <= sec_counter+1;

counter <= "000000000000000000000000000000";

else 

sec_counter <= sec_counter;

end if;

end if;

if switches(1) = '1' then

sec_counter <= "00000000";

end if;

end if;

end process;

 

LEDs <= sec_counter(7 downto 0);

 

end Behavioral;

------------------------------------------------------------------

The counting starts only ~10 seconds after the time it should.

should I have try a different design with the switches inside the sensitivity list?

has anyone solved it befor?

Thanks

Nadav

Link to comment
Share on other sites

Hi! Found the error - it is in here

if (counter = 32000000) then  if switches(0) = '1' then     sec_counter <= sec_counter+1;     counter <= "000000000000000000000000000000";  else      sec_counter <= sec_counter;   end if;end if;

Counter only gets reset if the switches(1) is true, otherwise it counts off till the counter wraps around. Here's a fixed version...

if (counter = 31999999) then  if switches(0) = '1' then     sec_counter <= sec_counter+1;  end if;  counter <= (others => '0');else counter <= counter+1;end if;
 
or even this makes it a little clearer...
 
if counter = 0 and  switches(0) = '1' then   sec_counter <= sec_counter+1;end if; if (counter = 31999999) then  counter <= (others => '0');else  counter <= counter+1;end if;
 
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.