Skip to main content
Bumped by Community user
added 585 characters in body
Source Link

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;
}

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 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;
}
Source Link

Milliseconds using square wave RTC

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();