Arduino - Tiny RTC 12C 时钟模块

测试了几个RTC库文件,不是很好用。后来终于有一个测试成功的,是下面的地址:
https://github.com/adafruit/RTClib
A fork of Jeelab’s fantastic RTC library
成功了,高兴一下。^-^

这是我买的时钟模块:
这是我买的时钟模块
这是接线方法:
这是接线方法
注意:A4是SDA,A5是SCL。

以下为控制代码:
【库文件:RTClib】
【注意点:A4是SDA,A5是SCL。】

#include <Wire.h>
#include <RTClib.h>

RTC_DS1307 RTC;

void setup () {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();
    if (! RTC.isrunning()) {
        Serial.println("RTC is NOT running!");
        //光庆注: 下面的代码用于矫正时间,__DATE__ 和 __TIME__ 应该是系统关键词,
        //指当前日期和当前时间,编译时直接将当前日期值和时间值写入芯片中,
        //而不是作为变量写入。 
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
}
void loop () {
    DateTime now = RTC.now(); 
    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.println(); 
    delay(1000);
}
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
以下是使用 Arduino 来初始化设置 Tiny RTC 12C 时钟模块的示例代码: ```cpp #include <Wire.h> #define DS1307_ADDRESS 0x68 // I2C address of the DS1307 RTC void setup() { Wire.begin(); // Initialize I2C communication Serial.begin(9600); // Initialize serial communication // Set the time and date on the DS1307 // Format: seconds, minutes, hours, day of the week, day, month, year setTimeDS1307(0, 0, 12, 5, 6, 11, 21); } void loop() { // Read the time from the DS1307 and print it to the serial monitor Serial.print(getHourDS1307()); Serial.print(":"); Serial.print(getMinuteDS1307()); Serial.print(":"); Serial.println(getSecondDS1307()); delay(1000); // Wait for one second } // Function to set the time and date on the DS1307 void setTimeDS1307(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year) { Wire.beginTransmission(DS1307_ADDRESS); Wire.write(0x00); // Set the register pointer to 00h Wire.write(decToBcd(second)); // Set the seconds Wire.write(decToBcd(minute)); // Set the minutes Wire.write(decToBcd(hour)); // Set the hours Wire.write(decToBcd(dayOfWeek)); // Set the day of the week (1=Sunday, 7=Saturday) Wire.write(decToBcd(dayOfMonth)); // Set the day of the month Wire.write(decToBcd(month)); // Set the month Wire.write(decToBcd(year)); // Set the year (0-99) Wire.endTransmission(); } // Function to get the hour from the DS1307 byte getHourDS1307() { Wire.beginTransmission(DS1307_ADDRESS); Wire.write(0x02); // Set the register pointer to 02h Wire.endTransmission(); Wire.requestFrom(DS1307_ADDRESS, 1); // Request one byte of data byte hour = bcdToDec(Wire.read() & 0x3f); // Get the hour (0-23) return hour; } // Function to get the minute from the DS1307 byte getMinuteDS1307() { Wire.beginTransmission(DS1307_ADDRESS); Wire.write(0x01); // Set the register pointer to 01h Wire.endTransmission(); Wire.requestFrom(DS1307_ADDRESS, 1); // Request one byte of data byte minute = bcdToDec(Wire.read()); // Get the minute (0-59) return minute; } // Function to get the second from the DS1307 byte getSecondDS1307() { Wire.beginTransmission(DS1307_ADDRESS); Wire.write(0x00); // Set the register pointer to 00h Wire.endTransmission(); Wire.requestFrom(DS1307_ADDRESS, 1); // Request one byte of data byte second = bcdToDec(Wire.read() & 0x7f); // Get the second (0-59) return second; } // Function to convert a decimal number to BCD byte decToBcd(byte val) { return ( (val/10*16) + (val%10) ); } // Function to convert a BCD number to decimal byte bcdToDec(byte val) { return ( (val/16*10) + (val%16) ); } ``` 这个示例代码展示了如何使用 Wire 库来读写 I2C 总线上的数据。首先,我们需要使用 `Wire.begin()` 来初始化 I2C 通信。然后,使用 `setTimeDS1307()` 函数来设置时间和日期。该函数会将当前时间和日期写入到 DS1307 的寄存器中。接下来,在 `loop()` 函数中,我们使用 `getHourDS1307()`、`getMinuteDS1307()` 和 `getSecondDS1307()` 函数来读取当前时间,并将其打印到串口监视器中。最后,我们使用 `delay(1000)` 函数来等待一秒钟,以确保每秒钟只打印一次时间。 请注意,上面的示例代码使用 DS1307 地址为 0x68。如果你使用的是不同的 RTC 时钟模块,可能需要修改地址。同时,你需要在 Arduino 中下载安装 Wire 库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卢光庆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值