0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

Arduino篇—實(shí)時(shí)時(shí)鐘

冬至子 ? 來(lái)源:X的創(chuàng)客課堂 ? 作者:MRXY ? 2023-11-01 16:49 ? 次閱讀

學(xué)習(xí)目標(biāo)

  • DS1307時(shí)鐘模塊的原理與應(yīng)用
  • I2C接口讀寫的工作過(guò)程
  • 學(xué)會(huì)用I2C編程控制時(shí)鐘模塊

相關(guān)知識(shí)

DS1307時(shí)鐘模塊: DS1307串行實(shí)時(shí)時(shí)鐘(RTC)是低功耗,全二進(jìn)制編碼的十進(jìn)制(BCD)時(shí)鐘/日歷以及56字節(jié)的NV SRAM。地址和數(shù)據(jù)通過(guò)I2C雙向總線串行傳輸。時(shí)鐘/日歷提供秒,分鐘,小時(shí),日期,日期,月份和年份信息。

RTC模塊的接線:

圖片

I2C總線協(xié)議: 集成電路總線,它是一種串行通信總線,使用多主從架構(gòu)。I2C總線只有兩根雙向信號(hào)線。一根是數(shù)據(jù)線SDA,另一根是時(shí)鐘線SCL。SCL:上升沿將數(shù)據(jù)輸入到每個(gè)EEPROM器件中;下降沿驅(qū)動(dòng)EEPROM器件輸出數(shù)據(jù)。(邊沿觸發(fā))

圖片

BCD碼: 用4位二進(jìn)制數(shù)來(lái)表示1位十進(jìn)制數(shù)中的0~9這10個(gè)數(shù)碼,是一種二進(jìn)制的數(shù)字編碼形式,用二進(jìn)制符號(hào)來(lái)表示十進(jìn)制數(shù)。BCD碼有:8421碼、5421碼、2421碼等多種類型。

8421碼示例:

圖片

電路搭建

所需材料

ArduinoUNO * 1
DS1307 RTC模塊 * 1
LCD1602液晶顯示屏 * 1
電位器 * 1
杜邦線若干

電路連接

圖片

程序編寫

練習(xí)一:時(shí)間設(shè)置

如圖連接電路后,編寫程序?qū)崿F(xiàn)將RTC模塊進(jìn)行當(dāng)前時(shí)間設(shè)置。

這里我們直接調(diào)用了DS1307RTC.h這個(gè)庫(kù)文件里面的例子實(shí)現(xiàn)校準(zhǔn)時(shí)間的功能。

