RTCモジュール、PC時間を自動に時間設定
- 2016/06/18
- 18:41
久しぶりにRTCモジュールの電源を入れたら時間がズレていた。。どうやって合わせるのか忘れてしまった。
http://project59.blog.fc2.com/blog-entry-273.html

何度か忘れて困ったので備忘録、
ArduinoのI2CでRTCモジュールを使う場合、時間設定が必要。
サンプルプログラムを実行する事で、PC時間を設定してくれるスケッチが提供されているので、それを使うと楽ちん。
下記のexampleフォルダ内のSetTimeファイル。
https://github.com/PaulStoffregen/DS1307RTC
http://project59.blog.fc2.com/blog-entry-273.html

何度か忘れて困ったので備忘録、
ArduinoのI2CでRTCモジュールを使う場合、時間設定が必要。
サンプルプログラムを実行する事で、PC時間を設定してくれるスケッチが提供されているので、それを使うと楽ちん。
下記のexampleフォルダ内のSetTimeファイル。
https://github.com/PaulStoffregen/DS1307RTC
//RTCモジュールに電源がONになっている事
//シリアルモニターを立ち上げると、実行される
//設定に知事が表示される。
#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;
}
- 関連記事
-
-
ロータリーエンコーダー用のArduinoライブラリ
-
割り込み実験
-
AE-ATMEGA328-MINIの入力電圧MAXは本当に12Vなんですか?
-
備忘録: LCD 2004 20x4 Display Shield Blue Backlight
-
RTCモジュール、PC時間を自動に時間設定
-
ATMEGA328P I/Oの電圧
-
AE-ATMEGA328-MINIが供給可能な消費電流→150mA
-
EEPROM実験
-
TMGA328Pブートローダー入り
-
LCDチカチカ TX/RX
-
Arduino続き(ロータリー、DDS、LCD) RIT機能
-
アナログピンをデジタルで使う
-
Arduino続き
-
Arduino購入
-