DS3234 clock SRAM question

Status
Not open for further replies.

philip.porhammer

Well-known member
/* dead on real time clock with SRAM
How do i read and write the SRAM

*/



#include <SPI.h>
const int cs=10; //chip select RTC
long MoonTimePoint=52830;// 2bytes
int Hi=0;
int Lo=126;
int moonPoint=0;// 1
int Latitude=7;// 1
int DayOfWeek=4;// 1
int WeekOfYear=35;//1
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(24, 1, 16, 1, 30, 29);

}

void loop() {
// Serial.println(ReadTimeDate());
delay(100);
StoreData();


}
//=====================================

int StoreData(){ delay(100);
digitalWrite(cs, LOW);
SPI.transfer(0x98);
SPI.transfer(0x00);
SPI.transfer(0x01);
SPI.transfer(0x02);
SPI.transfer(0x03);
SPI.transfer(0x09);
digitalWrite(cs, HIGH);
delay(50);


int aa=99;
int ab=99;
int ac=99;
int ad=99;
int ae=99;

digitalWrite(cs, LOW);
SPI.transfer(0x18);
aa= SPI.transfer(0x00);
ab= SPI.transfer(0x00);
ac= SPI.transfer(0x00);
ad= SPI.transfer(0x00); Lo= SPI.transfer(0x00);
ae= SPI.transfer(0x00);
digitalWrite(cs, HIGH);

Serial.println(" ") ;
Serial.print(" o = ") ;
Serial.print(aa) ; Serial.print(" o = ") ;
Serial.print(ab) ; Serial.print(" o = ") ;
Serial.print(ac) ; Serial.print(" o = ") ;
Serial.print(ad) ; Serial.print(" o = ") ;
Serial.print(ae) ; Serial.print(" o = ") ;
Serial.println(" lo ") ;



}



int RTC_init(){
pinMode(cs,OUTPUT); // chip select
// start the SPI library:
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE1); // 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/10;
int a= TimeDate-b*10;
if(i==2){
if (b==2)
b=B00000010;
else if (b==1)
b=B00000001;
}
TimeDate= a+(b<<4);

digitalWrite(cs, LOW);
SPI.transfer(i+0x80);
SPI.transfer(TimeDate);
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=a+b;
}
else if(i==4){
int b=(n & B00110000)>>4;
TimeDate=a+b*10;
}
else if(i==5){
int b=(n & B00010000)>>4;
TimeDate=a+b*10;
}
else if(i==6){
int b=(n & B11110000)>>4;
TimeDate=a+b*10;
}
else{
int b=(n & B01110000)>>4;
TimeDate=a+b*10;
}
}
temp.concat(TimeDate[4]);
temp.concat("/") ;
temp.concat(TimeDate[5]);
temp.concat("/") ;
temp.concat(TimeDate[6]);
temp.concat(" ") ;
temp.concat(TimeDate[2]);
temp.concat(":") ;
temp.concat(TimeDate[1]);
temp.concat(":") ;
temp.concat(TimeDate[0]);
return(temp);
}
 
It would help if you copied and pasted the output that your code prints on the serial monitor.

But I note that the StoreData function writes 6 values to the ram but only reads 5 of them (what is Lo?).
You also have the Serial.print arranged in a way that is confusing.
Code:
Serial.print(" o = ") ;
Serial.print(aa) ; Serial.print(" o = ") ;
The first print of " o = " is associated with the value of aa while the next " o = " on that same line is actually associated with the value of 'ab' but that is printed on the following line. It is a lot better to put one statement per line and perhaps put blank lines between pairs of statements to group them. And also to use the Tools|Auto Format menu item to tidy your code.

Pete
 
P.S. It would be better if you printed the name of the variable: e.g.
Code:
  Serial.print(" aa = ") ;  
  Serial.print(aa) ;
  Serial.print(" ab = ") ;
  Serial.print(ab) ;
 
OK, so this is just part of 108 NeoPixel clock i am making. I need to save Moon Phase, week of the year, latitude. I would like to use the SRAM to continuously save the data for when the power goes out. The idea for this test code is to write a byte, set an int to 99, read out of the SRAM and see if it worked. I am using a Teency 3.2


#include <SPI.h>
const int cs=10; //chip select RTC
long MoonTimePoint=52830;// 2bytes
int Hi=0;
int Lo=126;
int moonPoint=0;// 1
int Latitude=7;// 1
int DayOfWeek=4;// 1
int WeekOfYear=35;//1
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(24, 1, 16, 1, 30, 29);

}

void loop() {
// Serial.println(ReadTimeDate());
delay(100);
StoreData();


}
//=====================================

int StoreData(){ delay(100);
digitalWrite(cs, LOW);
delay(1);
SPI.transfer(0x98);//Address?
SPI.transfer(0x00); //data at location 0?
SPI.transfer(0x01);
SPI.transfer(0x02);
SPI.transfer(0x03);
SPI.transfer(0x04); //data at location 4?
digitalWrite(cs, HIGH);
delay(50);


int aa=99;
int ab=99;
int ac=99;
int ad=99;
int ae=99;

digitalWrite(cs, LOW);
delay(1);
SPI.transfer(0x18);
aa= SPI.transfer(0x00);
ab= SPI.transfer(0x00);
ac= SPI.transfer(0x00);
ad= SPI.transfer(0x00);
ae= SPI.transfer(0x00);
digitalWrite(cs, HIGH);

Serial.println(" ") ;
Serial.print(" o = ") ; Serial.print(aa) ; Serial.println(" ") ;
Serial.print(" 1 = ") ; Serial.print(ab) ; Serial.println(" ") ;
Serial.print(" 2 = ") ; Serial.print(ac) ; Serial.println(" ") ;
Serial.print(" 3 = ") ; Serial.print(ad) ; Serial.println(" ") ;
Serial.print(" 4 = ") ; Serial.print(ad) ; Serial.println(" ") ;
Serial.println(" ") ;



}



int RTC_init(){
pinMode(cs,OUTPUT); // chip select
// start the SPI library:
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE1); // 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/10;
int a= TimeDate-b*10;
if(i==2){
if (b==2)
b=B00000010;
else if (b==1)
b=B00000001;
}
TimeDate= a+(b<<4);

digitalWrite(cs, LOW);
SPI.transfer(i+0x80);
SPI.transfer(TimeDate);
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=a+b;
}
else if(i==4){
int b=(n & B00110000)>>4;
TimeDate=a+b*10;
}
else if(i==5){
int b=(n & B00010000)>>4;
TimeDate=a+b*10;
}
else if(i==6){
int b=(n & B11110000)>>4;
TimeDate=a+b*10;
}
else{
int b=(n & B01110000)>>4;
TimeDate=a+b*10;
}
}
temp.concat(TimeDate[4]);
temp.concat("/") ;
temp.concat(TimeDate[5]);
temp.concat("/") ;
temp.concat(TimeDate[6]);
temp.concat(" ") ;
temp.concat(TimeDate[2]);
temp.concat(":") ;
temp.concat(TimeDate[1]);
temp.concat(":") ;
temp.concat(TimeDate[0]);
return(temp);
}
 
Status
Not open for further replies.
Back
Top