目前在做一個小玩具,需要用到RTC(Real Time Clock)
該模組需先設定初始時間,時間設定好後將其註解
該範例為奇數秒則熄滅Led,偶數則亮起LED
GND接GND VCC接5V CLK接Pin12 DAT接Pin11 RST接Pin10 LED正極接13Pin
程式碼如下:
/*
Name: Test.ino
Created: 2016/8/6 下午 23:48:11
Author: C.Y.Fang
*/
#include <DS1302.h>
const uint8_t LED_PIN = 13;
const uint8_t CE_PIN = 10;
const uint8_t IO_PIN = 11;
const uint8_t SCLK_PIN = 12;
const int DELAY_TIME = 1000;
DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);
int count = 0;
void setup()
{
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
//防止寫入日期時間
rtc.writeProtect(true);
//停止計時
rtc.halt(false);
//Time t(2016, 8, 6, 0, 06, 00, Time::kSaturday);
//rtc.time(t);
}
void loop()
{
Time time = rtc.time();
char buff[25];
snprintf(buff, sizeof(buff), "%04d-%02d-%02d %02d:%02d:%02d", time.yr, time.mon, time.date,
time.hr, time.min, time.sec);
if (time.sec % 2 == 0)
digitalWrite(LED_PIN, HIGH);
else
digitalWrite(LED_PIN, LOW);
Serial.println(buff);
delay(DELAY_TIME);
}
執行結果:
滅燈
開燈


