nobcha23の日記

PICマイコンやArduinoを使う電子回路遊びを紹介します

ブログを参考にしたRTCのスケッチ

RTCのデータをLCDに出すというスケッチはKOSAKAさんのブログ書き込み#8を引用しました。参考にさせていただき感謝します。

ばんとさんのコメントを見て、RTCライブラリーがあることがわかりました。ライブラリーを持ってきて関数機能を理解し組み込んだ方が早いか、以前picで作ったRTC内データ、LCD表示データ、up/downスイッチでの時間設定モジュール群を持って来たほうが早いかの選択があります。どうしましょうか・・。

ところで、今回の引用スケッチは次です。

// Original SOURCE CODE is based on  KOSAKA's BLOG
// http://www.geocities.co.jp/arduino_diecimila/use/index.html#_lcd

#include <Wire.h>
#include <LiquidCrystal.h>

// Lcd PIN assign
// RS:5,RW:7,en:6,D0-D3:9-12
LiquidCrystal lcd(5, 7, 6, 9, 10, 11, 12);

// Global value
int RTC = 0xA2 >> 1;
byte sw_reg=0xff;
byte read_buff[7];

// Initial bcd date data:sec,  min, hour,  day,  week,  man, year
byte write_buff[7]   =  {0x50, 0x59, 0x23, 0x31,  0x00, 0x12, 0x08};

void setup()
{
  Wire.begin();    // I2c setup
  lcd.begin(16, 2);  // 16x2 lcd
  lcd.clear();     // clear LCD
  init_RTC();	   // set initial time VALUE
}

void loop()
{
  read_RTC();     // Get DATA from RTC
  if (read_buff[0] != sw_reg){
    lcd.clear();
    lcd.print("Date 20");
    put_date();
    lcd.setCursor(0, 1);
    lcd.print("Time   ");
    put_time();
    sw_reg=read_buff[0];
  }
}

void put_date()     // Put BCD Date to LCD
{
  put(read_buff[6] & 0x3F);
  lcd.print("/");
  put(read_buff[5] & 0x1F);
  lcd.print("/");
  put(read_buff[3] & 0x3F);
}

void put_time()     // Put TIME to LCD
{
  put(read_buff[2] & 0x3F);
  lcd.print(":");
  put(read_buff[1] & 0x7F);
  lcd.print(":");
  put(read_buff[0] & 0x7F);
}

void put(byte buff)
{
  if(buff < 16) lcd.print("0");  // ZERO PADDING
  lcd.print(buff, HEX);		 // RTC data is BCD
}  

//
// The below code are REFFERED from 
// Http://tokoya.typepad.jp/blog/2008/11/arduinortc-6cdd.html (Link DISCONNECTED)
//

void init_RTC() 
{
  Wire.beginTransmission(RTC);
  Wire.write(0x02);
  Wire.write(write_buff, 7);
  Wire.endTransmission();
}

void read_RTC()
{
  Wire.beginTransmission(RTC);  // OPEN
  Wire.write(0x02);		// MODE
  Wire.endTransmission();	// STOP
  Wire.requestFrom(RTC, 7);	// READ
  for(int i = 0; i < 7; i++)    // 7 BYTES
  {
    if(Wire.available())        // WAIT FOR DATA
    {
      read_buff[i] = Wire.read(); // READ DATA
    }
  }
}