Shop OBEX P1 Docs P2 Docs Learn Events
Wait tick - Need help — Parallax Forums

Wait tick - Need help

computer guycomputer guy Posts: 1,113
edited 2008-10-13 11:16 in Propeller 1
I have the code for getting the prop to wait a second but is there a way to get it to wait a tick.

A tick is a 10, 000 of a millisecond.

The code for getting it to wait a millisecond is:
waitcnt(((clkfreq / 1_000 * Duration - 3932)) + cnt)




I thought:
waitcnt((((clkfreq / 1_000 * Duration - 3932)) + cnt) / 10_000)



would work but it waits for more like 10 seconds.

Can the prop handle waiting for such small amounts of time?


Thank you smile.gif

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Building Blocks To The Propeller Chip A web site designed to help people who are new to the propeller chip.

Guitar Hero controller using the prop (WIP) --> HERE

Post Edited (computer guy) : 10/13/2008 8:04:21 AM GMT

Comments

  • BaggersBaggers Posts: 3,019
    edited 2008-10-13 08:01
    computer guy
    waitcnt((((clkfreq / 1_000 * Duration - 3932)) + cnt) / 10_000)
    
    



    is adding the 3932 ( which I'm assuming you're using as an offset that spin takes between getting the cnt value for the calculation ) and the current cnt value, before / 10_000 try doing / 10_000 first

    waitcnt( ( ( (clkfreq / 1_000 * Duration) /10_000 ) - 3932 ) + cnt )
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

    ·
  • computer guycomputer guy Posts: 1,113
    edited 2008-10-13 08:06
    Thank you Baggers smile.gif

    However it still takes 10 seconds if not longer.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Building Blocks To The Propeller Chip A web site designed to help people who are new to the propeller chip.

    Guitar Hero controller using the prop (WIP) --> HERE
  • JasonDorieJasonDorie Posts: 1,930
    edited 2008-10-13 08:21
    At the·maximum (recommended) speed of the Prop, 80MHz, 1 sec = 80,000,000 clocks, so 1ms is 80,000 clocks.· If a 'tick' is 1/10,000 of a ms, that means it's 8 clocks.· You could accomplish it in PASM using:

    nop··· ' takes 4 clocks
    nop··· ' takes 4 more

    There's no way you'll get SPIN (interpreted) code to wait for such a short time, as there's a·significant overhead for the waitcnt SPIN instruction.· PASM runs ~50x faster than SPIN.

    Note that this is ONLY if you want to wait for ONE tick, or very few.· If you want to wait for a few hundred ticks, you're getting within the realm of possibility.· It won't be cycle accurate like PASM, but it might be good enough - the overhead is more or less fixed, so waiting for (overhead + 10) is possible, but (overhead - 10) isn't.

    In the example you've given, if 'Duration' isn't a constant, the code has to do an integer multiply of (1_000·*·Duration), then divide clkfrq by the result, both of which take some time.· The -3932 is probably compensating for the time passing while the calcs are done.· If Duration IS a constant, then the compiler could do all the math (except +cnt) at compile time, and that -3932 would probably cut down to -350 or so.

    I hope that all makes sense.· [noparse]:)[/noparse]

    Jason


    Post Edited (JasonDorie) : 10/13/2008 8:32:02 AM GMT
  • computer guycomputer guy Posts: 1,113
    edited 2008-10-13 08:27
    Is there a way to call a PASM function from spin.

    DAT
      waitTick
        nop
        nop
    
    



    Thank you smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Building Blocks To The Propeller Chip A web site designed to help people who are new to the propeller chip.

    Guitar Hero controller using the prop (WIP) --> HERE
  • JasonDorieJasonDorie Posts: 1,930
    edited 2008-10-13 08:31
    The act of calling that function from SPIN would delay you about 100x the length of the delay in the function itself, so even if you COULD (which you can't), you wouldn't want to in this case.

    Jason
  • JasonDorieJasonDorie Posts: 1,930
    edited 2008-10-13 08:34
    The shortest delay you're likely to get out of SPIN itself would be from using a unary operator, like post-increment, on a variable. There may be others that will take less time, but I think in general the interpreter has a ~200+ cycle overhead per instruction.

    Someone please correct me if I'm way off there.

    If this is what you're trying to do, it's probably in your best interests to learn PASM.

    Jason
  • computer guycomputer guy Posts: 1,113
    edited 2008-10-13 08:35
    I will explain what I need to do. That way you might be able to help a bit better.

    I have a variable "output_time". This has a value like 59334592.

    I need to increment a variable "temp" by 1 and then compare it to output_time.
    If they match then continue else keep incrementing and compare.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Building Blocks To The Propeller Chip A web site designed to help people who are new to the propeller chip.

    Guitar Hero controller using the prop (WIP) --> HERE
  • JasonDorieJasonDorie Posts: 1,930
    edited 2008-10-13 08:43
    That's exactly what waitcnt is for. If you write:

    waitcnt( cnt + output_time )

    ...it'll take the current cycle count, add your 'output_time' value, and then wait until the cycle count reaches that new number. Assuming that your 'output_time' value is in clock ticks that'll work just fine. If you're running at 10MHz, that would be exactly right as 1 tick would equal 1 clock. At 80MHz, 1 tick = 8 clocks, so you'd need to use:

    waitcnt( cnt + output_time*8 )

    ...and since *8 is just a shift left by 3, the compiler should optimize it for you and it will take very little time.

    Does that help?

    Jason

    Post Edited (JasonDorie) : 10/13/2008 8:50:48 AM GMT
  • computer guycomputer guy Posts: 1,113
    edited 2008-10-13 08:45
    Thank you Jason smile.gif

    I will see how I go with the information you have provided.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Building Blocks To The Propeller Chip A web site designed to help people who are new to the propeller chip.

    Guitar Hero controller using the prop (WIP) --> HERE
  • BaggersBaggers Posts: 3,019
    edited 2008-10-13 08:53
    don't forget if it's timing critical, you might need to find out the delay between calculating the value for the waitcnt spin and add that too.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

    ·
  • computer guycomputer guy Posts: 1,113
    edited 2008-10-13 09:13
    Just quickly can someone plz explain why this code isn't working.

          repeat while temp < new_time
            temp++
            term.dec(temp)
            term.out(13)
    
    



    Its not doing anything.

    I have that code within another repeat statement.
    If I change the code to
            temp++
            term.dec(temp)
            term.out(13)
    
    


    It works fine.

    Is there some rule about having a repeat statement within another?

    Thank you smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Building Blocks To The Propeller Chip A web site designed to help people who are new to the propeller chip.

    Guitar Hero controller using the prop (WIP) --> HERE

    Post Edited (computer guy) : 10/13/2008 9:55:35 AM GMT
  • BaggersBaggers Posts: 3,019
    edited 2008-10-13 09:36
    what is temp and new_time set to? before the repeat
    if they're both 0 ( uninitialised )·it won't do anything as 0 (temp) is not < 0 (new_time)
    or if·temp·is greater than or equal to·new_time

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

    ·
  • computer guycomputer guy Posts: 1,113
    edited 2008-10-13 09:52
    I thought that but even changing in to
          repeat
            temp++
            term.dec(temp)
            term.out(13)
    
    



    It doesn't work.

    Thank you smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Building Blocks To The Propeller Chip A web site designed to help people who are new to the propeller chip.

    Guitar Hero controller using the prop (WIP) --> HERE
  • BaggersBaggers Posts: 3,019
    edited 2008-10-13 10:23
    is it getting to that part? and not locking up before hand?
    can you post more of the code?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

    ·
  • computer guycomputer guy Posts: 1,113
    edited 2008-10-13 10:37
    Thank you for your help Baggers.

    I managed to get it working after setting the 2 variables properly.


    Thank you smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Building Blocks To The Propeller Chip A web site designed to help people who are new to the propeller chip.

    Guitar Hero controller using the prop (WIP) --> HERE
  • BaggersBaggers Posts: 3,019
    edited 2008-10-13 11:16
    rofl [noparse]:)[/noparse] no probs.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

    ·
Sign In or Register to comment.