Shop OBEX P1 Docs P2 Docs Learn Events
Propeller timing — Parallax Forums

Propeller timing

NewzedNewzed Posts: 2,503
edited 2007-10-15 23:25 in Propeller 1
I have the following little routine in my program:

repeat· until z == 162_000
····· z := z + 1

It takes precisely 10 seconds for z to reach 162_000.· This translates to 16_200 counts per second, which further translates to 61.7us per count.

Does this sound about right when the Prop clock is running at 80_000_000 cycles per second?

Sid

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Yesterday is history, tomorrow is a mystery, and today is a gift.

That is why they call it the present.

Don't have VGA?
Newzed@aol.com
·
«1

Comments

  • deSilvadeSilva Posts: 2,967
    edited 2007-10-11 18:39
    From my former measurements I should have expected it to take around 30 us only.. Are you sure it is 80 MHz??

    There are many options to speed up SPIN, e.g. try this loop:
    z:= 162_000
    repeat while z
       z--
    
  • RaymanRayman Posts: 14,162
    edited 2007-10-11 18:41
    Sounds about right to me...· I've heard the number 200 used as the relative speed of assembly to SPIN.· This would be about 2 or 3 assembly lines at 50 ns each.· I get 3*50ns*200=30us..
  • deSilvadeSilva Posts: 2,967
    edited 2007-10-11 18:50
    Rayman said...
    I've heard the number 200 used as the relative speed of assembly to SPIN.
    It's rather 80 but 200 smile.gif But there are special cases where 150 to 200 can be accomplished, generally when using DJNZ and not much in the loop. So this is one of that examples. It will relativate when there is more in the loop...
  • NewzedNewzed Posts: 2,503
    edited 2007-10-11 19:12
    deS, I tried your little routine.· It changed the cycle time from 10 seconds to 9 seconds so it is a bit faster.· I really should have included the entire routine:

    ··· z:= 0
    ··· repeat· until z == 162_000 '5 = 78_000· 10 = 162_000·· 30 = 500000
    ····· z := z + 1
    ···· if key.gotkey
    ······· cmds

    In addtion to augmenting z, the program has to check the status of key.got key.· If z = 162_000 then it returns to menu, where the·DS1307 has to be read, then displayed.· All this takes time so your estimate of 30us is probably pretty close.

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
    ·
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-10-11 21:10
    Yes checking the keyboard will take time.

    deSilva,

    I love the way you optimized a delay routine for speed [noparse]:)[/noparse]
  • deSilvadeSilva Posts: 2,967
    edited 2007-10-11 21:39
    It was meant to show optimization potentialities. It would have shown that the loop could perform nearly twice as fast, if Newzed had not spoiled it all by adding this call gotkey... :-(
  • NewzedNewzed Posts: 2,503
    edited 2007-10-11 21:51
    deS, could you translate this for me:

    z--

    And don't fuss at me about the key.gotkey loop.· Without it, it would have just set there and cycled endlessly.· Besides, at my age I am very sensitive.

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
    ·
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-10-11 22:06
    Graham Stabler said...
    Yes checking the keyboard will take time.

    deSilva,

    I love the way you optimized a delay routine for speed [noparse]:)[/noparse]
    It may be waiting but it is doing it in the quickest possible way [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.

    Post Edited (Paul Baker (Parallax)) : 10/11/2007 10:13:00 PM GMT
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-10-11 22:35
    Sid, z-- is the same as z := z - 1 so his loop counts down instead of up.

    The deSilva, these people have a bad habit of spoiling efficient code with functionality don't they [noparse]:)[/noparse]

    Graham
  • Ken PetersonKen Peterson Posts: 806
    edited 2007-10-12 02:45
    Try this:

    repeat 162_000

    How long does that take?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    The more I know, the more I know I don't know.· Is this what they call Wisdom?
  • deSilvadeSilva Posts: 2,967
    edited 2007-10-12 07:48
    @Sid: So may be I misunderstood the title of this thread...
    Your idea (and question) was obviosly how to wait best for 1 second!

    I remember you did it with WAITCNT formerly, which did not help acting quickly on keys...

    So IF this was the question, than you should do it by:
    z := CNT
    REPEAT WHILE CNT-z < CLKFREQ
      IF gotKey
          doSomething or QUIT
          z:=CNT
    



    Note that it has to be exactly CNT-z< CLKFREQ to care for the overflow!
  • NewzedNewzed Posts: 2,503
    edited 2007-10-12 12:45

    deS, you are correct. I want to establish delays of precisely 5 seconds, 10 seconds and 30 seconds. If I use CNT and clkfreq, I can make the timing loop exact. However, the overhead of reading readadc, readclk2, etc., increases the overall delay. That is why I wrote "if z == 162_000". I can adjust the value of z to give the precisely, or very close to it, the delay that I want . Another thing that will affect the delay is how close is the clock to rolling over to the next second when you read it. If it has just rolled over, then your delay will be accurate for quite a while. If the clock is just about to roll over to the next second, and your timing loop is a few ms off, the it will gain 1 second in a short time. Right now the program gains 1 second about every 105 cycles, but there is nothing I can do about that so I accept it. The accuracy of your delay is really dependent on what point in time you read the clock.

    And there is one other thing – in case I should ever want to increase the delay to 60 seconds for long-term data logging, it would be easy enough to do by setting the value of z. I could never do it with clkfreq because the max value you can get is clkfreq*53. Above that the value exceeds 32 bits.



    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
    ·
  • deSilvadeSilva Posts: 2,967
    edited 2007-10-12 19:02
    You are right when your loop will exceed 40 seconds; within any 40 seconds I gave you the correct formula which obsoletes your concerns.

    When having to work with >40 seconds there is little support, something I should have expected from the SPIN interpreter in the first place; e.g. it would not have considerably disturbed the performance checking internally CNT during each loop and updating a milisecond clock or such...

    Post Edited (deSilva) : 10/12/2007 7:08:31 PM GMT
  • Stan671Stan671 Posts: 103
    edited 2007-10-12 19:06
    Sid, check out Jeff Martin's CLOCK in the object Exchange: http://obex.parallax.com/objects/139/

    You can use his MarkSync and WaitSync routines as examples of how to set things up to wait for some future point in time no matter how long the stuff you are doing takes.

    When you run all of your overhead routines and then delay for 30 seconds, the delay is more than 30.· What you want to do it remember the time (cnt) before you start your overhead stuff and predict what the cnt will be in 30 seconds, and then do your overhead stuff.· When·that is done, wait for cnt to catch up to where you predicted the 30 seconds would be.· This way, your cycles are always 30 seconds, no matter how long your overhead took.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Stan Dobrowski
  • NewzedNewzed Posts: 2,503
    edited 2007-10-12 22:59
    Stan, that would give me very precise timing - I have used cnto before.· However, in this case, I need to be able to interrupt the count with a keypress.· I can not do this with waitcnt - I have to wait until the end of the waitcnt before the program responds to a keypress.

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
    ·
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-10-13 00:41
    You don't have to use waitcnt to use cnt and still avoid the problems with overheads.

    Graham
  • NewzedNewzed Posts: 2,503
    edited 2007-10-13 12:31
    Graham, I wrote:

    cnto := cnt
    (code,etc.)
    repeat until cnt == cnto + clkfreq*5
    ····· if key.gotkey
    ······· cmds

    The display never cycled.· Am I writing it wrong?

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
    ·
  • hippyhippy Posts: 1,981
    edited 2007-10-13 12:48
    Newzed said...
    Graham, I wrote:

    cnto := cnt
    (code,etc.)
    repeat until cnt == cnto + clkfreq*5
    if key.gotkey
    cmds

    The display never cycled. Am I writing it wrong?

    Yes. Firstly you are looking for one exact value of CNT and secondly you are not taking into account overflow and wrap-round.

    deSilva already addressed this above ...
    deSilva said...

    z := CNT
    REPEAT WHILE CNT-z < CLKFREQ
      IF gotKey
          doSomething or QUIT
          z:=CNT
    



    Note that it has to be exactly CNT-z< CLKFREQ to care for the overflow!
  • NewzedNewzed Posts: 2,503
    edited 2007-10-13 13:23
    hippy, I wrote:

    ··· z := cnt
    ··· repeat while cnt-z < clkfreq
    ····· if key.gotKey
    ····· cmds

    The program will respond to a keypress but it never cycles.

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
    ·
  • deSilvadeSilva Posts: 2,967
    edited 2007-10-13 14:39
    What do you mean by "cycle"? The program will leave the loop after 1 second, that was the idea!
    When you add the suggested
    z := cnt
    inside the IF this will be "re-triggered" after each key press....
    But without such action it will leave the loop within 1 sec.
    What did you have in mind for that situation?
  • NewzedNewzed Posts: 2,503
    edited 2007-10-13 15:50
    deS, by cycle·I mean redisplay the screen with new temp and time data.·· Finally got it working, sort of.· I wrote:

    ··· z := cnt
    ··· repeat while cnt-z< clkfreq*5
    ····· if key.gotKey
    ······· cmds

    The screen refreshes sometime every 5 seconds, sometimes every 6 seconds.· If I press a key to select an option, it takes varying times for the screen to redisplay.· One time it was about 3 seconds, one time it took 22 seconds.· With my

    ··· repeat· until z == 78_000
    ····· z := z + 1·············· '20 = 328_000
    ····· if key.gotkey
    ······· cmds
    ······· z := 31_000······· '2 second delay

    I will gain a second about every 10 minutes and the wait after a keypress is always consistent.

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
    ·
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-10-13 17:15
    Can you attach the program please and just remind me exactly what it is supposed to do in terms of time out and refresh.

    Graham
  • hippyhippy Posts: 1,981
    edited 2007-10-13 17:31
    Can you not do something like this ... ?

    timeout := CLKFREQ + CNT
    repeat
      RefreshDisplay
      repeat 10
        repeat while ( timeout - CNT ) > 0
          if got.keys
            cmds
        timeout := CLKFREQ + CNT
    
    


    That should refresh the display every 10 seconds and handle any keys as they occur.

    Post Edited (hippy) : 10/13/2007 5:37:21 PM GMT
  • deSilvadeSilva Posts: 2,967
    edited 2007-10-13 18:37
    Sid, it is a little bit unfair to compare different programs...
    In YOUR program you set
    z:= 32_000
    after command processing, in my snippet you have added nothing of that kind, so the counters will go astray when your menu activities take longer than 40 seconds e.g....
    So please add a
    QUIT
    in the IF part after your cmd processing, or a
    z:= CNT - 4*CLKFREQ
    or whetever you WANT..... This in fact is still a little bit unclear, what you WANT.

    To your first remark: It is not caused by this loop when your screen refresh takes sometimes 5 and sometimes 6 seconds. When it takes 6 seconds this has a reason from code you did not show us.
  • NewzedNewzed Posts: 2,503
    edited 2007-10-13 18:43
    hippy, I set cnto :- cnt at the beginning of my program, then modified your suggestion to read:

    timeout := clkfreq + cnto
    ··· repeat 5
    ····· repeat while ( timeout - cnt ) > 0
    ······· if key.gotkey
    ········· cmds
    ····· timeout := clkfreq + cnt

    It is cycling every 5 seconds right now.· I'm letting it run to see if it holds the 5 second delay.· I'll hold up checking a keypress for a while.

    Graham, I have attached my original code.· Beginning with Line 180, the program refreshes depending on what value I set z to - 5, 10, 20 or 30 seconds.

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
  • NewzedNewzed Posts: 2,503
    edited 2007-10-13 19:18
    deS, I have never used QUIT - wasn't sure of what it did.· Read the Prop·Manual, tried it out and it works just fine.· Thank you.

    hippy, the displayed time increased 1· second after about 380 refreshes and then continued at 5 second intervals.· That is the best I have been able to do so far.· I think it depends a lot on exactly at what time you start reading the DS1307.· Now I'll try it out at a 10-second delay.

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
    ·
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-10-13 20:27
    Try this as your loop:

    
    PUB menu 
        repeat
           cnto :=  cnt
           cnt1 :=  cnt
           repeat until cnt > cnt0 + timeout
              if key.gotkey
                 cmds
                 cnt0 := cnt
              if cnt > cnt1 + refresh
                 text.out(0)
                 text.out(13)
                 key.clearkeys
                 y := y + 1
                 text.dec(y)
                 readadc
                 readclock2         
                 menu1
                 cnt1 := cnt
           'insert timeout function here indented to first repeat
    
    



    If it does not work post the full code you are testing. You obviously need to give sensible values for timeout and refresh.
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-10-13 20:29
    So you make a note of cnt in cnt0 and cnt1

    If the count goes greater than cnt+timeout then it leaves the second repeat you can add a warning function or whatever.

    Otherwise within that loop as well as checking the keyboard it will also refresh the screen at an appropriate interval, the second variable cnt1 keeps the too timing processes seperate despite the fact they use the same timer.

    Graham
  • NewzedNewzed Posts: 2,503
    edited 2007-10-13 20:48
    hippy, I changed your routine for a 10-second display and let it run.· After 460 refreshes - that's 1 hour and 16 minutes - there was no change in the display interval - still precisely 10 seconds and I got tired of watching.· Excellent!· Thank you.

    deS and Graham, thank you very much for your assistance.· I think I'll go with hippy's suggestion - it is quite·simple to implement and to change the delay interval.· In fact I could go to a 1-minute delay if I wanted, or even a 5-minute delay, just by changing the repeat value.

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
    ·
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-10-13 20:53
    I don't quite see how Hippy's code handles the time out and the refresh because will the refresh not be more frequent than the time out?

    Graham
Sign In or Register to comment.