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;
}
1 Answer 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.
-
do u have a helping code?Amr Ahmed– Amr Ahmed2022年04月03日 04:46:39 +00:00Commented 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.Majenko– Majenko2022年04月03日 09:00:00 +00:00Commented Apr 3, 2022 at 9:00
-
yes i have checked these codes for interrupt with no luck to make a running codeAmr Ahmed– Amr Ahmed2022年04月03日 09:02:39 +00:00Commented 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.Majenko– Majenko2022年04月03日 09:56:37 +00:00Commented 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 luckAmr Ahmed– Amr Ahmed2022年04月03日 10:09:15 +00:00Commented Apr 3, 2022 at 10:09
prints the time hh:mm:ss:ms
takes time to executemillis()
. If you need more accuracy, usemicros()
.