Shop OBEX P1 Docs P2 Docs Learn Events
Timing — Parallax Forums

Timing

NamNoriNamNori Posts: 4
edited 2013-06-19 08:16 in Propeller 1
I'm attempting to time how long it takes for an airflow sensor to measure a burst of airflow. I've been trying to use the spin self-timing techniques noted in other postings and other resources:
elapsed := -cnt

repeat until <my sensor condition to stop is met>
  read sensor
  waitcnt(clkfreq + cnt)

elapsed += cnt - 544

term.dec(elapsed)

the value I receive in the serial terminal is something like 244956324. I should be getting something on the order of 2 seconds (give or take). I'm not sure how to either convert or interpret this value as seconds (or better yet, milliseconds). Can somebody suggest where I might be going wrong?

thanks
marc

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2013-06-18 23:02
    The number you're seeing is the number of seconds * clkfreq. The latter factor is typically 80_000_000. So 244_956_324 / 80_000_000 = 3.06 seconds (or 3 with integer division).

    For finer resolution, you may want to consider checking the sensor condition more frequently, e.g. :
    ms := clkfreq / 1000
    elapsed := -cnt
    
    repeat until <my sensor condition to stop is met>
      read sensor
      waitcnt(ms + cnt)
    
    elapsed += cnt - 544
    
    term.dec(elapsed / ms)
    

    This will present the elapsed time in milliseconds.

    -Phil
  • JonnyMacJonnyMac Posts: 9,107
    edited 2013-06-19 07:44
    I'm with Phil. Why put a delay after reading the sensor which just delays getting back to the conditions test? Remove the waitcnt to get the actual timing.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2013-06-19 08:16
    JonnyMac wrote:
    Remove the waitcnt to get the actual timing.
    Good point. The only good reason to include a waitcnt there would be if the system is battery-powered and reading the sensor incurs extra power consumption.

    -Phil
Sign In or Register to comment.