Arduino DS3234 RTC output: YYYY-MM-DD hh:mm:ss

Original Code Snippet from sparkfun,
added YYYY-MM-DD hh:mm:ss output
i used Arduino MEGA-2560 via USB, power from USB,
important: i have to use SPI_MODE3 instead of SPI_MODE1 !!!
SPI_MODE1 delivered bullsh**

PinSetup
RTC – MEGA2560
————–
GND – GND
VCC – 3.3V
SQW – GND (squarewave signal output for interupters)
CLK – 52
MISO – 50
MOSI – 51
SS – 53


#include 
const int  cs=53; //chip select 

void setup() {
  Serial.begin(9600);
  RTC_init();
  //day(1-31), month(1-12), year(0-99), hour(0-23), minute(0-59), second(0-59)
  //SetTimeDate(27,4,12,16,5,0); // manuell Zeit setzen, kein 00 benutzen!
}

void loop() {
  Serial.println(ReadTimeDate());
  delay(1000);
}
//=====================================
int RTC_init(){ 
	  pinMode(cs,OUTPUT); // chip select
	  // start the SPI library:
	  SPI.begin();
	  SPI.setBitOrder(MSBFIRST); 
	  SPI.setDataMode(SPI_MODE3); // both mode 1 & 3 should work 
	  //set control register 
	  digitalWrite(cs, LOW);  
	  SPI.transfer(0x8E);
	  SPI.transfer(0x60); //60= disable Osciallator and Battery SQ wave @1hz, temp compensation, Alarms disabled
	  digitalWrite(cs, HIGH);
	  delay(10);
}
//=====================================
int SetTimeDate(int d, int mo, int y, int h, int mi, int s){ 
	int TimeDate [7]={s,mi,h,0,d,mo,y};
	for(int i=0; i<=6;i++){
		if(i==3)
			i++;
		int b= TimeDate[i]/10;
		int a= TimeDate[i]-b*10;
		if(i==2){
			if (b==2)
				b=B00000010;
			else if (b==1)
				b=B00000001;
		}	
		TimeDate[i]= a+(b<<4);
		  
		digitalWrite(cs, LOW);
		SPI.transfer(i+0x80); 
		SPI.transfer(TimeDate[i]);        
		digitalWrite(cs, HIGH);
  }
}
//=====================================
String ReadTimeDate(){
	String temp;
	int TimeDate [7]; //second,minute,hour,null,day,month,year		
	for(int i=0; i<=6;i++){
		if(i==3)
			i++;
		digitalWrite(cs, LOW);
		SPI.transfer(i+0x00); 
		unsigned int n = SPI.transfer(0x00);        
		digitalWrite(cs, HIGH);
		int a=n & B00001111;    
		if(i==2){	
			int b=(n & B00110000)>>4; //24 hour mode
			if(b==B00000010)
				b=20;        
			else if(b==B00000001)
				b=10;
			TimeDate[i]=a+b;
		}
		else if(i==4){
			int b=(n & B00110000)>>4;
			TimeDate[i]=a+b*10;
		}
		else if(i==5){
			int b=(n & B00010000)>>4;
			TimeDate[i]=a+b*10;
		}
		else if(i==6){
			int b=(n & B11110000)>>4;
			TimeDate[i]=a+b*10;
		}
		else{	
			int b=(n & B01110000)>>4;
			TimeDate[i]=a+b*10;	
			}
	}
        /* ORIG output
	temp.concat(TimeDate[4]); // tag
	temp.concat("/") ;
	temp.concat(TimeDate[5]); // monat
	temp.concat("/") ;
	temp.concat(TimeDate[6]); // jahr
	temp.concat("     ") ;
	temp.concat(TimeDate[2]); // stunde
	temp.concat(":") ;
	temp.concat(TimeDate[1]); // minute
	temp.concat(":") ;
	temp.concat(TimeDate[0]); // sekunde
        */
        
        /////
        // custom
        
        //temp.concat(" -- ");
        
        String DD = String( TimeDate[4] );
        if (DD.length()==1){ DD = "0"+DD; }
        
        String MM = String( TimeDate[5] );
        if (MM.length()==1){ MM = "0"+MM; }

        String YYYY = String( TimeDate[6] );
        if (YYYY.length()==1){ YYYY = "200"+YYYY; }
        if (YYYY.length()==2){ YYYY = "20"+YYYY;  } // ;o)
        if (YYYY.length()==3){ YYYY = "2"+YYYY;  }  // ;o)
        
        temp.concat( YYYY );
        temp.concat( "-" );
        temp.concat( MM );
        temp.concat( "-" );
        temp.concat( DD );
        
        
        String hh = String( TimeDate[2] );
        if (hh.length()==1){ hh = "0"+hh; }
        
        String mm = String( TimeDate[1] );
        if (mm.length()==1){ mm = "0"+mm; }
        
        String ss = String( TimeDate[0] );
        if (ss.length()==1){ ss = "0"+ss; }
        
        temp.concat(" ");
        temp.concat( hh );
        temp.concat( ":" );
        temp.concat( mm );
        temp.concat( ":" );
        temp.concat( ss );
        //
        /////
  return(temp);
}

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.