Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 8 of 8

Thread: millisecond precision from Teensy 3* RTC

  1. #1
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,593

    millisecond precision from Teensy 3* RTC

    From this discussion, https://community.nxp.com/thread/378715, I learned you can get 1/32768 second precision from the teeny 3* RTC. Here is a simple demo of combining RTC_TPR with RTC_TSR to get RTC millis
    https://github.com/manitou48/teensy3...ster/RTCms.ino

    The actual accuracy of the RTC time and frequency is another matter.

    Some anecdotal frequency accuracy data (ppm) for various MCUs:
    https://github.com/manitou48/crystal...r/crystals.txt

    I don't yet know the specs/part for the T3.5/3.6 crystals.

    Teensy Time library

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    12,752
    Thanks - works on the T-3.6 I have here.

  3. #3
    Senior Member blackketter's Avatar
    Join Date
    May 2015
    Location
    San Francisco
    Posts
    308
    Thanks, this is great. Any idea how to _set_ the RTC with ms accuracy?

  4. #4
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,593
    Quote Originally Posted by blackketter View Post
    Thanks, this is great. Any idea how to _set_ the RTC with ms accuracy?
    The teensy time library link above has various methods of setting time. Using serial/usb link to host computer, you can get sub-second accuracy, especially if you measure what the serial delay is. Using GPS with pps is probably the best. BUT keeping an accurate time requires adjusting the RTC crystal frequency, maybe with an NTP-like protocol. Crystal frequency can change with voltage, capacitance, age, and temperature. here is info on freq vs temperature,
    https://forum.pjrc.com/threads/24628...mperature-Data
    The teensy RTC has compensation logic to adjust the frequency.
    Buying a temperature-compensated RTC and coin-cell battery might be another solution.

  5. #5
    Senior Member blackketter's Avatar
    Join Date
    May 2015
    Location
    San Francisco
    Posts
    308
    Maybe I'm missing something, but the Time library deals only with second-level accuracy, as does Teensy3Clock.set(). With an accurate external clock (NTP, say) it would be great to resync the RTC with fractional seconds. I don't see how to set the RTC without losing fractional second time.

    Even with significant drift over time, having sub-second resolution from the RTC is useful for things like sweep-second hands on graphical clocks.

    I've built a C++ library on top of Time (https://github.com/blackketter/Clock) that does its best to calculate subsecond time using millis() and the RTC, but it would be preferable take advantage of the rtc hardware directly.

    Quote Originally Posted by manitou View Post
    The teensy time library link above has various methods of setting time. Using serial/usb link to host computer, you can get sub-second accuracy, especially if you measure what the serial delay is. Using GPS with pps is probably the best. BUT keeping an accurate time requires adjusting the RTC crystal frequency, maybe with an NTP-like protocol. Crystal frequency can change with voltage, capacitance, age, and temperature. here is info on freq vs temperature,
    https://forum.pjrc.com/threads/24628...mperature-Data
    The teensy RTC has compensation logic to adjust the frequency.
    Buying a temperature-compensated RTC and coin-cell battery might be another solution.

  6. #6
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,593
    I haven't tried it, but you would need your own set_time() function that sets both RTC_TSR and RTC_TPR. If host sends you time "on the second", then at 9600 baud it would take 10 or so milliseconds for character data to arrive, so you'd need to adjust TPR. If it turns out you can't set TPR, then you could wait 990ms before setting time to sec+1. there may be other ways ...

  7. #7
    Senior Member blackketter's Avatar
    Join Date
    May 2015
    Location
    San Francisco
    Posts
    308
    Thanks for the pointer, I'll give it a shot...

  8. #8
    Sorry to bring up this old thread, but i have a little confusion on the original sketch and the resulting output on the Serial Monitor. Can you explain a little bit more about exactly how this works?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •