donwazonesko
donwazonesko

Reputation: 135

VHDL Clock problem while creating modulo 16 counter

I've created this simple mod16 counter using basys3 board and something is not right with my clock. The code itself do works, however one count (changing from "1" to "2" etc.) last 40 seconds, instead of 1 second! I've tried to lower the "clk_vector" if condition to 1 but it didn't help either.

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;

entity mod_16_k is
    Port ( switch : in STD_LOGIC_VECTOR (3 downto 0);
           CLK1 : in STD_LOGIC;
           reset : in STD_LOGIC;
           led : out STD_LOGIC_VECTOR (15 downto 0));
end mod_16_k;
architecture Behavioral of mod_16_k is

signal      clk_vector   :integer;
signal      clk_vec2      :std_logic_vector(15 downto 0); 

begin
zegar_wew : process(CLK1)
begin

if(CLK1'event and CLK1 = '1')  then
    clk_vector <= clk_vector + 1;
        if(clk_vector = 100000000) then
            clk_vec2 <= std_logic_vector(unsigned(clk_vec2)  + 1);
        end if;
end if;
end process;
led <= clk_vec2;
end Behavioral;

The .XDC lines for clock are: enter image description here

If we check the basys3 datasheet, the clock is connected to "W5" port. enter image description here

Do you have any idea, what might be the problem in here? It might be connected with detecting the rising edge of an clk, however all of the changes (from 1 to 2 etc.) last ~40 seconds.

Upvotes: 2

Views: 1227

Answers (1)

Jonathan Drolet
Jonathan Drolet

Reputation: 3388

This is because you forget to reset clk_vector once it reaches 1 second. Because it is an integer, it is 32 bits, thus will count 2^32 instead of 100000000.

This should work:

If(CLK1'event and CLK1 = '1')  then
        if(clk_vector = 100000000-1) then
            clk_vector <= 0;
            clk_vec2 <= std_logic_vector(unsigned(clk_vec2)  + 1);
        else
                clk_vector <= clk_vector + 1;
        end if;
end if;

Also, notice that to count 1 second, you need to count up to 100000000-1, we start counting from zero!

Upvotes: 5

Related Questions