upcounter with enable signal for one clock cycle

Guest #4537852
Rate this post
• ▲ useful
▼ not useful
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) #4537930
Rate this post
• ▲ useful
▼ not useful
felix89 wrote:
> 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..
What language?
What target?
Whats the counting clock?
Whats the system clock?


BTW: do you know the German forum?
https://www.mikrocontroller.net/forum/fpga-vhdl-cpld
Maybe you want to switch over ther?
Moderator (Company: Titel) #4538008
Rate this post
• ▲ useful
▼ not useful
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
Rate this post
• ▲ useful
▼ not useful
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
Rate this post
• ▲ useful
▼ not useful
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

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.