how long it takes to turn a switch on and off


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;




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');



clk_proc: process(clk)


if rising_edge(clk) then

counter <= counter+1;

if (counter = 32000000) then

if switches(0) = '1' then

sec_counter <= sec_counter+1;

counter <= "000000000000000000000000000000";


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?



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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.