準備
◆Arduino IDE (PCにインストール無料)
◆Arduino Uno(360円+送料170円)
◆Arduino IDE(PC) と Arduino Uno をつなぐUSBケーブル(100円+送料170円)
◆Arduino IDE (PCにインストール無料)
◆Arduino Uno(360円+送料170円)
◆Arduino IDE(PC) と Arduino Uno をつなぐUSBケーブル(100円+送料170円)
◆リアルタイムクロックモジュール DS1302 (100円+送料170円)
内臓メモリSRAM 31 byte に読み書き可
ボタン電池でメモリ内データ保存
Data sheet
Data sheet
工作開始
◆接続します
◆接続を確認します
確認1
確認2(Arduinoがつながっているポート)
◆スケッチを書きます
/*---- created 27 Oct 2016 by dd647 -------*/
int DAT_pin = 3;
int CLK_pin = 4;
int RST_pin = 2;
void set(int c, int v){
digitalWrite(CLK_pin, LOW);
digitalWrite(RST_pin, HIGH);
shiftOut(DAT_pin, CLK_pin, LSBFIRST, c);//Command write
shiftOut(DAT_pin, CLK_pin, LSBFIRST, v);//set time
digitalWrite(RST_pin, LOW);
}
int read_t(int c){
int Bit = 0;
int v = 0;
digitalWrite(CLK_pin, LOW);
digitalWrite(RST_pin, HIGH);
shiftOut(DAT_pin, CLK_pin, LSBFIRST, c);//Command read
for (int i = 0; i < 8; ++i)//read time
{
Bit = digitalRead(DAT_pin);
Serial.print(Bit);
v |= (Bit << i);
digitalWrite(CLK_pin, HIGH);
delayMicroseconds(1);
digitalWrite(CLK_pin, LOW);
}
digitalWrite(RST_pin, LOW);
Serial.print(" ");
int t = (v & 15) + (10 * (v >> 4)) ;
return(t);
}
void setup()
{
pinMode(CLK_pin,OUTPUT);
pinMode(RST_pin,OUTPUT);
Serial.begin(9600);
Serial.println("reset time ? [y or n]");
while(Serial.available() == 0){}
int c = Serial.read();
if(c == 121){
pinMode(DAT_pin,OUTPUT);
set(128,72);//Seconds set to 48 or set(128,0x48);
set(130,89);//Minutes set to 59 or set(130,0x59);
set(132,35);//Hour set to 23 or set(132,0x23);
set(134,49);//Date set to 31 or set(134,0x31);
set(136,16);//Month set to 10 or set(136,0x10);
set(140,22);//Year set to 16 or set(140,0x16);
pinMode(DAT_pin, INPUT);
}else{
pinMode(DAT_pin, INPUT);
}
Serial.println(" s| 10s| m| 10m| h| 10h| d| 10d|");
}
void loop()
{
int S = read_t(129);//read Second
int MI = read_t(131);//read Minute
int H = read_t(133);//read Hour
int D = read_t(135);//read Date
int MO = read_t(137);//read Month
int Y = read_t(141);//read Year
Serial.print(" ");
Serial.print(S);
Serial.print("s ");
Serial.print(MI);
Serial.print("mi ");
Serial.print(H);
Serial.print("h ");
Serial.print(D);
Serial.print("d ");
Serial.print(MO);
Serial.print("mo ");
Serial.print("20");
Serial.print(Y);
Serial.println("y ");
delay(1000);
}
int DAT_pin = 3;
int CLK_pin = 4;
int RST_pin = 2;
void set(int c, int v){
digitalWrite(CLK_pin, LOW);
digitalWrite(RST_pin, HIGH);
shiftOut(DAT_pin, CLK_pin, LSBFIRST, c);//Command write
shiftOut(DAT_pin, CLK_pin, LSBFIRST, v);//set time
digitalWrite(RST_pin, LOW);
}
int read_t(int c){
int Bit = 0;
int v = 0;
digitalWrite(CLK_pin, LOW);
digitalWrite(RST_pin, HIGH);
shiftOut(DAT_pin, CLK_pin, LSBFIRST, c);//Command read
for (int i = 0; i < 8; ++i)//read time
{
Bit = digitalRead(DAT_pin);
Serial.print(Bit);
v |= (Bit << i);
digitalWrite(CLK_pin, HIGH);
delayMicroseconds(1);
digitalWrite(CLK_pin, LOW);
}
digitalWrite(RST_pin, LOW);
Serial.print(" ");
int t = (v & 15) + (10 * (v >> 4)) ;
return(t);
}
void setup()
{
pinMode(CLK_pin,OUTPUT);
pinMode(RST_pin,OUTPUT);
Serial.begin(9600);
Serial.println("reset time ? [y or n]");
while(Serial.available() == 0){}
int c = Serial.read();
if(c == 121){
pinMode(DAT_pin,OUTPUT);
set(128,72);//Seconds set to 48 or set(128,0x48);
set(130,89);//Minutes set to 59 or set(130,0x59);
set(132,35);//Hour set to 23 or set(132,0x23);
set(134,49);//Date set to 31 or set(134,0x31);
set(136,16);//Month set to 10 or set(136,0x10);
set(140,22);//Year set to 16 or set(140,0x16);
pinMode(DAT_pin, INPUT);
}else{
pinMode(DAT_pin, INPUT);
}
Serial.println(" s| 10s| m| 10m| h| 10h| d| 10d|");
}
void loop()
{
int S = read_t(129);//read Second
int MI = read_t(131);//read Minute
int H = read_t(133);//read Hour
int D = read_t(135);//read Date
int MO = read_t(137);//read Month
int Y = read_t(141);//read Year
Serial.print(" ");
Serial.print(S);
Serial.print("s ");
Serial.print(MI);
Serial.print("mi ");
Serial.print(H);
Serial.print("h ");
Serial.print(D);
Serial.print("d ");
Serial.print(MO);
Serial.print("mo ");
Serial.print("20");
Serial.print(Y);
Serial.println("y ");
delay(1000);
}
→
◆シリアルモニタで見てみます
input y