EmbDev.net

Forum: FPGA, VHDL & Verilog upcounter with enable signal for one clock cycle


von felix89 (Guest)


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.

: Edited by Moderator
von Lothar M. (Company: Titel) (lkmiller) (Moderator)


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?

von felix89 (Guest)


Rate this post
useful
not useful
vhdl,starix,same clock

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


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;

: Edited by Moderator
von Lothar M. (Company: Titel) (lkmiller) (Moderator)


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;

: Edited by Moderator
von Chris (Guest)


Rate this post
useful
not useful
Even if this is just a finger exercise, mostly it doesn't (really) help 
people if you do their homework.

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


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...

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.