Shop OBEX P1 Docs P2 Docs Learn Events
Looking for a timer or clock object — Parallax Forums

Looking for a timer or clock object

mcstarmcstar Posts: 144
edited 2009-01-07 07:54 in Propeller 1
Has anyone done a timer in spin or assembly? I'm looking for something that keeps track of at least seconds and minutes (maybe milliseconds) and that doesn't reset or loose time when the system clock recycles. I suppose I could use an external rtc but it should be possible to just keep a memory location up to date with elapsed time converted to seconds. I'm measuring real long times (hours) for a battery charger so I need to keep track of much longer time than the system clock can do.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-01-06 23:46
    It's easy enough to do. Just launch a cog that repeatedly waits for a second using WAITCNT and increments a long. You'd have a Spin interface routine that simply converts the seconds count from the long into days, hours, minutes, and seconds.
  • Shane De CataniaShane De Catania Posts: 67
    edited 2009-01-07 00:57
    Hi mcstar, have you seen date_time from Bob Belleville in the object exchange - that may be what you are looking for. Not sure what section its in, just look under view all objects.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    News Flash!... Energizer Bunny arrested and charged with battery.
  • grasshoppergrasshopper Posts: 438
    edited 2009-01-07 03:24
    got this from someone in forums not in assembly but its a good one.

    Pub Main |  timer1, timer2
    
      timer1 := cnt 
      timer2 := cnt
    
      IF (MainClock(timer1)=> 1_000)                                          
                                    timer1 := cnt                                                                                                          
                                    {{do something after 1 sec}}
       IF (MainClock(timer2)=> 500)                                            
                                    timer2 := cnt                                                                                                          
                                    {{do something after 1/2 sec}}
    
    PUB MAINCLOCK(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)                               
    
    
    



    Best timer there is as far as i am concerned.
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-01-07 07:54
    Hello,

    I would like to add, this timer works properly for measuring times less than 53 seconds.
    (After 53 seconds there is a rollover of the systemcounter from max to zero)

    For longer times (like hours and days) you can use

    timer-object

    from the obex

    best regards

    Stefan
Sign In or Register to comment.