Making a counter using VHDL

OP #7000555
Rate this post
• ▲ useful
▼ not useful
I want to make a counter that on every rising clock edge, adds 1 to my 
'counter' out and resets on a rising edge if 'reset = 1'. I had problems 
with working directly on the 'counter', so I have the extra signal 
'countr' as a temp.
However, when I run that code, it only adds 1 every two rising edges. 
Can someone explain why?
1
library ieee;
2
use ieee.std_logic_1164.all;
3
use ieee.numeric_std.all;
4

5
entity Counter is
6
port (
7
    clk : in std_logic;
8
    reset : in std_logic;
9
    count : out std_logic_vector(3 downto 0)
10
);
11
end Counter;
12

13
architecture rtl of Counter is
14
signal countr : std_logic_vector(3 downto 0);
15

16
begin
17
    process(clk)
18
    begin
19
        if(rising_edge(clk)) then
20
            countr <= std_logic_vector(to_unsigned(to_integer(unsigned(countr)) + 1, 4));
21
            if(reset = '1') then
22
                countr <= (others => '0');
23
            end if;
24
        end if;
25
        count <= countr;
26
    end process;
27
end architecture rtl;
Guest #7000559
Rate this post
• ▲ useful
▼ not useful
The assignment count <= countr is inside the process, which is only 
activated by simulator when the clk signal changes (see sensitivity 
list).

Just write it outside the process:
1
library ieee;
2
use ieee.std_logic_1164.all;
3
use ieee.numeric_std.all;
4

5
entity Counter is
6
port (
7
    clk : in std_logic;
8
    reset : in std_logic;
9
    count : out std_logic_vector(3 downto 0)
10
);
11
end entity Counter;
12

13
architecture rtl of Counter is
14

15
    signal countr : unsigned(3 downto 0);
16

17
begin
18

19
    process(clk)
20
    begin
21
        if(rising_edge(clk)) then
22
            countr <= countr + 1;
23
            if reset = '1' then
24
                countr <= (others => '0');
25
            end if;
26
        end if;
27
    end process;
28

29
    count <= std_logic_vector( countr);
30

31
end architecture rtl;

Duke

Reply

Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.

Formatting
  • [c]C-Code[/c]
  • [vhdl]VHDL-Code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.