How to convert CNT ticks to milliseconds?
Akkarin
Posts: 43
Hi there,
I use CNT to determine how long it takes to execute some commands. At the end I get around 500_000 ticks, which corresponds to 6.25ms.
How can I do this conversion in Spin?
At the moment I'm using "Float32Full" (fm in the code below) and "FloatToString" (fs in the code below) to get a useful result but I think this is quite inconvenient and also it doesn't look pretty in the code
I'm sure this can be done much simpler. I bet it's so simple I'll be embarrased that I even asked this ....
I use CNT to determine how long it takes to execute some commands. At the end I get around 500_000 ticks, which corresponds to 6.25ms.
How can I do this conversion in Spin?
At the moment I'm using "Float32Full" (fm in the code below) and "FloatToString" (fs in the code below) to get a useful result but I think this is quite inconvenient and also it doesn't look pretty in the code
stopWatch += cnt-544
msec := fm.FDiv(fm.Ffloat(stopWatch),fm.Ffloat(clkfreq))
pst.str(string("Time elapsed [ticks]: "))
pst.dec(stopWatch)
PST.NewLine
pst.str(string("Time elapsed [msec]: "))
pst.str(fs.FloatToString(fm.Fmul(msec,fm.FFloat(1000))))
pst.Home
I'm sure this can be done much simpler. I bet it's so simple I'll be embarrased that I even asked this ....

Comments
ten_us_units := stopWatch / 800 ' assuming 80MHz pst.str(string("Time elapsed [ticks]: ")) pst.dec(stopWatch) pst.NewLine pst.str(string("Time elapsed [msec]: ")) pst.dec(ten_us_units / 100) pst.write(".") pst.dec(ten_us_units // 100) pst.NewLineAssuming you want similar accuracy - this is a common trick for avoiding the overhead of floats.
Thanks Mark_T
Here's a more generic method for displaying "pseudo-real" numbers.
PUB DecPoint(value, denominator) if value < 0 Pst.Char("-") -value if value => denominator result := value / denominator Pst.Dec(result) value //= denominator else Pst.Char("0") Pst.Char(".") repeat while denominator > 1 denominator /= 10 if value => denominator result := value / denominator Pst.Dec(result) value //= denominator else Pst.Char("0")Here's Mark's code using the DecPoint method.
ten_us_units := stopWatch / 800 ' assuming 80MHz pst.str(string("Time elapsed [ticks]: ")) pst.dec(stopWatch) pst.NewLine pst.str(string("Time elapsed [msec]: ")) [COLOR=#ff0000] DecPoint(ten_us_units, 100) [/COLOR] pst.NewLineMark's original code wouldn't display all the numbers correctly. If "10.01" were to be displayed, it would show up "10.1".