Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
9 years ago

How to use a for loop for repetitive timed actions

I am trying to figure out how to use a for loop for some repetitive timed actions. A snippet of the code I am using right now is shown below, although only showing 2 repeats. I haven't been able to come up with something that seems like it would work.

Marie Stoffer

CASE ten_sec_cnt IS

WHEN "000" =>

IF ((rollovr_cnt = X"0") and (duration = X"00")) THEN

next_red <= '0'; -- turn red LED on

next_av_done <= avdone;

next_state <= state;

ELSIF ((rollovr_cnt = X"1") and (duration = X"F2")) THEN -- time = 5 sec

next_red <= '1'; -- turn red LED off

next_av_done <= avdone;

next_state <= state;

ELSIF ((rollovr_cnt = X"2") and (duration = X"B9")) THEN -- time = 7 sec, double-flash, start of 10 seconds

next_red <= '0'; -- turn red LED on

next_av_done <= avdone;

next_state <= state;

ELSIF ((rollovr_cnt = X"2") and (duration = X"D2")) THEN -- time = 7250 ms

next_red <= '1'; -- turn red LED off

next_av_done <= avdone;

next_state <= state;

ELSIF ((rollovr_cnt = X"3") and (duration = X"04")) THEN -- time = 7750 ms

next_red <= '0'; -- turn red LED on

next_av_done <= avdone;

next_state <= state;

ELSIF ((rollovr_cnt = X"3") and (duration = X"1C")) THEN -- time = 8 sec

next_red <= '1'; -- turn red LED off

next_av_done <= avdone;

next_state <= state;

ELSIF ((rollovr_cnt = X"3") and (duration = X"80")) THEN -- time = 9 sec

next_red <= '0'; -- turn red LED on

next_av_done <= avdone;

next_state <= state;

ELSIF ((rollovr_cnt = X"3") and (duration = X"99")) THEN -- time = 9250 ms

next_red <= '1'; -- turn red LED off

next_av_done <= avdone;

next_state <= state;

ELSIF ((rollovr_cnt = X"3") and (duration = X"CA")) THEN -- time = 9750 ms

next_red <= '0'; -- turn red LED on

next_av_done <= avdone;

next_state <= state;

ELSE

next_red <= red;

next_av_done <= avdone;

next_state <= state;

END IF;

WHEN "001" =>

IF ((rollovr_cnt = X"0") and (duration = X"00")) THEN -- time = 10 sec

next_red <= '1'; -- turn red LED off

next_av_done <= avdone;

next_state <= state;

END IF;

WHEN OTHERS => null;

END CASE;

6 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    For loops unroll into parallel or sequential logic depending on their usage. I suggest going back to your circuit diagram and ensuring that it meets your design specification. Modify your diagram before writing any code

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    For loops unroll into parallel or sequential logic depending on their usage. I suggest going back to your circuit diagram and ensuring that it meets your design specification. Modify your diagram before writing any code

    --- Quote End ---

    This is definitely sequential logic. It has to be since the LED gets turned on for 250ms and then off for 500ms then on again for 250ms and then off for 1 second and then this same sequence repeats 4 more times, then repeats every 10 seconds for 15 minutes. Where I am having the problem is figuring out how to write this as a for loop and how to use a timer in the for loop. I have pages of code to do much of this sequence without using a for loop but it seems extremely inefficient.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    What you describe sounds like a software programming concept. This is not programming, this is HDL (hardware description). In HDL, for loops are just a way to repeatedly describe blocks of similar logic. They do not cover passages of time.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    The logic turns an LED on and off and increments a counter (or several). The time is measured by the value of the counter which is clocked at a specific frequency.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    The logic turns an LED on and off and increments a counter (or several). The time is measured by the value of the counter which is clocked at a specific frequency.

    --- Quote End ---

    Yes. And at no point is a for loop useful for you. A for loop has no memory between clock cycles.

    Think about the logic circuit, not the software code.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    What might help is drawing the circuit. When you have a clear picture of how the multiplexers, comparators, registers etc. are connected you can write code to represent the same circuit. (You can use the RTL viewer to check the cuircuit resulting from the code.)