Nadav 0 Report post Posted May 1, 2014 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 Share this post Link to post Share on other sites
hamster 47 Report post Posted May 1, 2014 Hi Nadav That is looking pretty good - I can't see why the 10 second pause. I'll get my board out and try it... Mike PS. You might want to use 31999999 not 32000000. The signals are only updated when the process exits... Share this post Link to post Share on other sites
hamster 47 Report post Posted May 2, 2014 Hi! Found the error - it is in hereif (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; Share this post Link to post Share on other sites
Nadav 0 Report post Posted May 2, 2014 thanks a lot!!!! Now I got it working! Share this post Link to post Share on other sites