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

查看:515
本文介绍了如何从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 uint_16t 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);
}

如果setup()中的这些命令在您的Arduino上不起作用,请使用Google'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天全站免登陆