upcounter with enable signal for one clock cycle

Guest #4537852
1
______|``|________________________|``|_______       ---->enable input
2
________________________________________________
3
_______<0><1><2><3><4><5><5><5><5><0><1><2><3><4>  ---->counter output

hello leute,

i need your suggestions. i need to design a synthesizable up counter 
which starts counting if it finds enable signal toggling from one to 
zero..and  once count limit is reached, it has to wait for next enable 
pulse to start the next counting cycle again.
Moderator (Company: Titel) #4538008
Thats it:
1
:
2
signal olden : std_logic := '0';
3
signal cnt   : integer range 0 to 99 := 0;
4
:
5
:
6
process begin
7
   wait until rising_edge(clk);
8
   if (olden='0' and en='1') then -- "rising edge" of enable input
9
      cnt <= 0;
10
   elsif (cnt<99)                 -- saturate counter at 99
11
      cnt <= cnt+1;
12
   end if;
13
   olden <= en;                   -- remember "last" enable value
14
end process;

If the enable input is a async signal one flipflop stage (at least) 
must be added for syncing to the system clock:
1
:
2
signal ensr  : std_logic_vector (1 downto 0) := "00";
3
signal cnt   : integer range 0 to 99 := 0;
4
:
5
:
6
process begin
7
   wait until rising_edge(clk);
8
   if (ensr="01") then   -- "rising edge" of enable input
9
      cnt <= 0;
10
   elsif (cnt<99)        -- saturate counter at 99
11
      cnt <= cnt+1;
12
   end if;
13
   ensr <= ensr(0) & en; -- shift register for syncing and edge detection
14
end process;
Moderator (Company: Titel) #4538234
If the enable is synchronous and active for only one clock cycle, then 
it is extremely simple:
1
:
2
signal cnt   : integer range 0 to 99 := 0;
3
:
4
:
5
process begin
6
   wait until rising_edge(clk);
7
   if (en='1') then -- restart on enable signal
8
      cnt <= 0;
9
   elsif (cnt<99)                 -- saturate counter at 99
10
      cnt <= cnt+1;
11
   end if;
12
end process;
Moderator (Company: Titel) #4539020
Chris wrote:
> it doesn't (really) help people if you do their homework.
I could have written half a book, but I think, felix89 learns more by 
comparing those 3 solutions...

And additionally felix89 will ask himself (or he max be asked by his 
teacher) why the "wait until" is used here. And to find that answer will 
be a nice learning process too...

Reply

A question of your own? This thread is over 10 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

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.