How to get millisecond resolution from DS3231 RTC

ahmad picture ahmad · Jun 20, 2017 · Viewed 12.8k times · Source

How to get accurate milliseconds?

I need to calculate the delay of sending data from Arduino A to Arduino B. I tried to use DS3231 but I cannot get milliseconds. What should I do to get accurate milliseconds from DS3231?

Answer

TomServo picture TomServo · Jun 20, 2017

The comment above is correct, but using millis() when you have a dedicated realtime clock makes no sense. I'll provide you with better instructions.

First thing in any hardware interfacing project is a close reading of the datasheet. The DS3231 datasheeet reveals that there are five possible frequencies of sub-second outputs (see page 13):

  1. 32 KHz
  2. 1 KHz
  3. 1.024 KHz
  4. 4.096 KHz
  5. 8.192 KHz

These last four options are achieved by various combinations of the RS1 and RS2 control bits.

So, for example, to get exact milliseconds, you'd target option 2, 1KHz. You set RS1 = 0 and RS2 = 0 (see page 13 of the datasheet you provided) and INTCN = 0 (page 9). Then you'd need an ISR to capture interrupts from the !INT/SQW pin of the device to a digital input pin on your Arduino.

volatile uint16_t milliseconds;  // volatile important here since we're changing this variable inside an interrupt service routine:
ISR(INT0_vect) // or whatever pin/interrupt you choose
{
    ++milliseconds;
    if(milliseconds == 999)  // roll over to zero
        milliseconds = 0;
}

OR:

const int RTCpin = 3; // use any digital pin you need.
void setup()
{
    pinmode(RTCpin, INPUT);
    // Global Enable INT0 interrupt
    GICR |= ( 1 < < INT0);
    // Signal change triggers interrupt
    MCUCR |= ( 1 << ISC00);
    MCUCR |= ( 0 << ISC01);
}

If these commands in setup() don't work on your Arduino, google 'Arduino external interrupt INT0'. I've shown you two ways, one with Arduino code and one in C.

Once you have this ISR working and pin3 of the DS3231 connected to a digital input pin of your choosing, that pin will be activated at 1KHz, or every millisecond. Perfect!

// down in main program now you have access to milliseconds, you might want to start off by setting:

// When 1-second RTC changes seconds:
milliseconds = 0;  // So you can measure milliseconds since last second.

That's all there is to it. All you need to learn now is how to set the command register using I2C commands and you're all set.