An object to create 'NowSec'

I am sure this problem has been solved before. I have searched around but I cannot find it:
What I need is a counter of seconds since boot time. Thanks to some tidying up of my code, I now have a cog to spare. I assume a dedicated cog is required - or a dedicated chip.
Any idea for an Object - or a chip - is much appreciated.

Erlend

Comments

  • Well my solution would be to use a micro with a 64b timer. I use a good number of Micromites anyway so for ~$5 I would have that feature plus I/O expansion, I2C, SPI, analog, etc.
  • If @Erlend has a spare cog then there's no need for another chip. I'd just write a simple Spin function to increment a timer every second, something like:
    '' WARNING: this is untested code, it's just a proof of concept
    ''
    VAR
       long seconds_since_boot
       long stack[8] ' probably bigger than needed
    
    '' function to increment seconds_since_boot once per second
    PRI timer | next, freq
      freq := _CLKFREQ
      next := CNT
      repeat
        waitcnt(next += freq)
        seconds_since_boot++
    
    '' call starttimer to start the timer
    PUB starttimer
      cognew(timer, @stack)  ' run the timer function in a new COG
    
    '' call gettimer to get the integer count of seconds since boot
    PUB gettimer
      return seconds_since_boot
    
  • RaymanRayman Posts: 10,226
    edited 2019-12-29 - 16:20:33
    Here's PropTime, where you use a cog (doesn't have to be dedicated) to act as a RTC:
    rayslogic.com/propeller/Programming/RTC/PropTime.zip

    Posted about it here a long time ago: https://forums.parallax.com/discussion/106263/proptime-unix-style-rtc-using-one-cog/p1
  • JonnyMacJonnyMac Posts: 6,429
    edited 2019-12-29 - 20:23:05
    Almost all of my commercial programs have a background process for low-bandwidth housekeeping chores. This code runs in a 1ms loop (you can do a surprising amount of work in a millisecond -- even in Spin), so it's great for time-keeping.

    I've attached a little demo that shows how I use my background process. If you don't have a spare cog, my time object (jm_time_80.spin) will keep running count of milliseconds as well -- the difference is that you MUST access one of the methods that call the mark() method in that object before the cnt register runs a full, 32-bit cycle (about 53 seconds at 80MHz). Use of this is easy, just call time.seconds when you want to know the elapsed seconds. I'm doing this in the demo as well.

    I created jm_time_80.spin because I was coding a laser-tag controller and needed a bunch of timers, but had no available cogs (hence no background process). Thankfully, it all worked out well. If you don't need to do anything else as a background process, it's a good way to go (as others have shown with similar code).

    It can be this easy to change the state of the LED based on the time since boot.
    obj
      runtime : "jm_time_80"
    
    pub main
    
      runtime.start
      dira[LED] := 1
    
      repeat
        outa[LED] := runtime.seconds & 1
    
  • Incredible. I did not imagine you guys had so many good solutions up your sleeve. I love the idea to put one of the cheap micro controllers lying around to use, but then I'd have to do C programming - which is no fun. In this particular application the jm_time_80 is a brilliant solution, since my main code has a cycle that repeats (irregularly) every few seconds, and therefore can do the required mark call.
    Thanks a lot!
  • Erlend wrote: »
    Incredible. I did not imagine you guys had so many good solutions up your sleeve. I love the idea to put one of the cheap micro controllers lying around to use, but then I'd have to do C programming - which is no fun. In this particular application the jm_time_80 is a brilliant solution, since my main code has a cycle that repeats (irregularly) every few seconds, and therefore can do the required mark call.
    Thanks a lot!

    If someone had the time to go through all the posts and collect the code and ideas posted by the forumistas over the years it would probably cover 99% of the code needed for any project.
Sign In or Register to comment.