Shop OBEX P1 Docs P2 Docs Learn Events
waitcnt for lighthouse keepers and possibly beginners — Parallax Forums

waitcnt for lighthouse keepers and possibly beginners

Graham StablerGraham Stabler Posts: 2,507
edited 2007-08-11 20:53 in Propeller 1
Just a simple explanation of how to use waitcnt for synchronous timing as per the manual page 323

The basic idea is that waitcnt can do more than insert a delay, it can do exactly what it says it can wait for a certain
count.
1343 x 1302 - 134K
wait.jpg 134.1K

Comments

  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-08-11 03:11
    Nice jpg. I smell a meme coming on.

    On the 53 second limit, does that depend upon clock/crystal speed? My guess is that it doesn't.

    And is my understanding correct that asking for more than 53 seconds isn't wrong per se -- you can tell the chip to do it, but the addition of 54seconds + cnt would just wrap around the counter? So, essentially a 54 second wait would end up waiting a second.

    Because it seems to me that since cnt can be any value between 0 and $ffff_ffff, the waitcnt specification could/would wrap around if needed when you add your time value. (simple unsigned 32 bit addition)
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-11 03:22
    The limit is $FFFFFFFF system clocks whatever that comes out to. For an 80MHz system clock, that's roughly 53 seconds.
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2007-08-11 05:07
    For simple light house sequencing, you could always wind down the PLL to obtain more than the 53 seconds if it is really required.
    A 40MHz system clock would give you roughly 106 seconds ; likewise 20MHz ... 212 seconds ... etc.

    I have been on vacation in Michigan and on my way home right now. Two days ago I was inside the Point Betsie Lighthouse in Michigan.
    It has an ON-OFF pattern for the FOG horn that repeats every 30 Seconds (when there is fog) synchronized with the rotating beacon light.
    By timing the rotating light with the sound of the fog horn, a ship can gage it's relative distance to the light house in a similar way you would
    count the seconds between a lightning flash, and the sound of the thunder to determine how far away a storm was.


    FOG horn pattern:

    On for 1.5 Seconds
    Off for 2 seconds
    On for 1.5 Seconds
    Off for 25 seconds


    www.nightbeacon.com/zlighthouses/lakemich/individuallights/Point_Betsie_Lighthouse.htm

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 8/11/2007 5:16:48 AM GMT
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-08-11 07:49
    Fred, its not wrong as long as you don't expect it to wait for 54 seconds. I'll change it and mention the @80Mhz.

    Beau, nice lighthouse the one pictured in the document is the Cape Blanco lighthouse in Oregon, just got it off google.

    Now the light house was just supposed to be a physical example rather than just a stream of pulses if it has realistic timing pulse the poor guy will never get a cup of tea and people will just suggest replacing him with a propeller.

    Graham
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-11 08:32
    Excellent idea, Graham! Can of course be improved smile.gif

    (a) As it is obviously done for the absolute beginner, you can explain how to derive ms15 and ms60 from CLKFREQ, so being independent of the actual clock rate.
    (b) There is a difference between example (A) and (B), the student should perhaps have to spot himself... In (B) the "dark time" will be about 5 mys longer than in (A)
    (c) Note that you do not have an exakt 100 ms cycle, as you refresh TIME in each loop, this works against your very intention for this example. I should not do that!
    (d) Always consider "defensive programming". Add a check after the tea break that you did not overspent it! It might not be a desaster to be a little bit late once, but being blocked for 53 seconds certainly is smile.gif
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-08-11 09:32
    a) It is only supposed to be explaining one single element of waitcnt
    b) Where is the y in micro?
    c) Good point, I didn't want to over complicate it, the light house keeper does not have such issues but perhaps I should add some more code.
    d) In a lot of micro controller apps that just isn't needed if for example they are just a fixed set of simple commands with no communications with the outside world. Again I could *** a note about this in which case it gets to the point where it might as well be cold app note that the beginner is scared to read.

    Really I wanted to make a cartoon out of it like these:

    http://www.howtoons.com/toons/

    But I'm not that much of an artist and frankly don't have the time. I'm just aware that in my head I have visual models floating around and they help me.

    Graham
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-11 09:58
    Thanks for your kind answers; but I stil think it is an good idea to ALSO show the beginner how he can compute ms15 etc.


    I explain "mys" for some time now. "My" is the acknowledged transcription of the 12. Greek letter; due to some peculiarities of their accent, Americans transcribe it "mu", where I have difficulties to get accustomed to, sorry.
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-08-11 10:25
    Perhaps I will ditch this version and go for a more complete one or perhaps I'll just delete it and not bother.

    Sorry I missed your explanation though I know I asked before. As far as I am aware the letter is pronounced "mu (m-you)" (by English not just Americans) but it is just a symbol used to represent micro (m is taken my milli). The easiest way to represent it is with u because u looks quite a bit like the letter mu.

    I don't know who has acknowledged mys as the transcription for the letter but I have never seen it used at any time other than by you and it leads to confusion because someone might think you mean millyseconds not mycroseconds if you see what I mean. I doubt you will change your ways but I thought you might like to know.

    Graham
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-11 11:01
    All right, I see I cannot fight windmills smile.gif

    There is no doubt and discussion that the 12. Greek letter is pronounced "My - Ypsilon", in modern Greek "mee" in ancient Greek with a vowel that does not exist in the English language. If you wondered what I mean by "Ypsilon" - don't wonder anymore: I just found out that it is transcribed "upsilon" in English. So you have created yourself a consistent system!

    I shall try to write "mus" from now on smile.gif
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-08-11 11:32
    Recommended reading: Jeff Martin's nice Clock.spin in your root directory.
  • MightorMightor Posts: 338
    edited 2007-08-11 11:37
    Graham,

    I like this explanation of what waitcnt can do. I never thought to use it this way. Thanks!

    Gr,
    Mightor

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    | Any sufficiently advanced technology is indistinguishable from magic.
  • rjo_rjo_ Posts: 1,825
    edited 2007-08-11 12:22
    deSilva,

    La explicaci
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-11 12:34
    Graham Stabler said...
    or perhaps I'll just delete it and not bother.
    Don't do that Graham - I like your setting very much smile.gif I think something along
    PUB main | ms15, ms45, deadline
      MS15 = CLKFREQ/1000 * 15
      MS45 = CLKFREQ/1000 * 45
    
      deadline := CNT
      REPEAT
        deadline += ms15
        WAITCNT(deadline)
        OUTA[noparse][[/noparse]0] := 1
        ' tea break
        deadline += ms45
        IF CNT - deadline > 0
            'PANIC
        ELSE    
            WAITCNT(deadline)
        OUTA[noparse][[/noparse]0] := 0
    


    will be a very instructive example!

    Post Edited (deSilva) : 8/11/2007 12:40:26 PM GMT
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-08-11 13:35
    deSilva,

    ""Epsilon" (ἒ ψιλόν, "simple e") was coined to distinguish the letter from αι, which by the medieval period was pronounced the same way."

    Those silly medieval types don't know what they started [noparse]:)[/noparse]

    But aren't we talking science not ancient linguistics, it makes no difference how the Greeks ancient or otherwise pronounced a word, it's the symbol we wish to represent that or the English way of saying it on an English speaking forum (assuming Rich is not posting).

    I like the example, should we modify the values of ms15 and ms45 to account for the time taken to turn off the lighthouse etc according to your c) ?

    Graham
  • BTXBTX Posts: 674
    edited 2007-08-11 14:16
    Sorry Graham...it is a bit out of topic, but.

    De haber sabido que, "rjo_ y deSilva" hablan Espa
  • LawsonLawson Posts: 870
    edited 2007-08-11 18:16
    Yea I've used this technique before to get rock solid timing with spin. Nither of the code snippets in the Light house pic, fully hide the delays from spin execution. deSilva's code does manage to fully hide the spin delays, but the "panic" check makes it hard to read, and his output logic is inverted.


    Pub House_light | time, ms15, ms45
      ms15 := clkfreq/1000 * 15
      ms45 := clkfreq/1000 * 45
    
      time := cnt
      Repeat
        outa[noparse][[/noparse]0] := 1
        'take a tea break
        waitcnt(time += ms15)
        outa[noparse][[/noparse]0] := 0
        'read the paper
        waitcnt(time += ms45)
    



    The key thing here is that "time" is only loaded from CNT once. All future waits are essentially referenced to this one mark in time, thus eliminating most error stack up. I've made a similar modification to the blinker.spin demo from the manual. The last two LEDs blinking stayed in exactly the same phase after running for 12 hours. I am assuming here that the "tea break" and "read the paper" don't ever take too long. Also, due to the specifics of the code the light will be on slightly less than 15 milliseconds in every 60 millisecond cycle. Also, rounding errors in my math could cause a slight drift in the timing.

    Graham Stabler I think this is a cool thing you've made. CNT is one of my favorite features of the Prop. More micros should have a "CNT" register!

    edit: um... crud the forum is messing with my code! [noparse][[/noparse] 1 ] is a font size tag! so it won't show up even in a code block. huh?! (i've switched to [noparse][[/noparse]0] and that works)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Lunch cures all problems! have you had lunch?

    Post Edited (Lawson) : 8/11/2007 6:27:33 PM GMT
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-11 20:53
    Lawson said...
    but the "panic" check makes it hard to read, and his output logic is inverted.
    There are good reasons for exactly the code I chosesmile.gif
Sign In or Register to comment.