Intel® Quartus® Prime Software
Intel® Quartus® Prime Design Software, Design Entry, Synthesis, Simulation, Verification, Timing Analysis, System Design (Platform Designer, formerly Qsys)
16595 Discussions

How to use a for loop for repetitive timed actions

Altera_Forum
Honored Contributor II
1,306 Views

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;
0 Kudos
6 Replies
Altera_Forum
Honored Contributor II
448 Views

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

0 Kudos
Altera_Forum
Honored Contributor II
448 Views

 

--- 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.
0 Kudos
Altera_Forum
Honored Contributor II
448 Views

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.

0 Kudos
Altera_Forum
Honored Contributor II
448 Views

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.

0 Kudos
Altera_Forum
Honored Contributor II
448 Views

 

--- 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.
0 Kudos
Altera_Forum
Honored Contributor II
448 Views

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

0 Kudos
Reply