#include //http://github.com/JChristensen/DS3232RTC
#include //http://www.arduino.cc/playground/Code/Time
#include //http://arduino.cc/en/Reference/Wire (included withArduino IDE)
int latchPin = 10;
int clockPin = 11;
int dataPin = 12;
int enablePin = 9;
int hour_adjust=8;
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(hour_adjust, INPUT);
digitalWrite(latchPin, 0);
digitalWrite(enablePin, 1); //make sure data isn't latched
digitalWrite(hour_adjust, HIGH);
if(digitalRead(hour_adjust)==HIGH)
setSyncProvider(RTC.get);
else
setSyncProvider(wintertime);
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
setSyncInterval(30);
// RTC.set(1516773120+60);
}
time_t wintertime(){
time_t time=RTC.get()+3600;
return time;
}
long mylastmillis=0;
// long myinterval=30000;
int mylasthour=0;
int mylastminute=0;
void loop() {
mylastmillis=millis();
if(mylasthour!=hour()|| mylastminute!=minute())
{
settime(hour(), minute(), 0);
mylasthour=hour();
mylastminute=minute();
}
}
void settime(unsigned int Stunden, unsigned int Minuten, unsigned int temp) {
shiftOut(dataPin, clockPin, LSBFIRST,swapNibbles(reverse(mysort(uint2bcd(Minuten)))));
shiftOut(dataPin, clockPin, LSBFIRST,swapNibbles(reverse(mysort(uint2bcd(Stunden)))));
digitalWrite(latchPin, 1); // flick the latch to putthe data on the output pins
delay(100);
digitalWrite(latchPin, 0);
delay(100);
}
unsigned char reverse(unsigned char b) {
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
return b;
}
static unsigned int uint2bcd(unsigned int ival)
{
return ((ival / 10) << 4) | (ival % 10);
}
int MeineFolge[9] = {
2, 1, 0, 3, 6, 5, 4, 7};
unsigned int mysort(unsigned int ival)
{
uint8_t mytempival = 0;
for (int i = 0; i < 7; i++) {
if (bitRead(ival, i) == 1) {
bitWrite(mytempival, MeineFolge[i], 1);
}
}
return mytempival;
}
unsigned char swapNibbles(unsigned char x)
{
return ( (x & 0x0F) << 4 | (x & 0xF0) >> 4 );
}