Shop OBEX P1 Docs P2 Docs Learn Events
How to convert CNT ticks to milliseconds? — Parallax Forums

How to convert CNT ticks to milliseconds?

AkkarinAkkarin Posts: 43
edited 2012-08-16 06:53 in Propeller 1
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 ;)
        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

  • Mark_TMark_T Posts: 1,981
    edited 2012-08-16 05:06
    You could convert to multiples of 10us, then divide and remainder by 100 to get milliseconds with 2 decimals, something like
      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.NewLine
    

    Assuming you want similar accuracy - this is a common trick for avoiding the overhead of floats.
  • AkkarinAkkarin Posts: 43
    edited 2012-08-16 05:37
    I knew I'm gonna be embarrassed. ;)

    Thanks Mark_T
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-08-16 06:53
    If you're just don't simple arithmetic, you don't really need floating point numbers (as Mark_T shows).

    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.NewLine
    

    Mark's original code wouldn't display all the numbers correctly. If "10.01" were to be displayed, it would show up "10.1".
Sign In or Register to comment.