Search code examples
vhdlmodelsim

VHDL process for blinking led


I would like to have a process which make a led blinking 3 times (each time the light should be on for one second) and the interval between to light-on should be of 1 second.

The fastest and easiest way should be the following:

process(CLK)
begin
....
led <= '0';
led <= '1' after  1 sec,
 '0' after 1 sec,
 '1' after 1 sec,
 '0' after 1 sec,
 '1' after 1 sec,
 '0' after 1 sec;
....
end process;

By compiling it there is no Error... but the clock has its own working frequency (e.g. 1ns). By tring this code with Modelsim it will give the following error:

run
# Cannot continue because of fatal error.
# HDL call sequence:
# Stopped at C:/User...rm.vhd 50 Process line__41
# 

The row is exactly on the PROCESS(CLK) row.

I think the problem is because the CLK has bigger frequency so in this way a next process starts before the previous finished (it makes something really nasty...).

If what I wrote is correct, a solution can be playing with the clock for having the requested period but I would like to find a faster and more light solution.


Solution

  • As you wrote already, clk is faster than 1 sec. In one clock cycle (1ns) you try to "wait" 5-6 seconds depending on number of "... after 1 sec" lines. You should not make this way. If you want really so speedy clock then make some counters wide enough for one second. In process(clk) you increment this counter and compare counter value with pre-defined value for one second, if the counter value reaches to this value, you reset the counter and toggle the LED.

    If you want some other "phases" like LED shall blink 3-second long, and then it shall not blink further 3-second long, then you need more than one counter. Each phases should have a counter.