找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2506|回复: 1
收起左侧

Arduino驱动DS3231高精度时钟模块

[复制链接]
ID:344144 发表于 2018-6-3 18:45 | 显示全部楼层 |阅读模式
#include <DS3231.h>
#include <Wire.h>
DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;
byte year, month, date, DoW, hour, minute, second;
void setup() {
// 启动I2C(IIC)接口
Wire.begin();
        //以下部分是初始化时间,每次板子通电之后都会初始化成这个时间,只是测试用,以后可以删除。
        Clock.setSecond(50);//Set the second
        Clock.setMinute(59);//Set the minute 设置分钟
        Clock.setHour(11);  //Set the hour 设置小时
        Clock.setDoW(5);    //Set the day of the week 设置星期几
        Clock.setDate(31);  //Set the date of the month 设置月份
        Clock.setMonth(5);  //Set the month of the year 设置一年中的月份
        Clock.setYear(13);  //Set the year (Last two digits of the year) 设置年份(在今年的最后两位数——比如2013年最后的13)
// Start the serial interface
Serial.begin(115200);
}
void ReadDS3231()
{
  int second,minute,hour,date,month,year,temperature;
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h12, PM);
  date=Clock.getDate();
  month=Clock.getMonth(Century);
  year=Clock.getYear();
  
  temperature=Clock.getTemperature();
  
  Serial.print("20");
  Serial.print(year,DEC);
  Serial.print('-');
  Serial.print(month,DEC);
  Serial.print('-');
  Serial.print(date,DEC);
  Serial.print(' ');
  Serial.print(hour,DEC);
  Serial.print(':');
  Serial.print(minute,DEC);
  Serial.print(':');
  Serial.print(second,DEC);
  Serial.print('\n');
  Serial.print("Temperature=");  //这里是显示温度
  Serial.print(temperature);
  Serial.print('\n');
}
void loop()
{
  ReadDS3231();
  delay(1000);  //间隔1000ms(1000ms=1秒)循环一次。
}




回复

使用道具 举报

ID:234938 发表于 2020-1-14 22:32 | 显示全部楼层
感谢楼主分享!请问这个DS3231是否可以直接获取电脑时间?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表