注釈
こんにちは、SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Communityへようこそ!Facebook上で、仲間と一緒にRaspberry Pi、Arduino、ESP32をさらに深く探求しましょう。
なぜ参加するのか?
専門的なサポート:購入後の問題や技術的な課題をコミュニティやチームの助けを借りて解決。
学びと共有:スキルを向上させるためのヒントやチュートリアルを交換。
限定プレビュー:新製品発表や予告編に早期アクセス。
特別割引:最新製品の特別割引を楽しむ。
フェスティブプロモーションとプレゼント:プレゼントやホリデープロモーションに参加。
👉 私たちと一緒に探索と創造を始める準備はできましたか?[ここ]をクリックして、今すぐ参加しましょう!
レッスン16: リアルタイムクロックモジュール (DS1302)
このレッスンでは、Arduinoを使用してリアルタイムクロック(RTC)モジュールを設定し、使用する方法を学びます。RTC DS1302モジュールの初期化、現在の日付と時刻をシリアルモニターに表示する方法、および正確な時間管理を行う方法について説明します。このセッションは、組み込みシステムにおける時間ベースの操作に興味がある方に最適で、日付と時間の設定、RTCライブラリの使用、および一般的な問題のトラブルシューティングの実践経験を提供します。このプロジェクトは、Arduinoの基本を理解している中級学習者に適しています。
必要なコンポーネント
このプロジェクトでは、以下のコンポーネントが必要です。
一式をまとめて購入すると便利です。リンクはこちら:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Universal Maker Sensor Kit |
94 |
以下のリンクから個別に購入することもできます。
Component Introduction |
Purchase Link |
---|---|
Arduino UNO R3 or R4 |
|
配線
コード
注釈
ライブラリをインストールするには、Arduinoライブラリマネージャーを使用して "Rtc by Makuna" を検索し、インストールしてください。
コード解析
初期化とライブラリのインクルード
注釈
ライブラリをインストールするには、Arduinoライブラリマネージャーを使用して "Rtc by Makuna" を検索し、インストールしてください。
ここでは、DS1302 RTCモジュール用の必要なライブラリをインクルードします。
#include <ThreeWire.h> #include <RtcDS1302.h>
ピンの定義とRTCインスタンスの作成
通信のためのピンが定義され、RTCのインスタンスが作成されます。
const int IO = 4; // DAT const int SCLK = 5; // CLK const int CE = 2; // RST ThreeWire myWire(4, 5, 2); // 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); }