Help with time delay
mynet43
Posts: 644
I have a code loop in spin that interfaces with an assembly language routine.
The loop is designed to call an adc routine to supply adc values to the assembly language routine thru shared variables.
The loop needs to run continuously until x_step and y_step are both zero (two stepper motors running at 30KHz micro-steps).
What I'm trying to do is call a display routine every 0.1 seconds while it's looping, to show current values supplies by the assembly routine.
Here's my question: The variable disp_cnt may wrap around when it overflows the 32 bit register. It seems like this would make it so the "if cnt > disp_cnt" statement wouldn't work right. What is the right way to handle this, so the if statement always works?
Thank you for your help.
Jim
The loop is designed to call an adc routine to supply adc values to the assembly language routine thru shared variables.
The loop needs to run continuously until x_step and y_step are both zero (two stepper motors running at 30KHz micro-steps).
What I'm trying to do is call a display routine every 0.1 seconds while it's looping, to show current values supplies by the assembly routine.
Here's my question: The variable disp_cnt may wrap around when it overflows the 32 bit register. It seems like this would make it so the "if cnt > disp_cnt" statement wouldn't work right. What is the right way to handle this, so the if statement always works?
Thank you for your help.
Jim
disp_cnt := cnt + clkfreq/10 ' add 0.1 second to cnt
repeat while x_steps or y_steps ' wait for assy routines to finish
get_lim_sw_adc_values ' call the adc routine to get switch values for assy routine
if cnt > disp_cnt ' display now and then
disp_cnt := cnt + clkfreq/10 ' reset timer, add 0.1 second to cnt
display_table_loc(30,220,200,240) ' display table location in microns: +/- uuu.u
if (x_mot_err or y_mot_err) ' set by assy routine
x_t_loc := x_mot_err ' table location for X/Y calibration at lim switch
y_t_loc := y_mot_err
quit ' break if we hit a limit switch

Comments
startTime := CNT intervalTime := CLKFREQ / 10 REPEAT if CNT - startTime > intervalTime ' Has time elapsed yet? startTime += intervalTime ' Move the time forward ' This will execute every time 1/10 of a second elapses and ' each interval of 1/10 will exactly follow the previous interval.Thank you for the quick reply.
This has always bugged me, but I never took the time to figure it out.
Jim