Shop OBEX P1 Docs P2 Docs Learn Events
waitcnt problem — Parallax Forums

waitcnt problem

MinimumWageMinimumWage Posts: 72
edited 2008-01-27 19:25 in Propeller 1
Hi all,

I'm hoping someone can help me with this. I am trying to run the program below but am getting unusual results with the waitcnt statement in the BeatCount subroutine. I stripped it down from another program for troubleshooting - all it does is pass a shared value between cogs. No matter what value I use in that waitcnt statement the cog gets stuck for almost a minute before updating the beatavg value. I'm sure that indicates that the cog is waiting for the system counter to roll over, but I don't understand why. I've never had a problem like this with a simple waitcnt statement and feel like I must be missing something obvious...

CON

   _clkmode = xtal1 + pll8x     'set for SPINSTAMP!!
   _xinfreq = 10_000_000                                   

   b_out1 = 1

VAR

  long stack1[noparse][[/noparse]50]
  long stack2[noparse][[/noparse]50]
  long beatavg

PUB Main

  cognew(BeatCount, @stack1)
  cognew(FlashLED, @stack2)

PUB BeatCount

  waitcnt(clkfreq + cnt)
  beatavg := clkfreq/10

PUB FlashLED

  dira[noparse][[/noparse]b_out1]~~
  
  repeat while true
    outa[noparse][[/noparse]b_out1] := !outa[noparse][[/noparse]b_out1]
    waitcnt(beatavg + cnt)




Basically, when I run this code as-is the LED flash frequency doesn't get set to 1/10 sec for almost a minute. If I comment out the first waitcnt statement, it updates instantly.

Post Edited (MinimumWage) : 1/26/2008 8:23:05 AM GMT

Comments

  • Graham StablerGraham Stabler Posts: 2,507
    edited 2008-01-26 10:13
    You run beatcount first and it waits for clkfreq+cnt and that is a minute, you will be waiting for 80 million clocks and it's running at 80Mhz. Until that has completed beatavg will be zero.

    Graham
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-26 10:39
    As what Graham says is terribly obvious, the question is what you had in mind with the BeatCount COG in the first place? It makes no sense at all as it stands..
  • AribaAriba Posts: 2,685
    edited 2008-01-26 11:56
    No, no, 80 Million clocks at 80 MHz gives 1 second not 1 minute. So on that the code is correct.
    IMHO the problem is that the BeatCount Cog and the FlashLED Cog are startet immediate consecutively without waiting for the BeatCount has finished. So the beatavg value is zero for the first waitcnt in the loop, and thats why you have to wait 54 seconds.

    Andy
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2008-01-26 12:15
    Doh!!

    Quite right! It is essentially waiting for cnt which will have passed by the time it starts waiting.

    Graham
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-26 14:13
    Ah, we just wanted to hear something from Andy again....
  • MinimumWageMinimumWage Posts: 72
    edited 2008-01-26 17:16
    Andy and all,

    Thanks for the replies. I had thought something along those lines might be the case, so I tried running the code with beatavg explicitly init'ed to 0, and then changing the loop to "repeat while beatavg <> 0". With that change the LED never flashes (even after waiting a minute). I would expect it to update the variable after a second and start flashing, right? Am I still missing something?
  • MinimumWageMinimumWage Posts: 72
    edited 2008-01-26 17:21
    Ah wait - I just had some more coffee and I think my brain is working better now. Any kind of 0 + cnt statement is going to wait until the counter loops all the way around. I think knowing that I can work around it somehow. Thanks again for the help!
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2008-01-26 18:00
    You could have a repeat while to wait for it to be initialized.

    Graham
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-26 18:03
    It would be best to take another cup of coffee and reconsider the main logic smile.gif
  • MinimumWageMinimumWage Posts: 72
    edited 2008-01-26 19:32
    Graham,

    Thanks - that's what I realized I could do after re-reading the posts. I was spending too much time thinking about what would happen after the variable had been set, not what was happening before it was initalized. Thanks again!

    Mike
  • DgswanerDgswaner Posts: 795
    edited 2008-01-27 08:54
    I can hold my tongue no longer... are people just posting to hear themselves or to increase their post count? WOW!! This Forum is about help so if your comments aren't about help why bother saying them.. I won't point fingers but some one in this thread just got muted from my list. my apologies to you minimumwage for saying this in your thread... my comment wasn't targeted at you.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "A complex design is the sign of an inferior designer." - Jamie Hyneman, Myth Buster

    DGSwaner

    Post Edited (Dgswaner) : 1/27/2008 8:59:25 AM GMT
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-27 12:08
    There are many definitions of "help"... I don't think it's best to always reanimate drowned persons, but to lead them away from dangerious water... Ha, one more posting smile.gif
Sign In or Register to comment.