Shop OBEX P1 Docs P2 Docs Learn Events
How to get a fixed repeat loop time. — Parallax Forums

How to get a fixed repeat loop time.

BTXBTX Posts: 674
edited 2007-06-04 22:23 in Propeller 1
Hi all.
I've to get from a repeat loop the same time in each iteration.
But the time that·take·some job into the repeat loop is variable, and less than the time that I need in each iteration, so this is correct for me.

Some ideas ...how I could do this ?

I tried using cnt register but without luck in that case.

This don't work me correctly:
It seems that the time of each iteration that I get, is not the same everytime.
repeat
  x:= cnt + constant_value  (number of cicles in constant_value is greater tha the number of cicles of the 'job') 
  .
  . do some variable job here.
  .
  repeat while cnt =< x

Thanks in advance !!

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Regards.

Alberto.

Comments

  • edited 2007-06-03 18:37
    There's an example on page 20 of the I/O and Timing PE Kit lab.

    PE Kit Labs: http://forums.parallax.com/showthread.php?p=617192
    I/O and Timing Lab: http://www.parallax.com/dl/docs/prod/prop/PE-IOTimingBasics-v1.0.zip

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Andy Lindsay

    Education Department
    Parallax, Inc.
  • BTXBTX Posts: 674
    edited 2007-06-03 23:33
    Thanks so much Andy
    I'll read that.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Regards.

    Alberto.
  • BTXBTX Posts: 674
    edited 2007-06-04 12:35
    Hi all.
    I did this:
    dT := clkfreq/15
    T  := cnt
    repeat   
       T += dT
       waitcnt(T)
       waitpeq(|<22,|<22,0)
       waitpeq(0,|<20,0)
       outa[noparse][[/noparse]21]~~              
       outa[noparse][[/noparse]21]~         
    

    And this:
    dT := clkfreq/15
    T  := cnt
    repeat   
       waitpeq(|<22,|<22,0)
       waitpeq(0,|<20,0)
       T += dT
       waitcnt(T)
       outa[noparse][[/noparse]21]~~              '  Display muestra data 
       outa[noparse][[/noparse]21]~         
    


    And this:
    dT := clkfreq/15
    T  := cnt
    repeat   
       waitpeq(|<22,|<22,0)
       waitpeq(0,|<20,0)
       outa[noparse][[/noparse]21]~~              '  Display muestra data 
       outa[noparse][[/noparse]21]~         
    
       T += dT
       waitcnt(T)
    

    And it never works, the time involved in the pieces of my code (two waitpeq plus two outa) is less than 1/15 sec.
    If I have so problem in this part of my code, a solution for me could be do some similar but into another assembly code.
    How to do that ?? in asm. I could try....

    Thanks in advance.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Regards.

    Alberto.
  • Martin HebelMartin Hebel Posts: 1,239
    edited 2007-06-04 13:05
    Here's the simpliest way for a wait time:

    waitcnt(clkfreq/1000 * t + cnt)· ' t = time in milliseconds

    -Martin



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    StampPlot - GUI and Plotting, and XBee Wireless Adapters
    Southern Illinois University Carbondale, Electronic Systems Technologies
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-06-04 13:52
    dT := clkfreq/15
    T  := cnt
    T += dT
    repeat   
       waitpeq(|<22,|<22,0)
       waitpeq(0,|<20,0)
       outa[noparse][[/noparse]21]~~              '  Display muestra data
       outa[noparse][[/noparse]21]~        
       waitcnt(T)
       T += dT
    
    



    This should work.

    You need to remember that cnt is incrementing all the time in the background so you work out your target time, do stuff and then wait for the target time to come arround. Your examples do something and then add a fixed delay or wait a fixed time and do something which is not the same.

    Graham

    p.s. this assumes dT is a long enough time for the waitpeq's to occur.
  • BTXBTX Posts: 674
    edited 2007-06-04 15:59
    Martin.

    Yes, I know this way... but in my case "t" is variable and unknown in each iteration inside the repeat loop.

    Graham.

    Yes , this should work... I tried with a different code into the repeat loop and all works fine, my problem is that those two 'waitpeq' sincronyze two differents COGs and one in a different Pchip.
    Seems to be a problem with that when I add the waitcnt(T), if instead that, I add a waitcnt(clkfreq/n + cnt) it works fine ...but each iteration has a different execute time, dT and T variables are longs.
    Perhaps I'm not explaining well in English what I want to say.

    I just only copy the idea from "PE-IOTimingBasics-v1.0.zip" that Andy Lindsay suggest to me.

    I need to try into a differents piece of my code, some that is in asm, the question is ...How could I do the same as Andy example but in asm ?

    I attach here the page 20 of the pdf, where shows, when a code is correct and incorrect, see the example of "timekeeping aplications", ·take a look at that, is simple but awesome.
    That's what I need·to insert in my·asm code.

    Thanks all !!!!


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Regards.

    Alberto.
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-06-04 17:07
    Alberto,

    Did you notice that I had modified your code?

    My version will work even when the waitpeq take different times.

    You can do the same thing in assembly but using the assembly version of waitcnt.

              mov      time,cnt
              add       time,delay
    :loop    
               do something
               waitcnt, time,delay         ' Note that this will post increment time with delay
               jmp    #:loop
    
    





    Graham
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-06-04 17:43
    Actually the change I made makes no difference.

    If what you are doing is not working it is because the waitpeq do not happen in time, this means you may need to make dT larger.

    Graham
  • BTXBTX Posts: 674
    edited 2007-06-04 18:25
    Thanks very much Graham.
    I'll be testing your code later, to check if it works for me.

    What I'm doing, is to send data from the master Pchip to the 16 slaves that I have.....
    I'm sending a fixed quantity of data to each slave, and then they send the data to the display.
    Like I send many frames to the display, and I've check that all the 'animation' take about a rate of 22fps and it is correct seeing it, I want to delay all the data, to get a frame rate of only 15 fps. Due the video that I'm playing was done at 15fps and if I leave the controller in 22fps I'm getting an image faster than the real video is. (Hope do you understand what I mean and doing....)

    Due tehre are so many COGS and Pchis everywhere...it is a bit difficult for me, to get the correct place where to put the delay.
    I'll try with your suggests and then I will post again with the results.
    Thanks again !!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Regards.

    Alberto.
  • BTXBTX Posts: 674
    edited 2007-06-04 21:51
    Graham.
    The problem was solved.

    The spin code wont work (still with your changes...but is not a code problem, it is some problem with my slaves that get hung with the delay introduced).
    I solved that using the "delay" assembly code, into the Master code.

    Thanks !!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Regards.

    Alberto.
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-06-04 22:23
    Glad to be of help, that basic method is VERY useful for all kinds of things such as motion control and PWM!

    I used the counters and a fixed time length loop to get some really nice stepper drive stuff working, you just increment the counter frequency every loop for a certain number of loops, then keep it constant for a certain number of loops and then decrement it for a certain number of loops, the result is lovely trapizoidal pulse rates up to silly frequencies. You just need to be sure to scale the maximum frequency etc to make sure the "area under the graph" is the distance you wish to travel.

    Oh, I got distracted.

    Graham
Sign In or Register to comment.