FlexBASIC feature request
Mickster
Posts: 2,693
I do this a lot:
Or is this the way to do it?:
Would there be problems with getcnt() rollover?
'set timeout to 5ms timeout = timer + 5 do if timer > timeout then exit do loop
Or is this the way to do it?:
timeout = getcnt() + (clkfreq/200) do if getcnt() > timeout then exit do loop
Would there be problems with getcnt() rollover?
Comments
This way even if the subtraction spans a rollover, the result will still be positive and compare properly against the timeout value.
I still need a general purpose ms timer as I like to monitor the response times of many actuators for diagnostic purposes.
One of my processes only needs a 1ms scan so I think I will increment a variable in that cog, after a waitcnt and use that as a reference
If I name it Timer:
Micromite BASIC has a 64bit ms counter which I would never have to worry about but I still zero it because I can
I don't know why. It seems odd that the compiler would arbitrarily change inline assembly code. Maybe Eric can assist -- or even add the millis() function to FlexBASIC.
Hey Jon.
Isn't that solution still prone to falling into the rollover trap?
BTW, congrats on the excellent N&V article. If it was mentioned here then I missed it. I found the link on another forum.
The bonus was the comments section 😂😂😂. Some guy made a snide remark and I'm pretty sure the reply came from @ManAtWork, right? It was brilliant
As they say; the article needs to go viral
Using the 64-bit version of cnt gives you more range -- but you have to do this from assembly. You can use the lower half of cnt (getct() in the P2) if your events are very short. I run at 200MHz, so using the lower half of cnt means I have to access that register faster than every 21.4s, otherwise I will get a roll-over error.
I'm glad you enjoyed the article.
Ironically, there is a builtin _getms() function, but I forgot to document it and/or provide an alias for BASIC. @Mickster : Jon's function above should work, or you could call "_getms()" (underscore at the beginning) which does the same thing: provides a 32 bit millisecond timer.
The next version of FlexBASIC will have a "getms()" function (no underscore). The underscore version will continue to be supported.
See this thread: http://forums.parallax.com/discussion/172453/free-running-timer-in-the-spin-interpreter-cog#latest
With FlexBASIC, you could probably do something similar by creating a timer cog with access methods that suited your needs. Given that the Flex system compiles to native PASM, you don't have the Spin cog like I do to install an ISR within.
@JonnyMac
Thanks guys, it's all looking great.
Away working right now. Didn't bring my Prop stuff (easily sidetracked)