Pages: [1]   Go Down
Author Topic: SPI problem/SD card/DS3234  (Read 1440 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi
I am trying to use the DS3234 breakout board from sparkfun along with a sd card breakout board
The problem is that I can set the RTC by itself with the code provided by sparkfun and tronixstuff's tutorial and it works fine...
when I add code to log the time (part from the ladyadas' tutorial) to the sd the time I get to serial monitor is wrong (THE OUTPUT AT THE SERIAL MONITOR
Initializing SD card...card initialized.
Logging to: LOGGR021.CSV
0/12/40--4:0:21
0/12/120--4:0:22
0/12/120--4:0:23
0/12/120--4:0:24
0/12/120--4:0:28
)...
If I download the ds3234 code again without setting the clock the time is correct...
I have set the chip select pins at the code the same as I did on breadboard and also set pin 10 as output....
Can you please propose a solution?
best regards

Gjango
Code:
#include <SPI.h>
#include <SD.h>

File logfile; // the logging file

const int  cs=7; //chip select
const int chipSelect = 9;  // chip select for SD

//  **********************************   SAMPLING   ********************************************
#define LOG_INTERVAL  2000 // mills between entries (reduce to take more/faster data)
#define SYNC_INTERVAL 2000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0;   // time of last sync()
// *********************************************************************************************


//  ****************************   ECHO_TO_SERIAL   ********************************************
#define ECHO_TO_SERIAL   1 // echo data to serial port
// *********************************************************************************************



// **************************************************************************************************
// **********************************    SETUP      *************************************************
// **************************************************************************************************

void setup() {
  Serial.begin(9600);
RTC_init();
pinMode(7, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);




// initialize the SD card
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to output, even if you don't use it:
  
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    error("Card failed, or not present");
  }
  Serial.println("card initialized.");
  
 //  ***************** MAKE FILENAME WITH THREE DIGITS  ****************************
 char filename[] = "LOGGR000.CSV";
      for (uint8_t i = 0; i < 1000; i++) {
        uint8_t j=i/10;
        filename[5] = j/10 + '0';
        filename[6] = j%10 + '0';
        filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE);
      break;  // leave the loop!
    }
  }
  
  if (! logfile) {
    error("couldnt create file");
  }
  Serial.print("Logging to: ");
  Serial.println(filename);



// USE THE FOLLOWING LINE OF CODE TO SET TIME...UPLOAD...THEN COMMENT...UPLOAD AGAIN
//  //day(1-31), month(1-12), year(0-99), hour(0-23), minute(0-59), second(0-59)  
//SetTimeDate(9,7,11,9,26,16);
}

void loop() {
  logfile.println(ReadTimeDate());
  Serial.println(ReadTimeDate());
  delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
  
   if ((millis() - syncTime) < SYNC_INTERVAL) return;
  syncTime = millis();
  
  logfile.flush();
}
//=====================================
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[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;
}
}
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);
}
void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
    while(1);
}

« Last Edit: July 11, 2011, 03:07:40 am by gjango » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8653
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My guess is that the SD library is changing bit order, data mode, or clock divisor of the SPI interface.  Some combination of such changes can prevent communication with the clock.  What you can try is to re-set the SPI setting before each transfer.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you johnwasser for your reply...
I did what you advised but still no luck...now I get the date correct but not the time...

Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8653
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you johnwasser for your reply...
I did what you advised but still no luck...now I get the date correct but not the time...

Does the time you are getting have any relation to the actual time?  The seconds were incrementing.  Do the hours and minutes change?  How far off is the time?   
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I added a small delay of 200ms between the two initialisations (the RTC and the SD) and in this way the time is correct now at the serial monitor but...
when I write the time to the sd since the spi settings are probably different the data are wrong..the date is wrong but with constant value and the time has pattern of 4-5 values that are repeating in random order...
Logged

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1598
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

SD cards use SPI mode 0.

SD.h is a wrapper for an old version of my SdFat library that does not reinitialize the SPI registers.

You will need to restore the SPI mode if you use SD.h.

The new version of SdFat, http://code.google.com/p/sdfatlib/downloads/list, sets all SPI options each time it accesses the SPI bus.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I added the new version of the SDFat Library and everything works fine now!

Thank u fat16lib and johnwasser for your help
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 210
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I have the same problem, but I don't understand very well how to resolve this problem ?
Logged

Pages: [1]   Go Up
Jump to: