注釈
こんにちは、SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Communityへようこそ!Facebook上で、仲間と一緒にRaspberry Pi、Arduino、ESP32をさらに深く探求しましょう。
なぜ参加するのか?
専門的なサポート:購入後の問題や技術的な課題をコミュニティやチームの助けを借りて解決。
学びと共有:スキルを向上させるためのヒントやチュートリアルを交換。
限定プレビュー:新製品発表や予告編に早期アクセス。
特別割引:最新製品の特別割引を楽しむ。
フェスティブプロモーションとプレゼント:プレゼントやホリデープロモーションに参加。
👉 私たちと一緒に探索と創造を始める準備はできましたか?[ここ]をクリックして、今すぐ参加しましょう!
レッスン16: リアルタイムクロックモジュール (DS1302)
このレッスンでは、ESP32開発ボードを使用してリアルタイムクロック (RTC) モジュールの設定と使用方法を学びます。DS1302 RTCモジュールの統合、その機能の理解、および現在の日付と時刻を表示するようにESP32をプログラムする方法を説明します。また、RTCが日付と時刻の設定を失った場合に、それをスケッチのコンパイル時間に自動的に設定する方法についても学びます。このプロジェクトは、マイクロコントローラープロジェクトにおける時間関連の機能の理解を深めたい人に最適です。
必要な部品
このプロジェクトには以下の部品が必要です。
すべての部品が揃ったキットを購入すると便利です。リンクはこちら:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Universal Maker Sensor Kit |
94 |
以下のリンクから個別に購入することもできます。
Component Introduction |
Purchase Link |
---|---|
ESP32 & Development Board |
|
配線
![../_images/Lesson_16_DS1302_esp32_bb.png](../_images/Lesson_16_DS1302_esp32_bb.png)
コード
注釈
ライブラリをインストールするには、Arduino Library Managerを使用して "Rtc by Makuna" を検索し、インストールしてください。
コード解析
初期化とライブラリのインクルード
注釈
ライブラリをインストールするには、Arduino Library Managerを使用して "Rtc by Makuna" を検索し、インストールしてください。
ここでは、DS1302 RTCモジュールに必要なライブラリがインクルードされます。
#include <ThreeWire.h> #include <RtcDS1302.h>
ピンの定義とRTCインスタンスの作成
通信用のピンが定義され、RTCのインスタンスが作成されます。
const int IO = 27; // DAT const int SCLK = 14; // CLK const int CE = 26; // RST ThreeWire myWire(IO, SCLK, CE)); RtcDS1302<ThreeWire> Rtc(myWire);
setup()
関数この関数はシリアル通信を初期化し、RTCモジュールをセットアップします。RTCが正しく動作しているかどうかを確認するためのさまざまなチェックが行われます。
void setup() { Serial.begin(9600); Serial.print("compiled: "); Serial.print(__DATE__); Serial.println(__TIME__); Rtc.Begin(); RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); printDateTime(compiled); Serial.println(); if (!Rtc.IsDateTimeValid()) { // Common Causes: // 1) first time you ran and the device wasn't running yet // 2) the battery on the device is low or even missing Serial.println("RTC lost confidence in the DateTime!"); Rtc.SetDateTime(compiled); } if (Rtc.GetIsWriteProtected()) { Serial.println("RTC was write protected, enabling writing now"); Rtc.SetIsWriteProtected(false); } if (!Rtc.GetIsRunning()) { Serial.println("RTC was not actively running, starting now"); Rtc.SetIsRunning(true); } RtcDateTime now = Rtc.GetDateTime(); if (now < compiled) { Serial.println("RTC is older than compile time! (Updating DateTime)"); Rtc.SetDateTime(compiled); } else if (now > compiled) { Serial.println("RTC is newer than compile time. (this is expected)"); } else if (now == compiled) { Serial.println("RTC is the same as compile time! (not expected but all is fine)"); } }
loop()
関数この関数はRTCから現在の日付と時刻を定期的に取得し、シリアルモニタに表示します。また、RTCが依然として有効な日時を保持しているかどうかを確認します。
void loop() { RtcDateTime now = Rtc.GetDateTime(); printDateTime(now); Serial.println(); if (!now.IsValid()) { // Common Causes: // 1) the battery on the device is low or even missing and the power line was disconnected Serial.println("RTC lost confidence in the DateTime!"); } delay(5000); // five seconds }
日時の印刷関数
RtcDateTime
オブジェクトを受け取り、フォーマットされた日時をシリアルモニタに表示するヘルパー関数。void printDateTime(const RtcDateTime& dt) { char datestring[20]; snprintf_P(datestring, countof(datestring), PSTR("%02u/%02u/%04u %02u:%02u:%02u"), dt.Month(), dt.Day(), dt.Year(), dt.Hour(), dt.Minute(), dt.Second()); Serial.print(datestring); }