EmbDev.net

Forum: FPGA, VHDL & Verilog 8-bit counter with enable VHDL


von Dmitry O. (Company: VGTU) (dimsan)


Rate this post
0 useful
not useful
Hi guys.I need to write code for counter with input ENABLE and 
synchronous reset. The сounter must count up to 19. I have this code, 
but I don't understand where there is an error. Help me please anybody 
=)
1
library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
use IEEE.STD_LOGIC_UNSIGNED.ALL;
4
 
5
entity UP_COUNTER is
6
    Port ( clk: in std_logic; -- clock input
7
           reset: in std_logic; -- reset input 
8
           ce: in std_logic; -- enable input
9
           counter: out std_logic_vector(7 downto 0) -- output 4-bit counter
10
     );
11
end UP_COUNTER;
12
13
architecture Behavioral of UP_COUNTER is
14
    signal counter_up: std_logic_vector(7 downto 0);
15
begin
16
-- up counter
17
process(clk)
18
begin
19
    if rising_edge(clk) then
20
        if reset = '1' then
21
            counter_up <= (others=>'0');
22
        elsif ce = '1' then
23
            M1: for i in 0 to 19 
24
          loop
25
          counter_up <= counter_up + i;
26
        end loop;
27
        end if;
28
    end if;
29
 end process;
30
    counter <= counter_up;            
31
end Behavioral;

:
von 2loop|~2loop (Guest)


Rate this post
0 useful
not useful
Dmitry O. wrote:
> Hi guys.I need to write code for counter with input ENABLE and
> synchronous reset. The сounter must count up to 19. I have this code,
> but I don't understand where there is an error.

The keyword 'loop' marks the lines with errors.

PS:
Ваши английские предложения звучат больше немецкого, чем русского.

von Dmitry O. (Company: VGTU) (dimsan)


Rate this post
0 useful
not useful
Where is the error? Tell me please

von 2loop|~2loop (Guest)


Rate this post
1 useful
not useful
1) remove/replace everything that belongs to the "loop" construct ->
   you'll end up with a line:
1
 
2
    counter_up <= counter_up + 1;  --note the '1' instead of i
2) then simulate and think
 -how to detect the upper bound of the counting range
 -how to set the counter to the lower bound

-> then you will end up with a counter that works.

If you wanna improve your code further, think about an appropriate type 
for the counter_up value i.e. 'unsigned' instead of 'std_logic_vector'. 
And stay improving the code by using type 'integer' with 'range' 
constraint.

von Lothar M. (lkmiller) (Moderator)


Rate this post
1 useful
not useful
Dmitry O. wrote:
> Where is the error? Tell me please
First: pls wrapp your VHDL code in the VHDL tags
1
[vhdl]
2
  VHDL code
3
[/vhdl]

Second: do not use comments when they tend to be wrong or completely 
needless, such as this
1
    clk: in std_logic; -- clock input
2
    reset: in std_logic; -- reset input 
3
    ce: in std_logic; -- enable input
4
    counter: out std_logic_vector(7 downto 0) -- output 4-bit counter
Of course "clk: in std_logic" is the "clock input" andsoforth. And then 
(7 downto 0) is 8 bits, not 4 bits!

Third: forget what you know about loops from C or any other 
programming language. A loop in VHDL ist somthing COMPLETELY 
different.

Fourth: do not use the outdated std_logic_arith packages. Use the 
numeric_std instead!
See the conversion chart there 
http://www.lothar-miller.de/s9y/categories/16-Numeric_Std

Finally try this and think about it:
1
library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
use IEEE.NUMERIC_STD.ALL;
4
 
5
entity UP_COUNTER is
6
    Port ( clk: in std_logic;
7
           reset: in std_logic;
8
           ce: in std_logic;
9
           counter: out std_logic_vector(7 downto 0)
10
     );
11
end UP_COUNTER;
12
13
architecture Behavioral of UP_COUNTER is
14
    signal counter_up: integer range 0 to 19 := 0;
15
begin
16
-- up counter
17
process(clk)
18
begin
19
    if rising_edge(clk) then
20
        if reset = '1' then
21
           counter_up <= 0;
22
        elsif ce = '1' then
23
           if counter_up<19 then
24
              counter_up <= counter_up + i;
25
           else 
26
              counter_up <= 0;
27
           end if;
28
        end if;
29
    end if;
30
 end process;
31
    counter <= std_logic_vector(to_unsigned(counter_up,8));            
32
end Behavioral;

von Dmitry O. (Company: VGTU) (dimsan)


Rate this post
0 useful
not useful
Thanks for the explanation

Only registered users may reply in this topic. Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Facebook account
No account? Register here.