0

i am trying to make a code that prints the time hh:mm:ss:ms using RTC,i have made it using millis() but it is not accurate +/-2 milliseconds

ho can i make it using sq wave and intterupt

here is the code that i have used

#include <Wire.h> #include "RTClib.h" RTC_DS1307 RTC; // This is the DS1307 hardware RTC RTC_Millis SoftRTC; // This is the millis()-based software RTC long startMS; // This will hold the start time in milliseconds void setup () { Wire.begin(); RTC.begin(); // Connect to the DS1307 SoftRTC.begin(RTC.now()); // Initialize SoftRTC to the current time startMS = millis(); // get the current millisecond count } void loop() { DateTime now = SoftRTC.now(); long nowMS = millis(); Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(' '); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.print(':'); Serial.print((nowMS - startMS)%1000, DEC); // print milliseconds Serial.println(); 

i tried to put this code that is based on ISR but with no luck

volatile time_t isrUTC; // ISR's copy of current time in UTC volatile time_t millisecondsFromISR; // ISR's copy of current time millisecond offset void incrementTime() { millisecondsFromISR = millis(); //do first to get most accuracy ++isrUTC; } int getMilliseconds() { // need to make an atomic copy: nointerrupts(); unsigned long t = millisecondsFromISR; interrupts(); return millis() - t; } 
4
  • 1
    what is not accurate +/-2 milliseconds? ... the prints the time hh:mm:ss:ms takes time to execute Commented Apr 2, 2022 at 21:09
  • so if i make it with external clock will it make a diff? Commented Apr 3, 2022 at 4:46
  • 1
    You cannot get better than ±2 ms with millis(). If you need more accuracy, use micros(). Commented Apr 3, 2022 at 13:56
  • same code or what do u suggest? Commented Apr 3, 2022 at 14:36

1 Answer 1

1

All you have to do is record the number of milliseconds at the moment the square wave toggles on its 1 second cycle. Then your milliseconds is the difference between that and the current.

In short: update startMS when triggered by the RTC's output.

11
  • do u have a helping code? Commented Apr 3, 2022 at 4:46
  • @AmrAhmed no, but you already have code to set startms in your program, and there are plenty of interrupt examples on the web. Commented Apr 3, 2022 at 9:00
  • yes i have checked these codes for interrupt with no luck to make a running code Commented Apr 3, 2022 at 9:02
  • @AmrAhmed Then maybe you should try again, and then show us what you tried, and we may be able to help you with it. Commented Apr 3, 2022 at 9:56
  • volatile time_t isrUTC; // ISR's copy of current time in UTC volatile time_t millisecondsFromISR; // ISR's copy of current time millisecond offset ... void incrementTime() { millisecondsFromISR = millis(); //do first to get most accuracy ++isrUTC; } ... int getMilliseconds() { // need to make an atomic copy: nointerrupts(); unsigned long t = millisecondsFromISR; interrupts(); return millis() - t; } this one of the codes that i tried to work on with no luck Commented Apr 3, 2022 at 10:09

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.