Shop OBEX P1 Docs P2 Docs Learn Events
Need help with deterministic timing using CNT in PASM — Parallax Forums

Need help with deterministic timing using CNT in PASM

Mike GMike G Posts: 2,702
edited 2011-03-29 01:00 in Propeller 1
I copied this right out of the Propeller manual.
rdlong  delay, #0               'Get clock frequency
shr     delay, #$2              'Divide by 4 
mov     time, cnt               'Get current time
add     time, delay             'Adjust by 1/4 second
waitcnt time, delay             'Wait for 1/4 second

Right below this code snippet, I have a small code block of PASM code. The whole thing (delay and all) loops X number of times. I know that the rest of the code executes well under ¼ sec. See attached.

Given that a code block execute in under a ¼ second, how do I wrap the code block so that the time it take to execute the code block + X wait time = ¼ second?

I’m not sure how to handle when CNT + delay overflows.

Comments

  • bennettdanbennettdan Posts: 614
    edited 2011-03-28 20:43
    Mike I could be way off here but you just want it to always loop in under 1/4 a second you could look at the Quick Reference and under the assembly section it tells you the clockes needed for each instruction so you might try adding up the clocks of each of the instructions then subtract that from your waitcnt delay.
    One thing I noticed is most of the instructions are around 4 clocks but some you are using are longer so just account for the longest of each one and add them up.
  • SSteveSSteve Posts: 808
    edited 2011-03-28 20:46
    Maybe I'm missing something, but can't you just put your code between the 'add' and 'waitcnt' statements?
  • Mike GMike G Posts: 2,702
    edited 2011-03-28 21:18
    @bennettdan, yeah I though about adding up the clock cycles but I'm reading and writing to HUB RAM which can be 7-22 clock cycles.
    Maybe I'm missing something, but can't you just put your code between the 'add' and 'waitcnt' statements?

    I beleive your right... should have read the waitcnt instruction a little closer.
  • bennettdanbennettdan Posts: 614
    edited 2011-03-29 00:19
    I don't see how adding the code between the add and waitcnt would change how many clocks it takes to execute the loop?
    How does it make the loop stay at just 1/4 a second or less?
  • Andrey DemenevAndrey Demenev Posts: 377
    edited 2011-03-29 01:00
    If you do this way
    loop
    rdlong  delay, #0               'Get clock frequency
    shr     delay, #$2              'Divide by 4 
    mov     time, cnt               'Get current time
    add     time, delay             'Adjust by 1/4 second
    waitcnt time, delay             'Wait for 1/4 second
    my_code
    ' some code here
    jmp     #loop
    

    then one loop cycle takes 1/4 second + time taken by "some code" + few cycles occupied by jmp and timing setup. The correct way is this:
    rdlong  delay, #0               'Get clock frequency
    shr     delay, #$2              'Divide by 4 
    mov     time, cnt               'Get current time
    add     time, delay             'Adjust by 1/4 second
    loop
    waitcnt time, delay             'Wait for 1/4 second
    my_code
    ' some code here
    jmp     #loop
    
Sign In or Register to comment.