Shop OBEX P1 Docs P2 Docs Learn Events
useful method elapsed_time_msec to measure time or wait for amount of time AND — Parallax Forums

useful method elapsed_time_msec to measure time or wait for amount of time AND

StefanL38StefanL38 Posts: 2,292
edited 2008-09-13 06:22 in Propeller 1
Hello,

The waitcnt-command stops the complete cog until the systemcounter matches
the value of the parameter

below is a code-example how you can to things again and again in a loop
AND wait for a timeperiod before some other code is executed

f.e. this could be used to let LED's blink at different frequencies with ONE cog in ONE loop
AND do other things at the SAME time. Just define a long for each Timer

CON
  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

  
VAR
  long timer1
  long eTime1

  long timer2
  long timer3

PUB Main
  'the FIRST PUB-Method inside a spinfile is ALWAYS the startpoint where the program starts to run    

  timer1 := cnt 'make inital snapshot of systemcounter 
  timer2 := cnt 'make inital snapshot of systemcounter 
  timer3 := cnt 'make inital snapshot of systemcounter 

  repeat
    'do whatever you like inside this loop
    'and what should be done with almost no delay
    
    'the method elapsed_time_msec calculates the time that has gone since
    'variable timer1 has set to the (former) value of systemcounter
    eTime1 := elapsed_time_msec(timer1) 

    if eTime1 => 100 '<== this is the TimePeriod of millisecs that were "waited" 'before the code below is executed 
      timer1 := cnt 'make new snapshot of systemcouner to set new startvalue
        ' code here what should be executed after the TimePeriod definded in the if condition above   

    if elapsed_time_msec(timer2) => 200 
      timer2 := cnt 'make new snapshot of systemcouner to set new startvalue
      'toggle LED2 or what ever    

    if elapsed_time_msec(timer3) => 400
      timer3 := cnt 'make new snapshot of systemcouner to set new startvalue
      'toggle LED3 or what ever    


      
PUB elapsed_time_msec(p_TimeVar) | RightNow, ClockTicks

  RightNow := cnt

  if RightNow < p_TimeVar                          
    ClockTicks := ||($FFFFFFFF - RightNow + p_TimeVar)  
  else
    ClockTicks := ||(RightNow - p_TimeVar)

  result := ClockTicks / (clkfreq / 1_000) 'calculate milliseconds


  '################################################################################
  'explanation how it works
  ' SPIN treats longs as SIGNED longs this means bit-No 32 represents the sign "+-"
  ' the systemcounter thinks of the 32bits as UNsigned meaning bit 32 is
  ' just another bit of the number and NOT a sign "+-" 
  ' if one or both values are negative it could happen
  ' that the result is negative too
  ' the command "||" calculates the absolute value of the SIGNED longs

  'even if the systemcounter has wrapped around since the snapshot of
  'systemcounter (value of parameter p_TimeVar),
  'this method calculates the timedifference in the right way
  
  'wrap-around example with easy numbers: counter maxvalue 1000
  'p_TimeVar containing value 980
  'time goes on counter wraps around from 1000 to 0
  'time goes on 
  'RightNow containing 300. This means 20 ticks until maximum 1000 and 300 ticks
  'since wrapping from 1000 to 0 in summary 320 ticks of time has gone
  'this is calculated by MaxCounter - p_TimeVar + RightNow
  'in numbers              1000     -   980     +   300     = 320
  'the real systemcounter has 32bits max value 2^32 -1 = 4294967295
  'hexadecimal $FFFFFFFF
  '################################################################################
 
'end of PUB elapsed_time_msec(p_TimeVar) | RightNow, ClockTicks






And I have attached an archive.

In the archived project attached there is a small demo-program
that shows how it works

In the demo it is used to wait some amount of time until something new happens
in a way, that you could do other things AT THE SAME TIME as the program is waiting.

Only restriction is: the "other things" and the new "wait-command" HAVE TO BE in the SAME loop

I attached the coded as an archived project because then you just unzip it
load mainfile Elapsed_Timer_Test and compile it without caring in which directory
it is unzipped or if you have Extend_FDSerial.spin downloaded from the obex or not.
It should run after ONE hit of F11. Use F11 to store it the EEPROM.
If you open the COM-Port with a terminalsoftware this may cause a reset and then
the content of RAM will be overwritten by the content of the EEPROM.

Enjoy using it

best regards

Stefan

Post Edited (StefanL38) : 9/13/2008 9:54:37 AM GMT
Sign In or Register to comment.