Using counters for long term seconds time keeping
max72
Posts: 1,155
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
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
He has an output pin toggling at 1Hz and counts that...
1Hz toggling is kinda nice because a lot of "real" RTCs do that...
-Phil
Actually, there is rounding error...
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
It's also on my website, called "Proptime".
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