Thread: How to use a for loop for repetitive timed actions

1. Altera Pupil
Join Date
Jul 2015
Posts
5
Rep Power
1

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;

2. Moderator **Forum Master**
Join Date
Oct 2008
Posts
5,567
Rep Power
1

Re: How to use a for loop for repetitive timed actions

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

3. Altera Pupil
Join Date
Jul 2015
Posts
5
Rep Power
1

Re: How to use a for loop for repetitive timed actions

Originally Posted by Tricky
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

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.

4. Moderator **Forum Master**
Join Date
Oct 2008
Posts
5,567
Rep Power
1

Re: How to use a for loop for repetitive timed actions

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.

5. Altera Pupil
Join Date
Jul 2015
Posts
5
Rep Power
1

Re: How to use a for loop for repetitive timed actions

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.

6. Moderator **Forum Master**
Join Date
Oct 2008
Posts
5,567
Rep Power
1

Re: How to use a for loop for repetitive timed actions

Originally Posted by mafstoffer
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.
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.

7. Altera Guru
Join Date
Jun 2013
Location
The Netherlands
Posts
443
Rep Power
1

Re: How to use a for loop for repetitive timed actions

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

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•