如何从 DS3231 RTC 获得毫秒分辨率 [英] How to get millisecond resolution from DS3231 RTC

查看:41
本文介绍了如何从 DS3231 RTC 获得毫秒分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得准确的毫秒数?

How to get accurate milliseconds?

我需要计算从 Arduino A 向 Arduino B 发送数据的延迟.我尝试使用 DS3231,但我无法获得毫秒.我应该怎么做才能从 DS3231 获得准确的毫秒数?

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?

推荐答案

上面的评论是正确的,但是当你有一个专用的实时时钟时使用 millis() 是没有意义的.我会为您提供更好的说明.

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.

任何硬件接口项目的第一件事都是仔细阅读数据表. DS3231 数据表显示亚秒级输出有五种可能的频率(参见第 13 页):

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

最后四个选项是通过 RS1 和 RS2 控制位的各种组合实现的.

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

因此,例如,要获得精确的毫秒数,您可以选择选项 2,1KHz.您设置 RS1 = 0 和 RS2 = 0(请参阅您提供的数据表的第 13 页)和 INTCN = 0(第 9 页).然后,您需要一个 ISR 来捕获从设备的 !INT/SQW 引脚到 Arduino 上的数字输入引脚的中断.

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

或:

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

如果 setup() 中的这些命令在您的 Arduino 上不起作用,请谷歌Arduino 外部中断 INT0".我已经向您展示了两种方式,一种使用 Arduino 代码,一种使用 C.

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.

一旦此 ISR 工作且 DS3231 的 pin3 连接到您选择的数字输入引脚,该引脚将以 1KHz 或每毫秒激活.完美!

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.

仅此而已.您现在需要学习的是如何使用 I2C 命令设置命令寄存器,您就大功告成了.

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.

这篇关于如何从 DS3231 RTC 获得毫秒分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