clkfreq + cnt confusion
tobdec
Posts: 267
On page 51 of the Labs:Fundamentals book revision 1.2 in the timing delays with the system clock section. Second paragraph it says add cnt to clkfreq in the waitcnt system variable. I'm a little confused as to why I should or must do this.....what exactly is going on here? Please excuse my ignorance I'm still pretty new to Props.
Comments
-Phil
waitcnt(clkfreq/10 + cnt) 'means wait for 1s/10 which is 100ms
waitcnt(clkfreq*5 + cnt) 'means wait for 1s*5 which is 5 seconds
You can delay this way to around 56 seconds IIRC (just make sure it is <50s and you should be fine).
There is a minimum because of the spin overhead, but IIRC 1us is fine and close enough for good resolution.
Also, while in waitxxx the cog is in low power mode so you save power too.
Also notice that if your program appears to freeze for around a minute, there it likely an error in this area of your code. It you set it up wrong, it has to wait this long until the count rolls over.
I think the minimun wait time is 381 clock cycles. The object "clock" in the Propeller Tool library uses this as the minimum.
So you want to make sure if you're calculating a wait period based on the clock frequency, it doesn't fall below 381.
Here's the way a "Duration" number of microseconds is computed in the "clock" object:
I think the value "3928" takes into account the system overhead of making the calculations.
The "#> WMin" makes sure the computed value isn't less than 381 (which is the value of the constant "WMin").
Electrodude
But you get precise timing if you do it this way:
The reason this is often better is that all of the "do stuff" code and overhead are included in the time interval, with waitcnt dealing only with whatever time is left over.
-Phil