找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 3075|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

Arduino驅(qū)動(dòng)DS3231高精度時(shí)鐘模塊

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:344144 發(fā)表于 2018-6-3 18:45 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
#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() {
// 啟動(dòng)I2C(IIC)接口
Wire.begin();
        //以下部分是初始化時(shí)間,每次板子通電之后都會(huì)初始化成這個(gè)時(shí)間,只是測(cè)試用,以后可以刪除。
        Clock.setSecond(50);//Set the second
        Clock.setMinute(59);//Set the minute 設(shè)置分鐘
        Clock.setHour(11);  //Set the hour 設(shè)置小時(shí)
        Clock.setDoW(5);    //Set the day of the week 設(shè)置星期幾
        Clock.setDate(31);  //Set the date of the month 設(shè)置月份
        Clock.setMonth(5);  //Set the month of the year 設(shè)置一年中的月份
        Clock.setYear(13);  //Set the year (Last two digits of the year) 設(shè)置年份(在今年的最后兩位數(shù)——比如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秒)循環(huán)一次。
}




分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:234938 發(fā)表于 2020-1-14 22:32 | 只看該作者
感謝樓主分享!請(qǐng)問(wèn)這個(gè)DS3231是否可以直接獲取電腦時(shí)間?
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表