// ####################################### // デジタル時計 // // 2016/07 Ver0.11 // ####################################### // include the library code: #include /* SD1602HUDB LiquidCrystal Pin Mapping * LCD RS pin to digital PD3 * LCD Enable pin to digital PD5 * LCD D4 pin to digital PD6 * LCD D5 pin to digital PD7 * LCD D6 pin to digital PB0 * LCD D7 pin to digital PB1 * LCD R/W pin to GND */ #include //RTC8564NBの時計データ byte seconds; // 秒 byte minutes; // 分 byte hours; // 時 byte days; // 日 byte weekdays; // 曜日 byte months; // 月 int years; // 年 char command='z'; //コマンド // initialize the library with the numbers of the interface pins LiquidCrystal lcd(3, 5, 6, 7, 8, 9); void setup() { Serial.begin(38400); pinMode(13, OUTPUT); // LED lcd.begin(16, 2); lcd.setCursor(0, 0); lcd.print("Digital Clock"); analogReference(EXTERNAL); //AREF電圧を用いる Wire.begin(); // I2C初期化 マスター設定 RTC8564NB_init(); // RTC-8564NB 初期化 //attachInterrupt(0, int_timer,FALLING); //外部割り込み処理 HIGH-->LOW delay(1000); //LED_blink(13,200,2); // Green LED 点滅 lcd.clear(); //LCDの画面をクリア } void loop() { RTC8564NB_read(); // RTC-8564NB 読み出し // 時刻を表示 lcd.setCursor(0, 0); lcd.print(hours); lcd.print(":"); lcd.print(minutes); lcd.print(":"); lcd.print(seconds); lcd.print(" "); // 日付を表示 lcd.setCursor(0, 1); lcd.print(years); lcd.print("/"); lcd.print(months); lcd.print("/"); lcd.print(days); lcd.print(" "); //温度を表示 lcd.setCursor(10, 0); lcd.print((analogRead(0)*1.665-600)/10); //1730mv/1024=1.665 補正温度 -0℃ lcd.print("c"); //コマンドを受信 if(Serial.available()>0){ //detachInterrupt(0); //外部割り込み 禁止 command = Serial.read(); switch(command){ case 't': //時刻を受信 time_set(); //時刻を設定 break; case 'r': //データ送信 break; } //attachInterrupt(0, intA,FALLING); //外部割り込み 開始 command = 'z'; } delay(500); } //時刻を設定 ---- 受信データ yyyy/mm/dd hh/mm/ss --------------- void time_set() { char ts[20] = "yyyy/mm/dd hh/mm/ss"; char tb[3]; int n; //受信したバイト数 lcd.clear(); //LCDの画面をクリア lcd.print("Time set "); delay(500); n = Serial.available(); //受信したバイト数を取得 for (int i=0; i BCD Wire.write(ts[15] << 4 | ts[16] & 0x0f); // 分 Wire.write(ts[12] << 4 | ts[13] & 0x0f); // 時 Wire.write(ts[8] << 4 | ts[9] & 0x0f); // 日 Wire.write(0x00); // 曜日 Wire.write(ts[5] << 4 | ts[6] & 0x0f); // 月 Wire.write(ts[2] << 4 | ts[3] & 0x0f); // 年 Wire.endTransmission(); delay(200); lcd.clear(); //LCDの画面をクリア } //timer割り込み処理  -------------------------- void intA() { } // RTC-8564NB 初期化 ----------------------------- void RTC8564NB_init() { delay(1000); // 作動開始までの待ち時間 =1秒 Wire.beginTransmission(0x51); // スレーブアドレス Wire.write(0x00); // 書き込みレジスタのアドレス //レジスタテーブルを初期化 Wire.write(0x20); // 00 Control 1, STOP=1(機能を一時停止) Wire.write(0x00); // 01 Control 2 Wire.write(0x00); // 02 Seconds 0秒 Wire.write(0x01); // 03 Minutes  1分 Wire.write(0x07); // 04 Hours 7時 Wire.write(0x01); // 05 Days 1日 Wire.write(0x00); // 06 Weekdays 日曜日 Wire.write(0x07); // 07 Months 7月 Wire.write(0x16); // 08 Years 16年(2016) Wire.write(0x00); // 09 Minutes Alarm Wire.write(0x00); // 0A Hours Alarm Wire.write(0x00); // 0B Days Alarm Wire.write(0x00); // 0C Weekdays Alarm Wire.write(0x00); // 0D CLKOUT 端子をプルダウン=出力停止 Wire.write(0x00); // 0E Timer control Wire.write(0x00); // 0F Timer Wire.write(0x00); // 00 Control 1, STOP=0(作動開始) Wire.endTransmission(); // タイマー割り込みの設定 Wire.beginTransmission(0x51); Wire.write(0x01); // Control 2 Wire.write(0x11); // TI/TP=1, TIE=1,TF=0 Wire.endTransmission(); Wire.beginTransmission(0x51); Wire.write(0x0f); // Timer control Wire.write(0x01); // 割り込み発生秒数/1sec Wire.endTransmission(); Wire.beginTransmission(0x51); Wire.write(0x0e); // Timer control Wire.write(0x82); // TE=1,TD1=1,TD0=0 割り込み機能開始 Wire.endTransmission(); } // RTC-8564NB 読み出し ----------------------------- void RTC8564NB_read() { byte buff[7]; Wire.beginTransmission(0x51); Wire.write(0x02); // Secondsアドレスを指定 Wire.endTransmission(); // Seconds(02)からYears(08)まで7バイトを読み出す Wire.requestFrom(0x51, 7); seconds = Wire.read(); // 秒 minutes = Wire.read(); //分 hours = Wire.read(); // 時 days = Wire.read(); // 日 weekdays = Wire.read(); // 曜日 months = Wire.read(); // 月 years = Wire.read(); // 年 // 未使用ビットを削除 seconds = seconds & 0x7f; minutes = minutes & 0x7f; hours = hours & 0x3f; days = days & 0x3f; weekdays = weekdays & 0x07; months = months & 0x1f; // BCDデータを10進数に変換 seconds = Bcd2Dec(seconds); minutes = Bcd2Dec(minutes); hours = Bcd2Dec(hours); days = Bcd2Dec(days); weekdays = Bcd2Dec(weekdays); months = Bcd2Dec(months); years = Bcd2Dec(years)+2000; } // BCDデータを10進数に変換---------------------- byte Bcd2Dec(byte b) { return (((b >> 4) * 10) + (b & 0xf)); } // 10進数をBCDデータに変換----------------------------- byte Dec2Bcd(byte b) { return((b / 10) << 4 | (b % 10)); } // LED 点滅 (ピンNo、間隔(ms)、回数) ----------------------------- void LED_blink(byte Pin, byte interval, byte n) { while(n > 0) { digitalWrite(Pin, HIGH); delay(interval); digitalWrite(Pin, LOW); delay(interval); n--; } }