Shop OBEX P1 Docs P2 Docs Learn Events
Using counters for long term seconds time keeping — Parallax Forums

Using counters for long term seconds time keeping

max72max72 Posts: 1,155
edited 2011-02-25 13:57 in Propeller 1
I am playing with counters, and I would like to use them to sample time (seconds) in place of the cnt test.
This way I don't worry about rollover if I cannot check cnt for some time.
I could do the testing in a COG running routine tasks, like graphics or similar, but counters are so intriguing....
So I made the follong object.
In order to reduce the errors I have to hardcode the values, and a pin must be dedicated to this task.
Is there a smarter way?
Thanks in advance,
Massimo
CON

' must set appropriate settings
' clkfreq 80_000_000
' -7.61 ppm   rollover every 3107 days
'freq1 = 859
'shift1 = 4

' clkfreq 80_000_000
' 1.48 ppm    rollover every 24 days
freq1 = 109951
shift1 = 11

' clkfreq 100_000_000
' 283 ppm
'freq1 = 387
'shift1 = 4

' clkfreq 100_000_000
' 11 ppm
'freq1 = 10995
'shift1 = 8

' clkfreq 100_000_000
' -0.79 ppm
'freq1 = 87961
'shift1 = 11


  

PUB start (pin)
' sets pin as output
  DIRA[pin]~~
  CTRa := %00100<<26 + pin           ' set oscillation mode on pin
  FRQa := freq1                      ' set FRequency of first counter                   

  CTRB := %01010<<26 + pin           ' at every zero crossing add 1 to phsb
  FRQB := 1                   
 


pub seconds
   return (phsb>>shift1)

Comments

  • RaymanRayman Posts: 14,886
    edited 2011-02-25 08:17
    I have some code at home from deSilva (haven't seen him in a long time...) that does a similar thing.
    He has an output pin toggling at 1Hz and counts that...
    1Hz toggling is kinda nice because a lot of "real" RTCs do that...
  • max72max72 Posts: 1,155
    edited 2011-02-25 08:36
    I started with a 1 second toggle too, but there is a non negligible drift due to rounding. There are multiples where the integer freq value is closer to the "exact" value, so I was considering this option to reduce time drift.
  • RaymanRayman Posts: 14,886
    edited 2011-02-25 08:49
    Hmm... I'll have to dig up deSilva's code when I get home... I don't remember any rounding issues...
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-02-25 08:58
    I don't see how there could be rounding issues or drift with a 1 Hz clock, so long as the crystal is accurate and has an even-integer frequency. [See below.]

    -Phil
  • RaymanRayman Posts: 14,886
    edited 2011-02-25 09:06
    Ok, I found his code...
    Actually, there is rounding error...
    PUB Start(pin)  'Start the Time driver.  Parameter is pin to blink at 1 Hz (required).
      commPin:=pin
      'Use this cogs (often unused) counters to act as a RTC (real-time clock)
      'Counter A will blink the pin at 1 Hz, Counter B will count blinks...
      'An LED at comPin can be used to show the second (even without a resistor) 
      ctra := %00100<<26  + commPin       ' Set NCO mode 
      frqa := POSX/CLKFREQ*2              ' one second
      ctrb := %01010<<26  + commPin       ' set ctrb to POSEDGE detect
      frqb := 1
      dira[commPin] := 1
    

    I don't use his code more because it uses a pin...
    I think it's better to add timekeeping to a driver.
    I just posted code for my monochome Graphical LCD that includes timekeeping in the driver.
    A lot of drivers have a main loop where they're just sitting around waiting for something. That's a good place to add in timekeeping...
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-02-25 09:12
    Yeah, I see it now. Scratch what I said. The crystal frequency would have to be a power of two -- not just even -- to prevent roundoff for a 1 Hz clock.

    -Phil
  • RaymanRayman Posts: 14,886
    edited 2011-02-25 10:51
    BTW: If you do go with a seconds counter, that graphical LCD driver code has code in there that can convert from a 32-bit binary number to and from the time of day...
    It's also on my website, called "Proptime".
  • max72max72 Posts: 1,155
    edited 2011-02-25 13:57
    I only need a seconds counter, for regatta time keeping.
    Anyway I implemented a seconds counter similar to the "proptime", only counting seconds.
    My problem is probably more based on the principle or estetic.
    A dedicated cog feels like a waste of resources.
    Using a running cog is better. I did it with a button debounce. It could be done with many others objects doing a similar task. In my case a good candidate is graphics, but it is not a neat solution. Next time I might not be able to reuse the code, so it doesn't feel the best solution.
    Last option is the one investigated here.
    The advantage is it is almost zero footprint. Drawbacks are a sacrificed pin and a little error to be added to the xtal one.
    Probably there is not a solution so evidently better that it is a hands down winner.

    Massimo
Sign In or Register to comment.