Arduino: RTC DS1302 Unix Time

From OnnoWiki
Jump to navigation Jump to search

Code

// Example sketch for interfacing with the DS1302 timekeeping chip.
//
#include <stdio.h>
#include <Time.h>
#include <DS1302.h>

// Create a DS1302 object.
DS1302 rtc(8, 7, 6);

void setup() {
  Serial.begin(9600); 

  // Initialize a new chip by turning off write protection and clearing the
  // clock halt flag. These methods needn't always be called. See the DS1302
  // datasheet for details.
  rtc.writeProtect(false);
  rtc.halt(false);
  Time t = rtc.getTime();
  setTime(t.hour,t.min,t.sec,t.date,t.mon,t.year);
}

// Loop and print unix time every second.
void loop() {
  Serial.println(now());
  delay(1000);
}