#include < Wire.h >
#include < TimeLib.h >
#include < DS1307RTC.h >
const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
tmElements_t tm;
void setup() {
  bool parse=false;
  bool config=false;


  // get the date and time the compiler was run
  if (getDate(__DATE__) && getTime(__TIME__)) {
    parse = true;
    // and configure the RTC with this info
    if (RTC.write(tm)) {
      config = true;
    }
  }


  Serial.begin(9600);
  while (!Serial) ; // wait for Arduino Serial Monitor
  delay(200);
  if (parse && config) {
    Serial.print("DS1307 configured Time=");
    Serial.print(__TIME__);
    Serial.print(", Date=");
    Serial.println(__DATE__);
  } else if (parse) {
    Serial.println("DS1307 Communication Error :-{");
    Serial.println("Please check your circuitry");
  } else {
    Serial.print("Could not parse info from the compiler, Time="");
    Serial.print(__TIME__);
    Serial.print("", Date="");
    Serial.print(__DATE__);
    Serial.println(""");
  }
}


void loop() {
}


bool getTime(const char *str)
{
  int Hour, Min, Sec;


  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}


bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;


  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}

練習(xí)二: 可顯示的時(shí)鐘

在LCD1602上實(shí)時(shí)顯示時(shí)間。

/*項(xiàng)目名稱:實(shí)時(shí)時(shí)鐘
 *項(xiàng)目時(shí)間:2022.03.14
 *項(xiàng)目作者:MRX
 */
#include < Wire.h >
#include < TimeLib.h >
#include < DS1307RTC.h >
#include < LiquidCrystal.h >
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


void setup() {
lcd.begin(16,2);
}


void loop() {
tmElements_t tm;
lcd.clear();
if (RTC.read(tm))
{
if(tm.Hour >=12)
{
lcd.setCursor(14,0);
lcd.print("PM");
}
if(tm.Hour< 12)
{
lcd.setCursor(14,0);
lcd.print("AM");
}
lcd.setCursor(0,0);
lcd.print("TIME:");
lcd.print(tm.Hour);
lcd.print(":");
lcd.print(tm.Minute);
lcd.print(":");
lcd.print(tm.Second);
lcd.setCursor(0,1);
lcd.print("DATE:");
lcd.print(tm.Day);
lcd.print("/");
lcd.print(tm.Month);
lcd.print("/");
lcd.print(tmYearToCalendar(tm.Year));
} else {
if (RTC.chipPresent()) {
lcd.setCursor(0,0);
lcd.print("RTC stopped!!!");
lcd.setCursor(0,1);
lcd.print("Run SetTime code");
} else {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Read error!");
lcd.setCursor(0,1);
lcd.print("Check circuitry!");
}
delay(500);
}
delay(500);
}
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 實(shí)時(shí)時(shí)鐘

    關(guān)注

    4

    文章

    233

    瀏覽量

    65606
  • RTC
    RTC
    +關(guān)注

    關(guān)注

    2

    文章

    515

    瀏覽量

    66116
  • Arduino
    +關(guān)注

    關(guān)注

    187

    文章

    6455

    瀏覽量

    186346
  • SRAM芯片
    +關(guān)注

    關(guān)注

    0

    文章

    65

    瀏覽量

    12031
  • DS1307
    +關(guān)注

    關(guān)注

    1

    文章

    34

    瀏覽量

    14093
收藏 人收藏

    評(píng)論

    相關(guān)推薦

    STM32 RTC實(shí)時(shí)時(shí)鐘(一)

    STM32處理器內(nèi)部集成了實(shí)時(shí)時(shí)鐘控制器(RTC),因此在實(shí)現(xiàn)實(shí)時(shí)時(shí)鐘功能時(shí),無(wú)須外擴(kuò)時(shí)鐘芯片即可構(gòu)建實(shí)時(shí)時(shí)鐘系統(tǒng)。
    的頭像 發(fā)表于 07-22 15:41 ?4407次閱讀
    STM32 RTC<b class='flag-5'>實(shí)時(shí)時(shí)鐘</b>(一)

    【自制】Arduino:oled0-96實(shí)時(shí)時(shí)鐘顯示(mini桌面時(shí)鐘part12

    實(shí)時(shí)時(shí)鐘Arduino
    新創(chuàng)司XLOT
    發(fā)布于 :2022年03月01日 16:24:34

    FRDM-KL25Z系列講座之實(shí)時(shí)時(shí)鐘RTC

    大家好,附件里是FRDM-KL25Z系列講座之實(shí)時(shí)時(shí)鐘RTC。
    發(fā)表于 05-07 22:33

    轉(zhuǎn):基礎(chǔ)27--劉洋邊講邊寫STM32 .RTC實(shí)時(shí)時(shí)鐘實(shí)驗(yàn)

    【眾想】大黃蜂STM32視頻教程 ----劉洋邊講邊寫基礎(chǔ)27--RTC實(shí)時(shí)時(shí)鐘實(shí)驗(yàn)http://www.stmcu.org/module/forum/thread-606955-1-1.html
    發(fā)表于 07-23 16:15

    實(shí)時(shí)時(shí)鐘是什么

    定義:實(shí)時(shí)時(shí)鐘的縮寫是RTC(Real Time Clock).實(shí)時(shí)時(shí)鐘是一個(gè)獨(dú)立的定時(shí)器。RTC模塊擁有一組連續(xù)計(jì)數(shù)的計(jì)數(shù)哭奮,在相應(yīng)軟件配置下,可提供時(shí)鐘日歷的功能。修改計(jì)數(shù)器的值可以重新設(shè)置系統(tǒng)當(dāng)前的時(shí)間和日期。...
    發(fā)表于 08-09 06:18

    實(shí)時(shí)時(shí)鐘模DS1302程序列子

    實(shí)時(shí)時(shí)鐘模DS1302程序列子     /*********************************************************************//* 實(shí)時(shí)時(shí)鐘模塊
    發(fā)表于 07-08 16:26 ?142次下載

    ARM基礎(chǔ)應(yīng)用實(shí)驗(yàn)_實(shí)時(shí)時(shí)鐘

    ARM嵌入式應(yīng)用程序架構(gòu)設(shè)計(jì)實(shí)例精講--ARM基礎(chǔ)應(yīng)用實(shí)驗(yàn)03實(shí)時(shí)時(shí)鐘
    發(fā)表于 07-08 11:08 ?0次下載

    基于實(shí)時(shí)時(shí)鐘模塊 時(shí)鐘芯片DS1302

    基于實(shí)時(shí)時(shí)鐘模塊 時(shí)鐘芯片DS1302
    發(fā)表于 10-16 11:35 ?40次下載
    基于<b class='flag-5'>實(shí)時(shí)時(shí)鐘</b>模塊 <b class='flag-5'>時(shí)鐘</b>芯片DS1302

    實(shí)時(shí)時(shí)鐘芯片應(yīng)用

    對(duì)于一些測(cè)控系統(tǒng)或者手持式設(shè)備,經(jīng)常需要顯示以及設(shè)定時(shí)間。目前,市場(chǎng)上有多種實(shí)時(shí)時(shí)鐘芯片提供了這類功能。這種可編程的實(shí)時(shí)時(shí)鐘芯片內(nèi)置了可編程的日歷時(shí)鐘以及一定的RAM存儲(chǔ)器,用于設(shè)定以及保存時(shí)間
    發(fā)表于 03-16 15:31 ?14次下載
    <b class='flag-5'>實(shí)時(shí)時(shí)鐘</b>芯片應(yīng)用

    實(shí)時(shí)時(shí)鐘的硬件結(jié)構(gòu)_實(shí)時(shí)時(shí)鐘故障怎么解決

     實(shí)時(shí)時(shí)鐘的縮寫是RTC(Real_Time Clock)。RTC 是集成電路,通常稱為時(shí)鐘芯片。
    的頭像 發(fā)表于 11-16 17:10 ?7012次閱讀
    <b class='flag-5'>實(shí)時(shí)時(shí)鐘</b>的硬件結(jié)構(gòu)_<b class='flag-5'>實(shí)時(shí)時(shí)鐘</b>故障怎么解決

    Arduino的實(shí)驗(yàn)例程之實(shí)時(shí)時(shí)鐘DS1302的詳細(xì)資料說(shuō)明

    本文檔的主要內(nèi)容詳細(xì)介紹的是Arduino的實(shí)驗(yàn)例程之實(shí)時(shí)時(shí)鐘DS1302的詳細(xì)資料說(shuō)明免費(fèi)下載。
    發(fā)表于 03-01 11:42 ?25次下載

    淺談RTC實(shí)時(shí)時(shí)鐘特征與原理

    一、RTC實(shí)時(shí)時(shí)鐘特征與原理 查看STM32中文手冊(cè) 16 實(shí)時(shí)時(shí)鐘(RTC)(308頁(yè)) RTC (Real Time Clock):實(shí)時(shí)時(shí)鐘 實(shí)時(shí)時(shí)鐘是一個(gè)獨(dú)立的定時(shí)器。RTC模塊
    的頭像 發(fā)表于 06-30 15:54 ?1.1w次閱讀

    實(shí)時(shí)時(shí)鐘RTC】MSP430系統(tǒng)實(shí)時(shí)時(shí)鐘RTC學(xué)習(xí)日志(完善中)

    2012.1.11 讀取實(shí)時(shí)時(shí)鐘: 1、 RTCRDY 為0時(shí),不能取讀取實(shí)時(shí)時(shí)鐘 RT0PS源于ACLK,為了實(shí)時(shí)時(shí)鐘日歷的正確的運(yùn)行,ACLK必須是32768Hz。(易出錯(cuò)) 定時(shí)
    發(fā)表于 12-16 16:56 ?10次下載
    【<b class='flag-5'>實(shí)時(shí)時(shí)鐘</b>RTC】MSP430系統(tǒng)<b class='flag-5'>實(shí)時(shí)時(shí)鐘</b>RTC學(xué)習(xí)日志(完善中)

    CW32實(shí)時(shí)時(shí)鐘(RTC)介紹

    CW32實(shí)時(shí)時(shí)鐘(RTC)介紹
    的頭像 發(fā)表于 10-24 15:36 ?1015次閱讀
    CW32<b class='flag-5'>實(shí)時(shí)時(shí)鐘</b>(RTC)介紹

    使用實(shí)時(shí)時(shí)鐘庫(kù)

    電子發(fā)燒友網(wǎng)站提供《使用實(shí)時(shí)時(shí)鐘庫(kù).pdf》資料免費(fèi)下載
    發(fā)表于 10-22 10:09 ?0次下載
    使用<b class='flag-5'>實(shí)時(shí)時(shí)鐘</b>庫(kù)