0
\$\begingroup\$

How to design a counter to increment on the clock rising edge?

For example, I am trying to create a counter that increments on the rising edge of the clock if an input is a logic level 1. If the input is 0 on the clock rising edge, the counter is not incremented. When I write this logic, the code doesn't synthesise.

It seems like a counter enable signal I need after the clock edge detection but it won't synthesise.

EDIT

The error I am getting is:

[Synth 8-27] else clause after check for clock not supported 
[Synth 8-285] failed synthesizing module 'Encoder_Counter'

Code:

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
use work.Data_Sizes_Package.ALL;
entity Encoder_Counter is
 Port (Clock : in std_logic;
 Count_Input_A, Count_Input_B : in std_logic; 
 Reset_Counter : in std_logic;
 Counter_Value : out std_logic_vector(Data_width-1 downto 0)
 );
end Encoder_Counter;
 architecture Behavioral of Encoder_Counter is
signal Counter_Value_Temp : std_logic_vector(Data_width-1 downto 0) := (others => '0');
begin
Counter_Value <= Counter_Value_Temp;
 Process (Count_Input_A, Count_Input_B, Reset_Counter)
 
 begin
 
 if(rising_edge(Reset_Counter)) then --rising or falling edge?????
 Counter_Value_Temp <= (others => '0'); 
 
 elsif(falling_edge(Clock)) then
 
 if(Count_Input_A = '1' or Count_Input_B = '1') then
 Counter_Value_Temp <= Counter_Value_Temp + 1; 
 
 end if;
 
 end if;
 end Process;
end Behavioral;
jsotola
2,9343 gold badges15 silver badges22 bronze badges
asked Mar 17, 2021 at 20:03
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Please post your code and any error messages you are getting when you try to synthesize it. \$\endgroup\$ Commented Mar 17, 2021 at 20:35

1 Answer 1

1
\$\begingroup\$

For anyone interested in this post in the future, the VHDL code in the question is very poor. This answer provides a better VHDL approach.

To summarise, the code should infer a counter with active high synchronous reset, that increments on the rising edge if either of the two inputs are high (OR gate logic).

The unsigned package is removed as it should not be used in VHDL - use numeric_std instead.

To properly code a synchronous design (register), the sensitivity list only requires the clock to be included (for active high synchronous reset as in this case).

The counter is now properly interpreted as unsigned and works as expected.

Code

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
use work.Data_Sizes_Package.ALL;
entity Encoder_Counter is
 Port (Clock : in std_logic;
 Count_Input_A, Count_Input_B : in std_logic; 
 Reset_Counter : in std_logic;
 Counter_Value : out std_logic_vector(Data_width-1 downto 0)
 );
end Encoder_Counter;
 architecture Behavioral of Encoder_Counter is
signal Counter_Value_Temp : std_logic_vector(Data_width-1 downto 0) := (others => '0');
begin
Process (Clock)
 
begin
 
if (rising_edge(Clock)) then 
 if (Reset_Counter = '1') then
 Counter_Value_Temp <= (others => '0'); 
 
 elsif (Count_Input_A = '1' or Count_Input_B = '1') then
 Counter_Value_Temp <= std_logic_vector(unsigned(Counter_Value_Temp) + 1); 
 
 end if;
 
end if;
end Process;
 
Counter_Value <= Counter_Value_Temp;
end Behavioral;
answered Mar 17, 2021 at 20:45
\$\endgroup\$
8
  • 1
    \$\begingroup\$ You can put the corrected code in the answer. \$\endgroup\$ Commented Mar 18, 2021 at 13:05
  • \$\begingroup\$ @MituRaj Will do when I get home. \$\endgroup\$ Commented Mar 18, 2021 at 13:07
  • \$\begingroup\$ You never did, would you mind to fulfill your promise? \$\endgroup\$ Commented Dec 17, 2024 at 8:11
  • \$\begingroup\$ @thebusybee This is very bad VHDL code and while I may have got the design to synthesize back in 2021, there are still many code flaws present which were never fixed (project ended). \$\endgroup\$ Commented Dec 22, 2024 at 17:42
  • 1
    \$\begingroup\$ Thank you, it makes the world some bits better (pun intended). Merry Xmas! \$\endgroup\$ Commented Dec 24, 2024 at 12:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.