Shop OBEX P1 Docs P2 Docs Learn Events
Spin Language - Monitoring system clocks ticks — Parallax Forums

Spin Language - Monitoring system clocks ticks

JoeFLJoeFL Posts: 10
edited 2011-07-29 16:57 in Propeller 1
I'm fairly NEW to Spin, but picking it quite quickly. I know about the waitcnt() function where one
can wait a certain amount of time before program execution is continued.

I would like to be able to monitor the amount of system clock pulses/ticks since a certain event has happened without pausing program execution.

For instance, if an event has happened, not necessarily a Port event, I would like to be able to execute another event at a certain number of clock cycles since that event.

Say ... Time_Mark := cnt
Time_Mark_1/4sec := cnt + clkfreq/4
I'd like to be able to test whether Time_Mark_1/4sec has been reached.

Comments

  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-07-29 12:56
    so something like this?
    timespan := clkfreq >> 2 ' (clkfreq / 4 == quarter second)
    
      event_time := cnt ' mark when the event happened
      REPEAT UNTIL ((cnt - event_time) => timespan)
        '' do anything here, while waiting for enough cycles to pass
      ' timed event goes here
    
    or
    IF ((cnt - event_time) => timespan) ' just test at any point in your program
        ' timed event goes here
    
    You can't do a simple "is-current-time-greater-than-target-time" because the clock only holds 32 bits, thus it rolls over, and it is signed, so you would get anomalous instantly true or always false problems.
  • ypapelisypapelis Posts: 99
    edited 2011-07-29 12:58
    Joe,

    if you don't want to wait by using waitcnt, you can simply poll the value of cnt as it is by definition the counter of system clock pulses.

    So to your code above, you can use : if ( cnt >= Time_Mark_1/4sec ) to detect if that time has been reached
  • JasonDorieJasonDorie Posts: 1,930
    edited 2011-07-29 13:05
    ypapelis: That may not always work, since longs are signed and the value may wrap around. For example, if the counter is 2,140,000,000 and you add 80 million to it, it rolls over past $7FFF_FFFF, which makes it a negative number. Comparing cnt to that value before the counter ALSO wraps around will incorrectly pass your test. Bob's version will work in either case.
  • kuronekokuroneko Posts: 3,623
    edited 2011-07-29 16:57
    There was a [thread=133065]similar thread[/thread] recently about the same problem in PASM. Apart from the fact that SPIN uses signed arithmetic (which would confine you to about 26sec @80MHz) this approach may be easier.
    PUB null | goal
    
      ctra := constant(%0_11111_000 << 23)                  ' LOGIC always counter
      frqa := 1                                             ' increment at same rate as cnt
    
    ' Say ... Time_Mark := cnt
    ' Time_Mark_1/4sec := cnt + clkfreq/4
    ' I'd like to be able to test whether Time_Mark_1/4sec has been reached
    
      goal := clkfreq/4                                     ' 1/4sec
      [COLOR="red"]phsa := 0                                             ' reset timer[/COLOR]
    
      ' do stuff
    
      if phsa > goal
        ' we spent more than 1/4 of a second
      else
        ' we still have some time left
    
    The important bit here is to reset the timer occasionally, otherwise you get into (signed) trouble.
Sign In or Register to comment.