View Full Version : waitcnt problem

01-26-2008, 03:18 PM
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...


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

b_out1 = 1


long stack1[50]
long stack2[50]
long beatavg

PUB Main

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

PUB BeatCount

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



repeat while true
outa[b_out1] := !outa[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

Graham Stabler
01-26-2008, 05:13 PM
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.


01-26-2008, 05:39 PM
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..

01-26-2008, 06:56 PM
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.


Graham Stabler
01-26-2008, 07:15 PM

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


01-26-2008, 09:13 PM
Ah, we just wanted to hear something from Andy again....

01-27-2008, 12:16 AM
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?

01-27-2008, 12:21 AM
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 Stabler
01-27-2008, 01:00 AM
You could have a repeat while to wait for it to be initialized.


01-27-2008, 01:03 AM
It would be best to take another cup of coffee and reconsider the main logic http://forums.parallax.com/images/smilies/smile.gif

01-27-2008, 02:32 AM

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!


01-27-2008, 03:54 PM
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


Post Edited (Dgswaner) : 1/27/2008 8:59:25 AM GMT

01-27-2008, 07:08 PM
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 http://forums.parallax.com/images/smilies/smile.gif

01-28-2008, 02:25 AM
A forum is, according to the Oxford English Dictionary, "a place, meeting, or medium where ideas and views on a particular issue can be exchanged" which is generally what goes on here. Discussion, general or specific, which leads to better understanding is helpful...

Of course, this is only my opinion, I could be wrong.